其他分享
首页 > 其他分享> > Android严格模式

Android严格模式

作者:互联网

我已经使用AsyncTask在JSON解析上编写了一个简单的代码.我只是在TextView中显示响应.我不知道这是对还是错.它在GingerBread上工作,并在JellyBean上显示NetworkOnMainThreadException.如果我使用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);
    }

}

}

解决方法:

   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);
    }}

}

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

标签:android-asynctask,android-strictmode,android
来源: https://codeday.me/bug/20191031/1973205.html