3

我已经使用AsyncTask. 我只是在中显示响应TextView。我不知道是对是错。它在 GingerBread 上工作并NetworkOnMainThreadException在 JellyBean 上展示。如果我使用StrictMode它,它会在 JellyBean 上工作并在 GingerBread 上强制关闭。如何编写支持Android 3.0 以上和Android 3.0 以下的代码。

public class MainActivity extends Activity {
TextView tv;
Button b;
InputStream is = null;

DefaultHttpClient client;
HttpGet get;
HttpResponse response;
HttpEntity entity;
StringBuffer buffer;

@SuppressLint("NewApi")
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
  //    StrictMode.setThreadPolicy(policy);
    tv = (TextView) findViewById(R.id.texty);
    b = (Button) findViewById(R.id.buttonGET);

    b.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            MyOperation mytask = new MyOperation();
            mytask.execute();
        }
    });

}

private class MyOperation extends AsyncTask<String, Void, String> {
    ProgressDialog dialog = new ProgressDialog(MainActivity.this);

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();
        dialog.setMessage("Loading...");
        dialog.show();
    }

    @Override
    protected String doInBackground(String... params) {
        // TODO Auto-generated method stub
        client = new DefaultHttpClient();
        get = new HttpGet("http://www.google.com");
        try {
            response = client.execute(get);
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        entity = response.getEntity();

        try {
            is = entity.getContent();
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return null;
    }

    @Override
    protected void onPostExecute(String result) {
        // TODO Auto-generated method stub

        super.onPostExecute(result);
        dialog.dismiss();
        BufferedReader reader = new BufferedReader(
                new InputStreamReader(is));
        StringBuffer buffer = new StringBuffer();
        String line = null;
        do {
            try {
                line = reader.readLine();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            buffer.append(line);
        } while (line != null);
        tv.setText(buffer);
    }

}

}

4

3 回答 3

2
   public class MainActivity extends Activity {
     TextView tv;
    Button b;
    InputStream is = null;

    DefaultHttpClient client;
    HttpGet get;
    HttpResponse response;
HttpEntity entity;
StringBuffer buffer;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //StrictMode.ThreadPolicy policy = new 
  //    StrictMode.setThreadPolicy(policy);
    tv = (TextView) findViewById(R.id.texty);
    b = (Button) findViewById(R.id.buttonGET);

    b.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            MyOperation mytask = new MyOperation();
            mytask.execute();
        }
    });

}

private class MyOperation extends AsyncTask<String, Void, String> {
    ProgressDialog dialog = new ProgressDialog(MainActivity.this);

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();
        dialog.setMessage("Loading...");
        dialog.show();
    }

    @Override
    protected String doInBackground(String... params) {
StringBuffer buffer = new StringBuffer();
        // TODO Auto-generated method stub
        client = new DefaultHttpClient();
        get = new HttpGet("http://www.google.com");
        try {
            response = client.execute(get);
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        entity = response.getEntity();

        try {
            is = entity.getContent();
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    BufferedReader reader = new BufferedReader(
                new InputStreamReader(is));

        String line = null;
        do {
            try {
                line = reader.readLine();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            buffer.append(line);
        } while (line != null);

        return buffer.tostring();
    }

    @Override
    protected void onPostExecute(String result) {
        // TODO Auto-generated method stub

        super.onPostExecute(result);
        dialog.dismiss();

        tv.setText(result);
    }}

}

否则你正在主线程中运行读取行操作

于 2013-03-21T10:32:36.133 回答
1

使 InputStreamis和 Entityentity局部字段变量doInBackground()作为结果返回您想要在 TextView 上设置的字符串......然后onPostExecute()您只需获得结果(字符串)并将其直接设置在 TextView 中。

于 2013-03-21T10:29:01.930 回答
1

您应该在 doInBackground 函数中读取 inputStream 数据,而不是在 onPostExecute 中。因此在 doInBackground 中构造 StringBuffer 将所有行附加到它并返回 StringBuffer.toString 作为结果。在 onPostExecute 你会得到什么字符串作为参数。

于 2013-03-21T10:32:58.150 回答