title: Parsing simple JSON to android
body: <h2>Having problem parsing JSON data into android</h2>
  
  <ul>
  <li>I have descriptively mentioned the problem i am facing, Any ideas on
  how to overcome this</li>
  </ul>
  
  <p>Initially I have used the JSON from url</p>
  
  <p><strong><code>URL:: http://54.218.73.244:8084/</code></strong></p>
  
  <p><strong>JSONParser.java</strong></p>
  
  <pre><code>public class JSONParser {
  
      static InputStream is = null;
      static JSONArray jObj = null;
      static String json = "";
  
      // constructor
      public JSONParser() {
  
      }
  
      public JSONArray getJSONFromUrl(String url) {
  
          // Making HTTP request
          try {
              // defaultHttpClient
              DefaultHttpClient httpClient = new DefaultHttpClient();
              HttpPost httpPost = new HttpPost(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 JSONArray(json);
          } catch (JSONException e) {
              Log.e("JSON Parser", "Error parsing data " + e.toString());
          }
  
          // return JSON String
          return jObj;
  
      }
  }
  </code></pre>
  
  <p><strong>AndroidJSONParsingActivity.java</strong></p>
  
  <pre><code>    public class AndroidJSONParsingActivity extends Activity {
  
          // url to make request
          private static String url = "http://54.218.73.244:8084/";
          private HashMap&lt;Integer, String&gt; contentsMap = new HashMap&lt;Integer, String&gt;();
  
  
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.on
  
  Create(savedInstanceState);
          setContentView(R.layout.activity_main);
              // Creating JSON Parser instance
              JSONParser jParser = new JSONParser();
  
              // getting JSON string from URL
              JSONArray json = jParser.getJSONFromUrl(url);
  
              try {
                  for (int i = 0; i &lt; json.length(); i++) {
                      JSONObject c = json.getJSONObject(i);
  
                      // Storing each json item in variable
                      int id = c.getInt("id");
                      String name = c.getString("content");
  
                      contentsMap.put(id, name);
                  }
              } catch (JSONException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
          }
  
      @Override
      protected void onResume() {
          super.onResume();
  
          TextView textView=(TextView) findViewById(R.id.textView1);
          textView.setText(contentsMap.get(1));
  
          textView=(TextView) findViewById(R.id.textView2);
          textView.setText(contentsMap.get(2));
  
  
      }
  }
  </code></pre>
  
  <p><strong>activity_main.xml</strong></p>
  
  <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:orientation="vertical" 
      android:padding="10sp"&gt;
  
      &lt;TextView
          android:id="@+id/textView1"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:gravity="center"
          android:padding="10sp"
          android:text="TextView1" /&gt;
  
      &lt;TextView
          android:id="@+id/textView2"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:gravity="center"
          android:padding="10sp"
          android:text="TextView2" /&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <hr>
  
  <p><strong>OUTPUT::</strong></p>
  
  <p><em>The output for above code obtained is</em></p>
  
  <p><img src="https://i.stack.imgur.com/YUO35.png" alt="FIG-1"></p>
  
  <hr>
  
  <p>Next i change my URL to </p>
  
  <p><strong><code>URL:: http://54.218.73.244:7003/</code></strong></p>
  
  <p>And without changing other part of the code</p>
  
  <p>Now i get the error screen as</p>
  
  <p><img src="https://i.stack.imgur.com/zcjO2.png" alt="FIG-2"></p>
  
  <hr>
  
  <p><strong>ERROR LOG</strong></p>
  
  <pre><code>08-10 09:45:41.731: E/JSON Parser(448): Error parsing data org.json.JSONException: Value Cannot of type java.lang.String cannot be converted to JSONArray
  08-10 09:45:41.737: D/AndroidRuntime(448): Shutting down VM
  08-10 09:45:41.737: W/dalvikvm(448): threadid=1: thread exiting with uncaught exception (group=0x40015560)
  08-10 09:45:41.772: E/AndroidRuntime(448): FATAL EXCEPTION: main
  08-10 09:45:41.772: E/AndroidRuntime(448): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.json_web/com.example.json_web.AndroidJSONParsingActivity}: java.lang.NullPointerException
  08-10 09:45:41.772: E/AndroidRuntime(448):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
  08-10 09:45:41.772: E/AndroidRuntime(448):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
  08-10 09:45:41.772: E/AndroidRuntime(448):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
  08-10 09:45:41.772: E/AndroidRuntime(448):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
  </code></pre>
  
  <p><strong>My analysis according to error ::</strong> </p>
  
  <ul>
  <li>there is error parsing String cannot be converted to JSONArray</li>
  <li>How to solve this error ?</li>
  </ul>
  
  <hr>
  
  <ul>
  <li>for URL :: <code>http://54.218.73.244:8084/</code></li>
  </ul>
  
  <p>JSON reply i get is ::   <code>[{"id":1,"content":"Hello"},{"id":2,"content":"World"}]</code></p>
  
  <ul>
  <li>for URL :: <code>http://54.218.73.244:7003/</code></li>
  </ul>
  
  <p>JSON reply i get is :: <code>[{"id":1,"content":"Hello"},{"id":2,"content":"World"}]</code></p>
  
  <p><em>Structure of the JSON is same, Please confirm this by using the url in the browser</em></p>
  
  <hr>
  
  <p><strong>Thanks,</strong></p>
  
  <hr>