发生异常:android.os.NetworkOnMainThreadException

时间:2013-12-12 08:16:03

标签: java android android-asynctask networkonmainthread

我收到错误发生异常:android.os.NetworkOnMainThreadException,即使我已经使用AsyncTask。该行的LongOperation.java出错 while ((line = reader.readLine()) != null) { 这是代码:

AsyncTaskActivity.java:

package com.example.myfirstapp;

import java.util.concurrent.ExecutionException;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class AsyncTaskActivity extends Activity implements OnClickListener {

    Button btn;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        btn = (Button) findViewById(R.id.button1);
        btn.setOnClickListener(this);
    }

    public void onClick(View view) {

        switch (view.getId()) {
        case R.id.button1:
            try {
                TextView txt = (TextView) findViewById(R.id.output);

                txt.setText(new LongOperation().execute("http://search.twitter.com/search.json?q=javacodegeeks").get());

            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (ExecutionException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            break;
        }

    }

}

main.xml中:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ProgressBar
        android:id="@+id/progressBar"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:indeterminate="false"
        android:max="10"
        android:padding="10dip" >
    </ProgressBar>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Start Progress" >
    </Button>

    <TextView
        android:id="@+id/output"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Replace" />

</LinearLayout>

LongOperation.java:

package com.example.myfirstapp;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;

import android.os.AsyncTask;
import android.util.Log;

public class LongOperation extends AsyncTask<String, Void, String> {

    InputStream is = null;

    @Override
    protected String doInBackground(String... urls) {

        try {

            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(urls[0]);
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            HttpParams myParams = new BasicHttpParams();
            HttpConnectionParams.setConnectionTimeout(myParams, 10000);
            HttpConnectionParams.setSoTimeout(myParams, 10000);

            is = entity.getContent();

        } catch (Exception e) {
            Log.e("log_tag", "Error in http connection " + e.toString());
        }
        return is.toString();
    }

    @Override
    protected void onPostExecute(String result) {
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = "";
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            result = sb.toString();
            // System.out.println("Result = " + result);

        } catch (Exception e) {
            Log.e("log_tag", "Error converting result " + e.toString());
        }
    }

    @Override
    protected void onPreExecute() {
    }

    @Override
    protected void onProgressUpdate(Void... values) {
    }
}

有人可以帮忙吗?

2 个答案:

答案 0 :(得分:2)

onPostExecute()onPreExecute()在主UI线程上运行。将网络操作移至doInBackground()

答案 1 :(得分:2)

new LongOperation().execute("http://search.twitter.com/search.json?q=javacodegeeks").get()

调用get()不会使其异步。阻止ui线程。

public final Result get ()

如果需要等待计算完成,然后检索其结果

你需要

new LongOperation().execute("http://search.twitter.com/search.json?q=javacodegeeks")

我会使用一个接口作为回调来将结果传回给活动。

类似于以下链接中黑带的回答

How do I return a boolean from AsyncTask?

同时将所有与网络相关的操作移至doInbackground并在onPostExecute更新ui

示例:

public class MainActivity extends Activity implements LongOperation.callBack {

    Button btn;
    TextView txt;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        txt = (TextView) findViewById(R.id.textView1);
        btn = (Button) findViewById(R.id.button1);
        btn.setOnClickListener(new OnClickListener()
        {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                new LongOperation(MainActivity.this).execute("http://search.twitter.com/search.json?q=javacodegeeks");
            }

        });
    }

    @Override
    public void returnText(String value) {
        // TODO Auto-generated method stub
        txt.setText(value);
    }
    }

activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:text="Button" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="48dp"
        android:text="TextView" />

</RelativeLayout>

LongOperation.java

public class LongOperation extends AsyncTask<String, Void, String> {

    InputStream is = null;
    String _response;
    callBack cb;
    ProgressDialog pd;
    interface callBack
    {
        public void returnText(String value);
    };
    public LongOperation(Context context) {
        // TODO Auto-generated constructor stub
        cb = (callBack) context;
        pd = new ProgressDialog(context);
        pd.setMessage("LongOperation...");
    }

    @Override
    protected String doInBackground(String... urls) {

        try {

            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(urls[0]);
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            HttpParams myParams = new BasicHttpParams();
            HttpConnectionParams.setConnectionTimeout(myParams, 10000);
            HttpConnectionParams.setSoTimeout(myParams, 10000);
            HttpEntity resEntity = response.getEntity();
             _response=EntityUtils.toString(resEntity);
            is = entity.getContent();

        } catch (Exception e) {
            Log.e("log_tag", "Error in http connection " + e.toString());
        }
        return _response;
    }

    @Override
    protected void onPostExecute(String result) {
      super.onPostExecute(result);
      pd.dismiss();
      if(cb!=null)
      {
          cb.returnText(result);
      }
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pd.show();
    }

    @Override
    protected void onProgressUpdate(Void... values) {
    }
}
相关问题