首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >尝试执行HTTP Post时出现NetworkOnMainThread错误

尝试执行HTTP Post时出现NetworkOnMainThread错误
EN

Stack Overflow用户
提问于 2013-07-26 19:33:29
回答 2查看 75关注 0票数 0

嗨,我正在尝试做一个简单的HTTP post到一个基于PHP的服务器,它接受一个POST数据$_POST‘’username‘。

代码语言:javascript
复制
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格式的消息,如下所示:

代码语言:javascript
复制
{"response":"404"}

我和android.os.NetworkOnMainThread有点问题。我意识到这是因为我的例程试图从主UI线程中执行与网络相关的操作。

我确实试过这个:

代码语言:javascript
复制
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;
}

..。其余的代码将被省略...

谁能给我指个方向?

EN

回答 2

Stack Overflow用户

发布于 2013-07-26 19:38:53

加上这个..

代码语言:javascript
复制
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
             .permitAll().build();
StrictMode.setThreadPolicy(policy);

将此文件放入清单文件中

代码语言:javascript
复制
     <uses-permission android:name="android.permission.INTERNET"/>
票数 0
EN

Stack Overflow用户

发布于 2013-07-26 19:40:42

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

处理此问题的懒惰方法是打开以下检查:

代码语言:javascript
复制
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17880248

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档