0

i currently wrote a php script that is linked to my android application however i have a question : MY php script have 2 outputs:

  1. ["Success"]
  2. null

When output is null i want in my android application:

if (json.equals(null)) {
    goodrating.setVisibility(View.GONE);
    badrating.setVisibility(View.GONE);

if output is ["Success"] i want my android app to do :

    } else {
 goodrating.setVisibility(View.VISIBLE);
 badrating.setVisibility(View.VISIBLE);

        }

PROBLEM IS NOT SOLVED YET

UPDATE HERE'S MY FULL CODES THROUGH ASYNCTASK:

protected String doInBackground(String... params) {
if (fb.isSessionValid()) {
                    String resulta;
                    JSONObject obj = null;
                    String jsonUser = fb.request("me");
                    obj = Util.parseJson(jsonUser);
                    String email = obj.optString("email");

                    String phonename = prefs.getString("esmlphone", "value");
                    InputStream isr = null;
                    HttpClient httpclient = new DefaultHttpClient();
                    String tablename = prefs.getString("hazahuwakeylurl",
                            "walashi");
                    HttpPost httpost = new HttpPost(
                            "secret");
                    HttpResponse resposne = httpclient.execute(httpost);
                    HttpEntity entity = resposne.getEntity();
                    isr = entity.getContent();
                    BufferedReader reader = new BufferedReader(
                            new InputStreamReader(isr, "UTF-8"), 8);
                    StringBuilder sb = new StringBuilder();
                    String line = null;
                    while ((line = reader.readLine()) != null) {
                        sb.append(line + "\n");
                    }
                    isr.close();
                    resulta = sb.toString();
                    final JSONArray jArray = new JSONArray(resulta);
                    try {
                        for (int i = 0; i < jArray.length(); i++) {
                            JSONObject json = jArray.getJSONObject(0);
                            if (json.isNull("response")) {
                                goodrating.setVisibility(View.GONE);
                                badrating.setVisibility(View.GONE);
                            } else {
                                goodrating.setVisibility(View.VISIBLE);
                                badrating.setVisibility(View.VISIBLE);
                            }
                        }

                    } catch (Exception e) {
                        Log.d("visibility", e.toString());
                    }
                }

            return null;

        }

4 Answers 4

1

Problems

  1. "null" is not valid JSON, however, [null] is. Check http://jsonlint.com/
  2. According to your exception, you're converting a string to a JSON object. So I'm guessing jArray is simply an array of your response. You should be converting your response to a JSON array or object first.
  3. Once you have #2 all settled, http://developer.android.com/reference/org/json/JSONArray.html#isNull(int) is the proper way of checking whether a JSONObject in a JSONArray is null.
Sign up to request clarification or add additional context in comments.

1 Comment

can you give me an example in #2?
0

So what you'll want to do is this:

if(json.isNull("response"))
    goodrating.setVisibility(View.GONE);
    badrating.setVisibility(View.GONE);
} else if(json.getString("response").equals("Success")){
    goodrating.setVisibility(View.VISIBLE);
    badrating.setVisibility(View.VISIBLE);
}

2 Comments

Awesome, glad I could help! Do us all a favor though and mark this as an accepted answer if it solves your problem so everyone knows this question has been answered.
So then what exactly is the issue? I see the code update, but you're not saying anything else as to what is wrong...
0

Try if (json == null). That's usually how you check for null objects in Java.

edit: This is because most 'Objects' in Java are actually just references to the data structures themselves. You use == null check if the reference is null, and use .equals() to determine if an object is logically equivalent to another object.

One more try - it looks like there's a JsonValue.NULL constant in the JsonObject class. Try if (json == JsonValue.NULL)

Comments

0

So when you hit the website, is does it ONLY output ["Success"] or null? If that's the case, its not json what you're outputting. What you want to output is something like this:

{
    "response": "Success"
}

or

{
    "response": null
}

Only then will the JSON on the Android end will work properly

Comments

Your Answer

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