2

I am getting this JSON object from a PHP file on internet:

{
"1": "bar",
"2": "foo",
"3": "umIar",
"4": "shubham"
}

I tried a lot but was unable to convert it to a String array. How to do it?

I used this JAVA code :

public class showNewsFeed extends AsyncTask <String, Void, String> {
        HttpClient httpclient = new DefaultHttpClient();

         HttpPost httppost = new HttpPost("http://www.ludlowcastle.co.in/getNotifiedDTU/newsFeed.php");
         String result;
         protected String doInBackground(String... voids ){
             try {
                 HttpResponse response = httpclient.execute(httppost);
                 HttpEntity entity = response.getEntity();
                 InputStream is = entity.getContent();
                 String result = convertStreamToString(is);
                 System.out.println("String response = " + result);
             } catch (Exception e){
                 e.printStackTrace();
             }

             return result;
         }
        protected void onProgressUpdate(Void...voids){

        }


        protected void onPostExecute(String result){
            super.onPostExecute(result);

            try {
                JSONObject obj=new JSONObject(result);
                Iterator iter = obj.keys();
                 while(iter.hasNext()){
                   String key = (String)iter.next();
                   System.out.println(obj.getString(key));
                }

              } catch (Exception e) {
                e.printStackTrace();
              }
        }



    }

This is the logcat :

02-18 04:58:19.412: W/System.err(1268): java.lang.NullPointerException
02-18 04:58:19.431: W/System.err(1268):     at org.json.JSONTokener.nextCleanInternal(JSONTokener.java:116)
02-18 04:58:19.431: W/System.err(1268):     at org.json.JSONTokener.nextValue(JSONTokener.java:94)
02-18 04:58:19.431: W/System.err(1268):     at org.json.JSONObject.<init>(JSONObject.java:154)
02-18 04:58:19.431: W/System.err(1268):     at org.json.JSONObject.<init>(JSONObject.java:171)
02-18 04:58:19.431: W/System.err(1268):     at com.umair.shubham.fragment1$showNewsFeed.onPostExecute(fragment1.java:71)
02-18 04:58:19.431: W/System.err(1268):     at com.umair.shubham.fragment1$showNewsFeed.onPostExecute(fragment1.java:1)
02-18 04:58:19.441: W/System.err(1268):     at android.os.AsyncTask.finish(AsyncTask.java:631)
02-18 04:58:19.441: W/System.err(1268):     at android.os.AsyncTask.access$600(AsyncTask.java:177)
02-18 04:58:19.441: W/System.err(1268):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
02-18 04:58:19.451: W/System.err(1268):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-18 04:58:19.451: W/System.err(1268):     at android.os.Looper.loop(Looper.java:137)
02-18 04:58:19.451: W/System.err(1268):     at android.app.ActivityThread.main(ActivityThread.java:5039)
02-18 04:58:19.451: W/System.err(1268):     at java.lang.reflect.Method.invokeNative(Native Method)
02-18 04:58:19.451: W/System.err(1268):     at java.lang.reflect.Method.invoke(Method.java:511)
02-18 04:58:19.451: W/System.err(1268):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-18 04:58:19.461: W/System.err(1268):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-18 04:58:19.471: W/System.err(1268):     at dalvik.system.NativeStart.main(Native Method)
3
  • Possible duplicate of: {1}, {2}, {3}, and numerous others. Commented Feb 18, 2013 at 4:45
  • 1
    add this and this too Commented Feb 18, 2013 at 4:49
  • just replace this line String result = convertStreamToString(is); with result = convertStreamToString(is); in do in background Commented Feb 18, 2013 at 5:05

1 Answer 1

1

Try this

ArrayList<String> arr=new ArrayList<String>();
JSONObject obj=new JSONObject(response);
Iterator iter = obj.keys();
 while(iter.hasNext()){
   String key = (String)iter.next();
   arr.add(obj.getString(key));
}
Sign up to request clarification or add additional context in comments.

1 Comment

which line of code is there in line number 71

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.