Java 尝试执行HTTP Post时获取NetworkOnMainThread错误

Java 尝试执行HTTP Post时获取NetworkOnMainThread错误,java,android,http-post,networkonmainthread,Java,Android,Http Post,Networkonmainthread,您好,我正在尝试向基于PHP的服务器发送一个简单的HTTP post,该服务器接受post数据$\u post['username'] public void sendMessage(View view){ String result = ""; Intent intent = new Intent(this, DisplayMessageActivity.class); EditText editText = (EditText) findViewById(R.id

您好,我正在尝试向基于PHP的服务器发送一个简单的HTTP post,该服务器接受post数据$\u post['username']

public void sendMessage(View view){

    String result = "";

    Intent intent = new Intent(this, DisplayMessageActivity.class);
    EditText editText = (EditText) findViewById(R.id.edit_message);
    String message = editText.getText().toString();
    intent.putExtra(EXTRA_MESSAGE, message);

    Toast.makeText(
            getApplicationContext(),
            "Attempting to open second activity",
            Toast.LENGTH_LONG
    ).show();

    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    nameValuePairs.add(new BasicNameValuePair("username","ghost"));

    InputStream is = null;

    // ATTEMPT HTTP POST
    try{
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://example.com/getuserbyuname");
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();
    }catch(Exception e){
            Log.e("log_tag", "Error in http connection (Reason: " + e.toString() + " )");
    }
我在android.os.NetworkOnMainThread上遇到问题。我知道这是因为我的例程试图从主UI线程中执行与网络相关的操作

我确实试过:

public class AccountConnector extends AsyncTask <String, Integer, Long>{

@Override
protected Long doInBackground(String... params)
{
    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    nameValuePairs.add(new BasicNameValuePair("username","bond"));

    InputStream is = null;

    // ATTEMPT HTTP POST
    try{
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://example.com/getuserbyuname");
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();
    }catch(Exception e){
            Log.e("log_tag", "Error in http connection (Reason: " + e.toString() + " )");
    }
    return null;
}
公共类AccountConnector扩展异步任务{
@凌驾
受保护的长doInBackground(字符串…参数)
{
ArrayList nameValuePairs=新的ArrayList();
添加(新的BasicNameValuePair(“用户名”、“债券”);
InputStream=null;
//尝试HTTP POST
试一试{
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(“http://example.com/getuserbyuname");
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
HttpResponse response=httpclient.execute(httppost);
HttpEntity=response.getEntity();
is=entity.getContent();
}捕获(例外e){
Log.e(“Log_标记”,“http连接错误(原因:“+e.toString()+”)”);
}
返回null;
}
…其余代码被省略

谁能给我指一下正确的方向吗?

加上这个

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
             .permitAll().build();
StrictMode.setThreadPolicy(policy);
把这个放在清单文件里

     <uses-permission android:name="android.permission.INTERNET"/>


解决此问题的正确方法是使用网络访问

处理此问题的惰性方法是将检查转到:

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);

出于好奇:为什么您希望网络I/O在主线程中运行?使用AsynchTask()示例:@mthmulders基本上检查数据库中是否存在被查询的uname。@KaranMavadhiya正在研究这个问题。谢谢。我确实研究了您的参考资料。不过,引用文章中的内容是这样的“StrictMode只能在开发过程中使用,而不能在实时应用程序中使用。”
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);