HASNI Faiçal HASNI Faiçal - 3 months ago 14
Java Question

reading JSON via webservice android

I try to develop an Android application web service to return a json file, for example:


{"success":1,"message":"Post
Available!","posts":[{"post_id":"1","username":"hasni","title":"titre","message":"this
is my message"},{"post_id":"2","username":"user2","title":"titre
2","message":"this is my message
2"},{"post_id":"3","username":"123","title":"12","message":"111"},{"post_id":"4","username":"1212","title":"1212","message":"1212"},{"post_id":"5","username":"1212","title":"bonjour","message":"voila
ce message qui vient d'une session
"},{"post_id":"6","username":"121212","title":"titre","message":"message"}]}


This is the part of the code to read this JSON:

// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("receiver", strSender));
JSONObject json = jsonParser.makeHttpRequest(
LOGIN_URL, "POST", params);
success = json.getInt(TAG_SUCCESS);

Log.d("ok", "ok");

if (success == 1){

messagesArray = (JSONArray)json.getJSONArray(TAG_POSTS);
for (int i= 0;i <messagesArray.length();i++){
Log.d("iteration", "iteration" + i);
JSONObject messageJson = messagesArray.getJSONObject(i);
Log.d("post_id","post_id: "+i+ " "+ messageJson.getString(TAG_POSTS_ID) );
Log.d("username","username: " +i+" " + messageJson.getString(TAG_POSTS_USERNAME));
Log.d("title", "title: " +i+ " " + messageJson.getString(TAG_POSTS_ID_TITLE));
Log.d("message","message: "+i+" "+ messageJson.getString(TAG_POSTS_ID_MESSAGE));
}

}else{
Log.d("failed", "!!!!!!!!!!!!!!!!!!!!!!!!");
}


But I have a problem: the apps crash and exit. This is the logcat:


09-27 23:15:37.119: W/System.err(489): org.json.JSONException: No
value for posts 09-27 23:15:37.119: W/System.err(489): at
org.json.JSONObject.get(JSONObject.java:354) 09-27 23:15:37.128:
W/System.err(489): at
org.json.JSONObject.getJSONArray(JSONObject.java:544) 09-27
23:15:37.128: W/System.err(489): at
com.example.mysql.ReadMessages.onCreate(ReadMessages.java:73) 09-27
23:15:37.128: W/System.err(489): at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-27 23:15:37.128: W/System.err(489): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
09-27 23:15:37.128: W/System.err(489): at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
09-27 23:15:37.128: W/System.err(489): at
android.app.ActivityThread.access$1500(ActivityThread.java:117) 09-27
23:15:37.128: W/System.err(489): at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
09-27 23:15:37.138: W/System.err(489): at
android.os.Handler.dispatchMessage(Handler.java:99) 09-27
23:15:37.138: W/System.err(489): at
android.os.Looper.loop(Looper.java:123) 09-27 23:15:37.138:
W/System.err(489): at
android.app.ActivityThread.main(ActivityThread.java:3683) 09-27
23:15:37.138: W/System.err(489): at
java.lang.reflect.Method.invokeNative(Native Method) 09-27
23:15:37.138: W/System.err(489): at
java.lang.reflect.Method.invoke(Method.java:507) 09-27 23:15:37.138:
W/System.err(489): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
09-27 23:15:37.138: W/System.err(489): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 09-27
23:15:37.138: W/System.err(489): at
dalvik.system.NativeStart.main(Native Method)

Answer

I tried your code this way,

    private void jsonTest() {
    try {
        String jsonString = "{\"success\":1,\"message\":\"Post Available!\",\"posts\":[{\"post_id\":\"1\",\"username\":\"hasni\",\"title\":\"titre\",\"message\":\"this is my message\"},{\"post_id\":\"2\",\"username\":\"user2\",\"title\":\"titre 2\",\"message\":\"this is my message 2\"},{\"post_id\":\"3\",\"username\":\"123\",\"title\":\"12\",\"message\":\"111\"},{\"post_id\":\"4\",\"username\":\"1212\",\"title\":\"1212\",\"message\":\"1212\"},{\"post_id\":\"5\",\"username\":\"1212\",\"title\":\"bonjour\",\"message\":\"voila ce message qui vient d'une session \"},{\"post_id\":\"6\",\"username\":\"121212\",\"title\":\"titre\",\"message\":\"message\"}]}";

        Log.i(TAG, "jsonString = " + jsonString);
        JSONObject json = new JSONObject(jsonString);
        int success = json.getInt("success");

        Log.d("ok", "ok");

        if (success == 1) {

            JSONArray messagesArray = (JSONArray) json
                    .getJSONArray("posts");
            for (int i = 0; i < messagesArray.length(); i++) {
                Log.d("iteration", "iteration" + i);
                JSONObject messageJson = messagesArray.getJSONObject(i);
                Log.d("post_id",
                        "post_id: " + i + " "
                                + messageJson.getString("post_id"));
                Log.d("username",
                        "username: " + i + " "
                                + messageJson.getString("username"));
                Log.d("title",
                        "title: " + i + " "
                                + messageJson.getString("title"));
                Log.d("message",
                        "message: " + i + " "
                                + messageJson.getString("message"));
            }

        } else {
            Log.d("failed", "!!!!!!!!!!!!!!!!!!!!!!!!");
        }
    } catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();
    }
}

And worked properly, may be something wrong with your TAGs that you have defined.