title: Android Strict Mode
body: <p>I have written a simple code on JSON Parsing using <code>AsyncTask</code>. I'm just displaying the response in <code>TextView</code>. I don't know whether it is right or wrong. Its  working on GingerBread and showing <code>NetworkOnMainThreadException</code> on JellyBean. If I use <code>StrictMode</code>, its working on JellyBean and getting force close on GingerBread. How to write the code which support above Android 3.0 and below Android 3.0.</p>
  
  <pre><code>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&lt;String, Void, String&gt; {
      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);
      }
  
  }
  </code></pre>
  
  <p>}</p>