【问题标题】:Getting NetworkOnMainThread error when trying to do HTTP Post尝试执行 HTTP Post 时出现 NetworkOnMainThread 错误
【发布时间】:2013-07-26 15:47:53
【问题描述】:

您好,我正在尝试向基于 PHP 的服务器进行简单的 HTTP 发布,该服务器接受 POST 数据 $_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() + " )");
    }

然后服务器将返回一个 JSON 格式的消息,如下所示:

{"response":"404"}

我遇到了 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;
}

...省略其余代码...

谁能指出我正确的方向?

【问题讨论】:

标签: java android http-post networkonmainthread


【解决方案1】:

添加这个..

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

将其放入清单文件中

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

【讨论】:

    【解决方案2】:

    解决这个问题的正确方法是使用Android AsyncTask进行网络访问。

    处理这种情况的懒惰方法是打开检查:

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

    【讨论】:

    • 我确实查看了您的参考资料。然而,从文章中引用这就是它所说的“StrictMode 应该只在开发期间使用,而不是在你的实时应用程序中。”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-13
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    相关资源
    最近更新 更多