NetworkOnMainThereadException when activating a new activity from MainActivity to display details about a list item
<p>Whenever I run and click on a list item in my <strong>MainActivity</strong> to initiate <strong>DetailsActivity</strong> that is suppose to diaply a little more info about the selected list item(name, description) LogCat throws the following at me:</p>

<blockquote>
  <p>E/AndroidRuntime(678):    at
  com.example.myfirstapp.JSONParser.makeHttpRequest(JSONParser.java:61)
  E/AndroidRuntime(678):    at
  com.example.myfirstapp.DetailsActivity$GetProductDetails$1.run(DetailsActivity.java:93)</p>
</blockquote>

<p>Line 61(JSONParser): <code>HttpResponse httpResponse = httpClient.execute(httpGet);</code>
Line 93(DetailsActivity): <code>JSONObject json = jsonParser.makeHttpRequest(
                                url_car_details, "GET", params);</code></p>

<p><strong>DetailsActivity</strong>:</p>

<pre><code>package com.example.myfirstapp;

/*
*   ...imports...
*/

public class JSONParser {

    static InputStream is = null;
    static JSONObject jObj = null;
    static String json = "";

    // constructor
    public JSONParser() {

    }

    // function get json from url
    // by making HTTP POST or GET mehtod
    public JSONObject makeHttpRequest(String url, String method, List&lt;NameValuePair&gt; params) {

        // Making HTTP request
        try {

            // check for request method
            if(method == "POST"){
                // request method is POST
                // defaultHttpClient
                DefaultHttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(url);
                httpPost.setEntity(new UrlEncodedFormEntity(params));

                HttpResponse httpResponse = httpClient.execute(httpPost);
                HttpEntity httpEntity = httpResponse.getEntity();
                is = httpEntity.getContent();

            }else if(method == "GET"){
                // request method is GET
                DefaultHttpClient httpClient = new DefaultHttpClient();
                String paramString = URLEncodedUtils.format(params, "utf-8");
                url += "?" + paramString;
                HttpGet httpGet = new HttpGet(url);

                HttpResponse httpResponse = httpClient.execute(httpGet);
                HttpEntity httpEntity = httpResponse.getEntity();
                is = httpEntity.getContent();
            }           

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            json = sb.toString();
        } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }

        // try parse the string to a JSON object
        try {
            jObj = new JSONObject(json);
        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }

        // return JSON String
        return jObj;

    }
}
</code></pre>

<p><strong>JSONParser</strong>:</p>

<pre><code>package com.example.myfirstapp;

/*
*   ...imports...
*/

public class DetailsActivity extends Activity {

    TextView lblTitle;
    TextView lblDesc;

    String id;

    // Progress Dialog
    private ProgressDialog pDialog;

    // JSON parser class
    JSONParser jsonParser = new JSONParser();

    // single product url
    private static final String url_car_details = "http://10.0.2.2/webservice/get_car_details.php";

    // JSON Node names
    private static final String TAG_SUCCESS = "success";
    private static final String TAG_CAR = "car";
    private static final String TAG_ID = "id";
    private static final String TAG_NAME = "name";
    private static final String TAG_DESC = "description";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_details);

        // getting product details from intent
        Intent i = getIntent();

        // getting product id (pid) from intent
        id = i.getStringExtra(TAG_ID);

        // Getting complete product details in background thread
        new GetProductDetails().execute();
    }

    /**
     * Background Async Task to Get complete product details
     * */
    class GetProductDetails extends AsyncTask&lt;String, String, String&gt; {

        /**
         * Before starting background thread Show Progress Dialog
         * */
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(DetailsActivity.this);
            pDialog.setMessage("Loading car details. Please wait...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }

        /**
         * Getting product details in background thread
         * */
        protected String doInBackground(String... params) {

            // updating UI from Background Thread
            runOnUiThread(new Runnable() {
                public void run() {
                    // Check for success tag
                    int success;
                    try {
                        // Building Parameters
                        List&lt;NameValuePair&gt; params = new ArrayList&lt;NameValuePair&gt;();
                        params.add(new BasicNameValuePair("id", id));

                        // getting product details by making HTTP request
                        // Note that product details url will use GET request
                        JSONObject json = jsonParser.makeHttpRequest(
                                url_car_details, "GET", params);

                        // check your log for json response
                        Log.d("Single Car Details", json.toString());

                        // json success tag
                        success = json.getInt(TAG_SUCCESS);
                        if (success == 1) {
                            // successfully received product details
                            JSONArray productObj = json
                                    .getJSONArray(TAG_CAR); // JSON Array

                            // get first product object from JSON Array
                            JSONObject product = productObj.getJSONObject(0);

                            // product with this pid found
                            // Edit Text
                            lblTitle = (TextView) findViewById(R.id.lblTitle);
                            lblDesc = (TextView) findViewById(R.id.lblDesc);

                            // display product data in EditText
                            lblTitle.setText(product.getString(TAG_NAME));
                            lblDesc.setText(product.getString(TAG_DESCRIPTION));

                        }else{
                            // product with pid not found
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            });

            return null;
        }

        /**
         * After completing background task Dismiss the progress dialog
         * **/
        protected void onPostExecute(String file_url) {
            // dismiss the dialog once got all details
            pDialog.dismiss();
        }
    }
}
</code></pre>
