1

So I get that error when I try to parse the JSON Object. I have google the problem and I am still confused and can't find a solution to it. It looks like my code is correct.

This is the Android Java file

InputStream is = null;
String result = "";
protected String doInBackground(String... params) {
    try {

        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(params[0]);
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();


        is = entity.getContent();
        Log.v("mysql", "Success");
    } catch (Exception e) {
        Log.v("log_tag", "Error in http connection " + e.toString());
    }
    return null;
}

protected void onPostExecute(String result) {
    try{
        BufferedReader reader = new BufferedReader(new InputStreamReader(is,"UTF-8"));
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
        }
        is.close();

        result=sb.toString();
        Log.v("result", result);
}catch(Exception e){
        Log.e("log_tag", "Error converting result "+e.toString());
}

    try{
        JSONArray jArray = new JSONArray(result);
        for(int i=0;i<jArray.length();i++){
                JSONObject json_data = jArray.getJSONObject(i);
                Log.i("log_tag","uID: "+json_data.getInt("uID")+
                        ", uName: "+json_data.getString("uName")+
                        ", uPass: "+json_data.getString("uPass")
                );
        }
}catch(JSONException e){
        Log.e("log_tag", "Error parsing data "+e.toString());

    }

}

The code for PHP file is this

    <?php
$username = "removed";
$password = "removed";
$database = "removed";
$user = "art";
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die ("it's dead jim");

$q = mysql_query("SELECT * FROM user WHERE uName = '$user'");
while($e=mysql_fetch_assoc($q)){
        print(json_encode($e));
        $output[]=$e;
}

?>

The JSON value I receive is

{"uID":"1","uName":"art","uPass":"password"}

When I try to parse it I get:

Error parsing data org.json.JSONException: Value {"uID":"1","uName":"art","uPass":"password"} of type org.json.JSONObject cannot be converted to JSONArray

3
  • What don't you understand about the exception message? Do you know the JSON format? Commented Feb 27, 2014 at 1:58
  • I understand the error that it can't be converted but I don't understand exactly what I am doing wrong and how to fix it. I get that I am storing it into an JSONArray but what else can I store it in to parse it? Commented Feb 27, 2014 at 2:03
  • 1
    You're trying to parse it as a JSONArray when it is a JSONObject. Commented Feb 27, 2014 at 2:04

1 Answer 1

2

You data in the file is in JSONObject format and you are tryong to receive it in JSONArray format which is causing the problem. The line below is the culprit:

JSONArray jArray = new JSONArray(result);

Rather, you need to do something like this:

JSONObject jObject = null;
jObject = new JSONObject(mJsonString);
JSONArray jsonImageArray = jObject.getJSONArray("imageTarget");

Hope it helps.

Sign up to request clarification or add additional context in comments.

4 Comments

Is mJsonString equivalient to my result string? Also can you explain the "imageTarget". Finally after I code that do I just go ahead with the for loop I had?
Yes mJsonString is equivalent to "result" string. For imagetarget, I have attached my Json file. You can have a look at it : { "imageTarget": [ "image1" ], "videoUrls": [ "mylink.com/video1.mp4" ], "videoOrUrl": [ "video" ] }
Thanks. Just to make sure I am understanding this properly... For the JSON file you provided I would basically have jsonImageArray = jObject.getJSONArray("imageTarget"); followed by jsonImageArray = jObject.getJSONArray("videoUrls"); etc...?
yes exactly.. just the name of variable needs to be changed :) .. rather than "jsonImageArray" I used "jsonUrlArray" to avoid overwriing

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.