Werdli
Werdli

Reputation: 335

Value 0 of type Java.lang.Integer cannot be converted to JSONObject

I'm trying to execute this Code for my App:

JSONObject json = jsonParser.makeHttpRequest(url_kickit_connect,
                    "POST", params);

            // check log cat for response
            Log.d("Create Response", json.toString());

            try {
                int response = json.getInt(TAG_RESPONSE);
                if (response == 0){
                Toast.makeText(getApplicationContext(), "email " +mail , Toast.LENGTH_SHORT).show();
                }

                }

                catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

While getting a 0 as return value, the 0 should be parsed as jsonobject within this code (above I do the standard Httppost and Stringbuilder):

try {
            jObj = new JSONObject(json);
        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }

The JSON value I get from my MySQL side(.php):

else
{
    $response = 0;
    echo json_encode($response);
}

But I get this error in Logcat:

02-26 09:24:14.920: E/JSON Parser(619): Error parsing data org.json.JSONException: Value 0 of type java.lang.Integer cannot be converted to JSONObject

If I try to change the value, or the data type to string etc. The error message just changes to the told value or data type. How do I solve this?

Upvotes: 1

Views: 9822

Answers (2)

To send the response as a JSON, add this on the php/mysql side

else {
    //creates an array
    $result = array();
    //adds a json node for response
    $result['response'] = 0;
    //encode and echo out
    echo json_encode($result);
}

Upvotes: -1

darja
darja

Reputation: 5025

0 is not a valid json. Server should return something like {"response": 0} for this case

Upvotes: 3

Related Questions