title: How to parse JSON with AsyncTask?
body: <p>I tried to parse some strings from this JSON-site:</p>
  
  <p><img src="https://i.stack.imgur.com/wRTWt.png" alt="Image of JSON table"></p>
  
  <p>I wrote this code with asynctask:</p>
  
  <pre><code>package com.example.nortti.jsonexample;
  
  import android.app.Activity;
  import android.app.ProgressDialog;
  import android.os.AsyncTask;
  import android.os.Bundle;
  import android.view.View;
  import android.widget.Button;
  import android.widget.TextView;
  import android.widget.Toast;
  
  import org.json.JSONArray;
  import org.json.JSONException;
  import org.json.JSONObject;
  
  public class MainActivity extends Activity {
      TextView txView;
  
  //URL to get JSON Array
  private static String url = "http://localhost:10101/api/stats/1";
  
  //JSON Node Names
  private static final String TAG_USER = "";
  private static final String TAG_NAME = "PersonName";
  private static final String TAG_EMAIL = "Rank";
  
  
  JSONArray user;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
  
      user = null;
              new JSONParse().execute();
  
  
  
  }
  
  private class JSONParse extends AsyncTask&lt;String, String, JSONObject&gt; {
      private ProgressDialog pDialog;
      @Override
      protected void onPreExecute() {
          super.onPreExecute();
  
          txView = (TextView)findViewById(R.id.txView);
  
          pDialog = new ProgressDialog(MainActivity.this);
          pDialog.setMessage("Getting Data ...");
          pDialog.setIndeterminate(false);
          pDialog.setCancelable(true);
          pDialog.show();
  
      }
  
      @Override
      protected JSONObject doInBackground(String... args) {
          JSONParser jParser = new JSONParser();
  
          // Getting JSON from URL
          JSONObject json = jParser.getJSONFromUrl(url);
          return json;
      }
      @Override
      protected void onPostExecute(JSONObject json) {
          pDialog.dismiss();
          try {
              // Getting JSON Array
              user = json.getJSONArray("ArrayOfCommonStatViewModel");
              Toast.makeText(getApplication(),user.toString(),Toast.LENGTH_SHORT).show();
              JSONObject c = user.getJSONObject(0);
  
              // Storing  JSON item in a Variable
  
              String name = c.getString(TAG_NAME);
              String email = c.getString(TAG_EMAIL);
  
              //Set JSON Data in TextView
  
              txView.setText(name+" " +email);
  
  
          } catch (JSONException e) {
              e.printStackTrace();
          }
  
      }
  }
  
  }
  </code></pre>
  
  <p>And I have another class like:</p>
  
  <pre><code>import android.util.Log;
  
  import org.apache.http.HttpEntity;
  import org.apache.http.HttpResponse;
  import org.apache.http.client.ClientProtocolException;
  import org.apache.http.client.methods.HttpGet;
  import org.apache.http.client.methods.HttpPost;
  import org.apache.http.impl.client.DefaultHttpClient;
  import org.json.JSONException;
  import org.json.JSONObject;
  
  import java.io.BufferedReader;
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.InputStreamReader;
  import java.io.UnsupportedEncodingException;
  
  public class JSONParser {
  
  static InputStream is = null;
  static JSONObject jObj = null;
  static String json = "";
  
  // constructor
  public JSONParser() {
  
  }
  
  public JSONObject getJSONFromUrl(String url) {
  
      // Making HTTP request
      try {
          // defaultHttpClient
          DefaultHttpClient httpClient = new DefaultHttpClient();
          HttpGet httpPost = new HttpGet(url);
  
          HttpResponse httpResponse = httpClient.execute(httpPost);
          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>But it says that I have a <code>NullPointerException</code></p>
  
  <p>I want to get a JSONArray to get information and put it into a <code>ListView</code>.</p>
  
  <p>UPD: Here is JSON screenshot <a href="https://i.stack.imgur.com/CGj7i.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/CGj7i.png" alt="JSON"></a></p>
  
  <p>UPD2: full log</p>
  
  <pre><code>12-17 14:17:03.712 20308-20308/com.example.nortti.politrange E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                 Process: com.example.nortti.politrange, PID: 20308
                                                                                 android.os.NetworkOnMainThreadException
                                                                                     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1147)
                                                                                     at java.net.InetAddress.lookupHostByName(InetAddress.java:418)
                                                                                     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
                                                                                     at java.net.InetAddress.getAllByName(InetAddress.java:215)
                                                                                     at org.apache.http.impl.conn.SystemDefaultDnsResolver.resolve(SystemDefaultDnsResolver.java:44)
                                                                                     at org.apache.http.impl.conn.HttpClientConnectionOperator.connect(HttpClientConnectionOperator.java:102)
                                                                                     at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:318)
                                                                                     at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:373)
                                                                                     at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:225)
                                                                                     at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:195)
                                                                                     at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:86)
                                                                                     at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:108)
                                                                                     at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:178)
                                                                                     at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
                                                                                     at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:106)
                                                                                     at com.example.nortti.politrange.utils.WebApiAdapter.select(WebApiAdapter.java:33)
                                                                                     at com.example.nortti.politrange.intefaces.impls.PersonCatalog.populateData(PersonCatalog.java:37)
                                                                                     at com.example.nortti.politrange.views.GeneralFragment.listData(GeneralFragment.java:65)
                                                                                     at com.example.nortti.politrange.views.GeneralFragment.onClick(GeneralFragment.java:88)
                                                                                     at android.view.View.performClick(View.java:4756)
                                                                                     at android.view.View$PerformClick.run(View.java:19761)
                                                                                     at android.os.Handler.handleCallback(Handler.java:739)
                                                                                     at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                     at android.os.Looper.loop(Looper.java:135)
                                                                                     at android.app.ActivityThread.main(ActivityThread.java:5253)
                                                                                     at java.lang.reflect.Method.invoke(Native Method)
                                                                                     at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:900)
                                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:695)
  </code></pre>