0

I know this exception is asked a million times before and I saw various post saying different suggestion and nothing worked for me yet.

I need to get some data from a url using httpget am giving a specific id along my url. my output should looks likes this

{
"id": "35ZGXU7WQHFYY5BFTV6EACGEUS",
"createTime": "2014-04-11T12:52:26",
"updateTime": "2014-04-11T12:52:55",
"status": "Completed",
"transaction": {
    "amount": {
        "currencyCode": "GBP",
        "total": 7.47
    },
    "qrcode": "189e8dad99908745o7439f8ffabdfipp",
    "description": "This is the payment transaction description."
}
}

but due to something am getting this error

04-11 18:24:14.655: E/STATUS_ERR(30067): org.json.JSONException: Value com.mywallet.android.rest.MyWalletRestService$getStatus@41837fd0 of type java.lang.String cannot be converted to JSONObject

my code is as below

 String line = null;
        try{
            BufferedReader in = null;


            HttpGet request = new HttpGet();
            URI website = new URI(url);
            request.setURI(website);
            request.setHeader("Accept", "application/json");
            request.setHeader(AppConstants.PAYMENT_HEADER1, BEARER);
            request.setHeader(AppConstants.content_type, AppConstants.application_json);
            response = httpClient.execute(request);


            in = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"), 8);
            StringBuilder sb = new StringBuilder();

            while ((line = in.readLine()) != null)
            {
                sb.append(line + "\n");
            }

            result = sb.toString();
           JSONObject jObject = new JSONObject(toReturn);

            }

        if(jObject.has("error")){
            RES_STATUS      =   AppConstants.FAIL;
        }
        else{

             AppVariables.id                    =   jObject.getString(AppVariables.id_);
             AppVariables.createTime            =   jObject.getString(AppVariables.createTime_);
             AppVariables.updateTime            =   jObject.getString(AppVariables.updateTime_);
             AppVariables.status                =   jObject.getString(AppVariables.status_);             
             JSONObject oneObject               =   jObject.getJSONObject("transaction");
             JSONObject twoObject               =   oneObject.getJSONObject("amount");           
             AppVariables.total                 =   twoObject.getString("total");
             AppVariables.currencyCode          =   twoObject.getString("currencyCode");
             AppVariables.qrcode                =   oneObject.getString(AppVariables.qrcode_);
             AppVariables.description           =   oneObject.getString(AppVariables.description_);              

             MWLog.e("AppVariables.id", AppVariables.id);
             MWLog.e("AppVariables.createTime", AppVariables.createTime);
             MWLog.e("AppVariables.updateTime", AppVariables.updateTime);
             MWLog.e("AppVariables.status", AppVariables.status);
             MWLog.e("AppVariables.currencyCode", AppVariables.currencyCode);                
             MWLog.e("AppVariables.total", AppVariables.total);
             MWLog.e("AppVariables.qrcode", AppVariables.qrcode);
             MWLog.e("AppVariables.des", AppVariables.description);

             RES_STATUS                     =   AppConstants.SUCCESS;

             MWLog.e("BUYER", toReturn);
        }
suja
  • 1,198
  • 2
  • 12
  • 32

3 Answers3

0

Use this function to get proper json response

public String getResponseBody(final HttpEntity entity) throws IOException, ParseException {

    if (entity == null) {
        throw new IllegalArgumentException("HTTP entity may not be null");
    }

    InputStream instream = entity.getContent();

    if (instream == null) {
        return "";
    }

    if (entity.getContentLength() > Integer.MAX_VALUE) {
        throw new IllegalArgumentException(

        "HTTP entity too large to be buffered in memory");
    }

    StringBuilder buffer = new StringBuilder();

    BufferedReader reader = new BufferedReader(new InputStreamReader(instream, HTTP.UTF_8));

    String line = null;
    try {
        while ((line = reader.readLine()) != null) {
            buffer.append(line.trim());
        }

    } finally {
        instream.close();
        reader.close();
    }
    return buffer.toString().trim();

}

How to use?

result= getResponseBody(response.getEntity());
Biraj Zalavadia
  • 28,348
  • 10
  • 61
  • 77
0
       result = sb.toString();
       JSONObject jObject = new JSONObject(toReturn);

Shouldn't that be

       result = sb.toString();
       JSONObject jObject = new JSONObject(result);

You are trying to convert the String toReturn, which has invalid JSON data.

Udo Klimaschewski
  • 5,150
  • 1
  • 28
  • 41
0

Here Amount is a JsonArray and you are trying to cast it in json object

JSONArray JsonArray2 = jsonobject.getJSONArray("amount");

Try this way

Shravan
  • 540
  • 6
  • 24
  • this is a nested Json Array your Parent Json Array contain An inner Array with the name tRansaction And transaction Also contain a json Array with name Amount –  Apr 11 '14 at 13:43