android.os.NetworkOnMainThreadException
我有这个例外,我正在阅读关于这个的一个线索,而且看起来很混乱:
如何解决android.os.NetworkOnMainThreadException?
我已经将此行添加到我的清单中:
<uses-permission android:name="android.permission.INTERNET" />
在那个讨论中,他们讨论了应用程序的主要执行线程无法联网。 我想知道的是如何重构我的代码,以便与Android良好实践保持一致。
这里是我的Activity类:
package com.problemio;
import java.io.InputStream;
import java.util.ArrayList;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class LoginActivity extends Activity
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
// Show form for login_email
final EditText loginEmail = (EditText) findViewById(R.id.login_email);
String name = loginEmail.getText().toString();
// Show field for password
final EditText password = (EditText) findViewById(R.id.password);
String text = password.getText().toString();
// Show button for submit
Button submit = (Button)findViewById(R.id.submit);
// Show options for create-profile and forgot-password
submit.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
String email = loginEmail.getText().toString();
String pass = password.getText().toString();
sendFeedback(pass, email);
}
});
}
public void sendFeedback(String pass , String email)
{
Log.d( "1" , pass );
Log.d( "1" , email );
// Go to db and check if these r legit
// How do I do that? :)
ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("username", email ));
postParameters.add(new BasicNameValuePair("password", pass ));
String responseString = null;
try
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("myUrl");
// no idea what this does :)
httppost.setEntity(new UrlEncodedFormEntity(postParameters));
// This is the line that send the request
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
}
catch (Exception e)
{
Log.e("log_tag", "Error in http connection "+e.toString());
}
}
}
我在这里做错了什么,我该如何解决它? :) 谢谢!!
NetworkOnMainThreadException:应用程序尝试在其主线程上执行联网操作时引发的异常。
你应该在asynctask上调用sendfeedback方法,然后只有上面的代码才能工作。 由于Web服务器花费大量时间响应主线程变得无法响应。 为了避免它,你应该在另一个线程上调用它。 因此,asynctask更好。
这里是说明如何使用asynctask的链接
当您的应用程序在主线程中尝试联网时,会引发NetworkOnMainThreadException 。
为了解决这个问题,你可以在你的Activity中使用一个私有的内部类来扩展android.os.AsyncTask<Params, Progress, Result>
,它将执行服务器调用的东西。
作为,
private class SendfeedbackJob extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String[] params) {
// do above Server call here
return "some message";
}
@Override
protected void onPostExecute(String message) {
//process message
}
}
然后像下面那样从submit.setOnClickListener
调用上面的类,
SendfeedbackJob job = new SendfeedbackJob();
job.execute(pass, email);
参考
AsyncTask文档
AsyncTask Android示例
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
链接地址: http://www.djcxy.com/p/29515.html
上一篇: android.os.NetworkOnMainThreadException
下一篇: How to post JSON object to an api and receive raw text in Android using Volley