0

I used the link for JSON RPC. I am getting a response as expected. But when i try to parse the response, it's giving me json error.

My code:

JSONEntity entity = new JSONEntity(jsonRequest);
    HttpPost request = new HttpPost("http://192.168.1.150/jsondemo12/service.asmx");
    request.setEntity(entity);
    HttpResponse response = httpClient.execute(request);
    StatusLine statusLine = response.getStatusLine();
    int statusCode = statusLine.getStatusCode();
    if (statusCode == 200) {
        HttpEntity httpEntity = response.getEntity();
        InputStream content = httpEntity.getContent();

        BufferedReader reader = new BufferedReader(
                new InputStreamReader(content,"iso-8859-1"),8);
        String line;
        while ((line = reader.readLine()) != null) {
            builder.append(line);
        }
        content.close();
    } else {
        Log.e(AndroidJSONActivity.class.toString(), "Failed to download file");
    }


    strJSONValue=builder.toString();

    txtViewParsedValue.append("\n+++++++++++++\n"+strJSONValue+"\n");
    try {
        parseJSON();
    } catch (JSONException e) {
        Log.e("error","Error while parsing!!!");
        e.printStackTrace();
    }
    Log.e("response", strJSONValue);
public void parseJSON() throws JSONException
    {
        String attr1="",attr2="";
        jsonObject = new JSONObject(strJSONValue);
        JSONArray  result = jsonObject.getJSONArray("result");    <- Error in this line!!!
        for(int i=0;i < result.length();i++){
            JSONObject e = result.getJSONObject(i);
            attr1 = "ExhibitorID: "+ e.getString("ExhibitorID");
            attr2 = "ExhibitorName: "+e.getString("ExhibitorName");
        }
        strParsedValue=attr1+"\n"+attr2;
        Log.d("Parse", attr1);
        Log.d("Parse", attr2);

        txtViewParsedValue.append("\n**********\nParsed Value: \n");
        txtViewParsedValue.append(strParsedValue);
    }

The result i get in "strJSONValue" is string format, without the starting and ending double quotes. Like:

{"id":2,"result":"[
{\"ExhibitorID\":42, etc....}
]"}

The result string is as per requirement,but i am not able to parse the string into the JSON Object as per requirement. It gives error in Logcat: org.json.JSONException: Value <content of the string> at result of type java.lang.String cannot be converted to JSONArray

Please help me. Thanks

1 Answer 1

1

your result is actually returning a string not a json array. it will return json array if your json format would be like this

{
  "id": 2,
  "result": [
    {
      "ExhibitorID": 42
    }
  ]
}

currently it is in this form:

{
  "id": 2,
  "result": "[ {\"ExhibitorID\":42, etc....} ]"
}
Sign up to request clarification or add additional context in comments.

4 Comments

Send me your cv on my id.there is an opening for an android dev in interrait
so, i need to alter the string received then pass it ahead for parsing?
I think , In webservice when you are creating json array , you are converting it into string. don't convert it. changing received response is not a good habit.well For solution, you can take "result tag" in string first using getString and then covert the string in json object using new JSONObject(string); and after that parse it.
how do i take the result tag ?

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.