0

我收到错误发生异常: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;
        }

    }

}

主.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) {
    }
}

有没有人可以帮忙?

4

2 回答 2

2

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

于 2013-12-12T08:17:16.307 回答
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")

我将使用接口作为回调将结果传达回活动。

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

如何从 AsyncTask 返回布尔值?

还将所有与网络相关的操作移至doInbackground并更新 uionPostExecute

例子:

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) {
    }
}
于 2013-12-12T08:17:24.973 回答