1

Im trying to parse JSON, i am able to receive response {"status":false,"code":"101","message":"Cannot find a POST request in register"} by Log.e("JSON Object", String.valueOf(json));After that im getting an JSONException: No value for user . please anyone help me to resolve this problem. and i already checked tutorials from internet and other questions in stackoverflow. But still i cant resolve my problem.

My JSON Response

{"status":false,"code":"101","message":"Cannot find a POST request in register"}

LogCat Error

W/System.err: org.json.JSONException: No value for user   

My JSON Response parsing code:

       //URL to get JSON Array
       private static String url = "http://mywebsite/api/index.php?action=user_register";

        //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;

    // Creating new JSON Parser
        JSONParser jParser = new JSONParser();

        // Getting JSON from URL
        JSONObject json = jParser.getJSONFromUrl(url);

 try {
            // Getting JSON Array
            user = json.getJSONArray(TAG_USER);
            JSONObject c = user.getJSONObject(0);

            // Storing  JSON item in a Variable
            String id = c.getString(TAG_ID);
            String name = c.getString(TAG_NAME);
            String email = c.getString(TAG_EMAIL);

            Toast.makeText(getContext(), id , Toast.LENGTH_SHORT).show();
            Toast.makeText(getContext(), name , Toast.LENGTH_SHORT).show();
            Toast.makeText(getContext(), email , Toast.LENGTH_SHORT).show();
    } catch (JSONException e) {
            e.printStackTrace();
        }

    }

JSONParser.java

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

public class JSONParser {

    static InputStream is = null;
    static JSONObject jObj = null;
    static String json = "";

    // constructor
    public JSONParser() {

    }

    public JSONObject 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 JSONObject(json);
        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }

        // return JSON String
        return jObj;

    }
}
2
  • there is no user tag in your response. Commented Dec 30, 2015 at 6:21
  • 1
    try use if(json.has(TAG_USER)) user = json.getJSONArray(TAG_USER); Commented Dec 30, 2015 at 6:23

4 Answers 4

3

try to use

if(json.has(TAG_USER))
   user = json.getJSONArray(TAG_USER);

So if there is no such tag u wont get error

UPDATED:

//Storing  JSON item in a Variable
String id = json.getString(TAG_ID);
String name = json.getString(TAG_NAME);
String email = json.getString(TAG_EMAIL);
Sign up to request clarification or add additional context in comments.

2 Comments

i tried it but it shows java.lang.NullPointerException: Attempt to invoke virtual method 'org.json.JSONObject org.json.JSONArray.getJSONObject(int)' on a null object reference
check the updated ans,As per your json there is no jsonarray, you are getting a json object as a response
1

It because there is no user tag in your response and you are trying to fetch the same.

Your response:

{"status":false,"code":"101","message":"Cannot find a POST request in register"}

and you are doing: user = json.getJSONArray(TAG_USER);, here user tag is missing which throw error.

W/System.err: org.json.JSONException: No value for user

1 Comment

So . how will i solve this error . i need to display status ,code and message. can u please help me.
1

you are not getting user in your response

{"status":false,"code":"101","message":"Cannot find a POST request in register"}

That is the reason you are getting JSONException: No value Error

try with this

if(json.has(TAG_USER))
{
   user = json.getJSONArray(TAG_USER);
   JSONObject c = user.getJSONObject(0);

   // Storing  JSON item in a Variable
   String id = c.getString(TAG_ID);
   String name = c.getString(TAG_NAME);
   String email = c.getString(TAG_EMAIL);


}

It will fetch value for user if it is available in your response

Comments

1

In stead of using 'getJSONMethods' use 'optJSONMethods' if there is a chance of missing tags.

'optMethods' don't throw exceptions. It returns some default values depending on types. Null for type JSONArray.

For your case you can use:

user = json.optJSONArray(TAG_USER);

if(null != user) {
    JSONObject c = user.optJSONObject(0);

    // Storing  JSON item in a Variable
    if(null != c) {
        String id = c.optString(TAG_ID);
        String name = c.optString(TAG_NAME);
        String email = c.optString(TAG_EMAIL);
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.