0

When ever i run this JSON Parsing code . my app crashes. im getting a null value. please help me , how to resolve this problem.

Here My response is different.

My JSON Response

[{"id":"28","image_url":"http:\/\/localhost\/assets\/gallery-images\/320cdfa72f-images.jpg"},
    {"id":"27","image_url":"http:\/\/localhost\/assets\/gallery-images\/af89bf9f94-images.jpg"},
    {"id":"26","image_url":"http:\/\/localhost\/assets\/gallery-images\/97e28042a9-images.jpg"},
    {"id":"25","image_url":"http:\/\/localhost\/assets\/gallery-images\/ffe626bf48-images.jpg"},
    {"id":"21","image_url":"http:\/\/localhost\/assets\/gallery-images\/96b210ed39-images.jpg"},
    {"id":"20","image_url":"http:\/\/localhost\/assets\/gallery-images\/96b210ed39-images.jpg"}]

My mainactivity

public class MainActivity extends Activity {
Button Btngetdata;
 //URL to get JSON Array
    private static String url = "http://zama.in/miradmin/api/index.php?action=get_images";

    //JSON Node Names
//    private static final String TAG_USER = "user";
    private static final String TAG_ID = "status";
    private static final String TAG_NAME = "code";
    private static final String TAG_EMAIL = "message";

    JSONArray user = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        Btngetdata = (Button) findViewById(R.id.getdata);
        Btngetdata.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                new JSONParse().execute();

            }
        });

    }

    private class JSONParse extends AsyncTask<String, String, JSONObject> {
        private ProgressDialog pDialog;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            uid = (TextView) findViewById(R.id.uid);
            name1 = (TextView) findViewById(R.id.name);
            email1 = (TextView) findViewById(R.id.email);
            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);
            Log.e("MyActivity", String.valueOf(json));
            return json;
        }

        @Override
        protected void onPostExecute(JSONObject json) {
            pDialog.dismiss();
            try {
                // Getting JSON Array
                JSONArray user = json.getJSONArray("Employee");
                Log.e("JSON ARRAY", String.valueOf(user));
//                JSONObject c = user.getJSONObject(0);
   email1.setText((CharSequence) user);
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }
    }

}

Logcat Error

             E/JSON Parser: Error parsing data org.json.JSONException: Value 
       [{"id":"28","image_url":"http:\/\/localhost\/assets\/gallery-images\/320cdfa72f-images.jpg"},
        {"id":"27","image_url":"http:\/\/localhost\/assets\/gallery-images\/af89bf9f94-images.jpg"},
        {"id":"26","image_url":"http:\/\/localhost\/assets\/gallery-images\/97e28042a9-images.jpg"},
        {"id":"25","image_url":"http:\/\/localhost\/assets\/gallery-images\/ffe626bf48-images.jpg"},
        {"id":"21","image_url":"http:\/\/localhost\/assets\/gallery-images\/96b210ed39-images.jpg"},
        {"id":"20","image_url":"http:\/\/localhost\/assets\/gallery-images\/96b210ed39-images.jpg"}]

 of type org.json.JSONArray cannot be converted to JSONObject.
E/MyActivity: null
E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                     Process: com.example.root5solutions.getanother, PID: 2078
                                                                                     java.lang.NullPointerException: Attempt to invoke virtual method 'org.json.JSONArray org.json.JSONObject.getJSONArray(java.lang.String)' on a null object reference
                                                                                         at com.example.root5solutions.getanother.MainActivity$JSONParse.onPostExecute(MainActivity.java:82)
                                                                                         at com.example.root5solutions.getanother.MainActivity$JSONParse.onPostExecute(MainActivity.java:50)
                                                                                         at android.os.AsyncTask.finish(AsyncTask.java:636)
                                                                                         at android.os.AsyncTask.access$500(AsyncTask.java:177)
                                                                                         at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:653)
                                                                                         at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                         at android.os.Looper.loop(Looper.java:135)
                                                                                         at android.app.ActivityThread.main(ActivityThread.java:5254)
                                                                                         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:903)
                                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
4
  • you have to return your response in JSONObject instead of JSONArray ... Commented Jan 6, 2016 at 9:09
  • @Saif : please help me , how to change Commented Jan 6, 2016 at 9:13
  • change your response Commented Jan 6, 2016 at 9:13
  • 1
    means like this your response return like this {"data" :[{"id":"28","image_url":"http:\/\/localhost\/assets\/gallery-images\/320cdfa72f-images.jpg"}, {"id":"27","image_url":"http:\/\/localhost\/assets\/gallery-images\/af89bf9f94-images.jpg"}, {"id":"26","image_url":"http:\/\/localhost\/assets\/gallery-images\/97e28042a9-images.jpg"} }]} Commented Jan 6, 2016 at 9:14

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.