Mr Robot
Mr Robot

Reputation: 1747

Cant display JSON array , JSONException: No value Error

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;

    }
}

Upvotes: 1

Views: 1897

Answers (4)

Madhur
Madhur

Reputation: 3353

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

Upvotes: 3

Anuron
Anuron

Reputation: 66

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);
    }
}

Upvotes: 1

Ravi
Ravi

Reputation: 35569

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

Upvotes: 1

RobinHood
RobinHood

Reputation: 10969

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

Upvotes: 1

Related Questions