Skip to content Skip to sidebar Skip to footer

Org.json.jsonexception: No Value For Status

org.json.JSONException: No value for status Here is my java code method for json parse java public void performSearch() { String url= 'http://192.168.0.136/fyp/stitl

Solution 1:

We will take this step for step. Lets start with response.

Your PHP code is returning a status value =4 which indicates that you are not getting the parameters sent to the PHP code properly. It is possible that getParams() is not even being called.

Change the getParams() method to look like this:

@OverrideprotectedMap<String, String> getParams() throws AuthFailureError {
    Map<String, String> params = newHashMap<>();
    try{
        String s = searchtitle.getText().toString();
        Log.e("Volley request", "getParams called : " + s);
        params.put("Title", s);
    }
    catch(Exception ex){
        Log.e("Volley request ERROR", ex.getMessage());
    }
    return params;
}

For the second part, lets try to deal with the parsing code. Change the code to look like this:

Now regardless of how your php code responds, you will be getting a well formed JSONObject as a response which you can parse and react to it appropriately.

Change the onResponse() part of the code to look like this:

@OverridepublicvoidonResponse(JSONObject response) {
    // Log.d("Response", response.toString());try {
        //converting the string to json array objectif(response != null){
            if(!response.has("status"){
                Log.e(TAG, "Something went wrong -- no status key!");
                return;
            }
            else{
                int status = response.optInt("status", -1);
                if(status == 1){
                    //There could be quite a few books in this response...//...you might want to parse in an AsyncTask insteadparseJsonObject(response);
                }
                else{
                    String message = response.optString("message", "uups");
                    Log.e(TAG, "error message = " + message);
                    return;
                }
            }
        }
    }
    catch(Exception ex){
        Log.e(TAG, ex.getMessage());
    }
}

And now to parse the JSONObject:

Map<String, String> booksMap = newHashMap<>();

    privatevoidparseJsonObject(JSONObject jsonObject){
        try{
            if(jsonObject == null) return;
            //Not Available!String na = "NA"Log.i("test", " value : " + jsonObject.toString());
            if(jsonObject.has("books")){
                JSONArray array = jsonObject.getJSONArray("books");
                for(int i = 0; array.length(); i++){
                    JSONObject book = array.getJSONObject(i);
                    Iterator<String> it = book.keys();
                    while(it.hasNext()){
                        String key = it.next();
                        String value = book.optString(key, na);
                        booksMap.put(key, value);
                    }
                }
            }
        }
        catch(Exception ex){
            Log.e(TAG, ex.getMessage());
        }
    }

Post a Comment for "Org.json.jsonexception: No Value For Status"