0

i am getting response from data services after that to create JSON Array from string it showing exception, this is my code

response = client.execute(request);
// getJSONString(response);
entity = response.getEntity();
int length = (int) entity.getContentLength();
StringBuffer sb = new StringBuffer(length);
if (entity.getContentLength() != 0) {
   reader = new InputStreamReader(entity.getContent(), "UTF-8");

   String line = "";

   BufferedReader bufferedReader = new BufferedReader(reader);
   while((line = bufferedReader.readLine()) != null)
       result += line;
   reader.close();

   jar = new JSONArray(result);**this statement getting exception**
   for (int i = 0; i < jar.length(); i++) {
      try {
         JSONObject c = jar.getJSONObject(i);
         String vtype = c.getString("ChannelID");
         res.setText(""+vtype);
      } catch (JSONException e) {
        e.printStackTrace();
      }
   }
}

this is response in string this is my response in string result:

{
    "d":[{
    "__metadata":{
        "id":
        "XXX.XX.xX.XX:33396/FalconCPDataService.svc/DESystemOptions(guid'ca08834a-da4b-4bdd-9def-c91a7ea098b5')", "uri":
        "XXX.XX.xX.XX:33396/FalconCPDataService.svc/DESystemOptions(guid'ca08834a-da4b-4bdd-9def-c91a7ea098b5')", "type":
        "FalconCP_3_1Model.DESystemOption"
    },"DEOptionsLock":{
        "__deferred":{
            "uri":
            "XXX.XX.xX.XX:33396/FalconCPDataService.svc/DESystemOptions(guid'ca08834a-da4b-4bdd-9def-c91a7ea098b5')/DEOptionsLock"
        }
    },"ID":"ca08834a-da4b-4bdd-9def-c91a7ea098b5", "ApplicationType":0, "OptionsLocksID":
    "dd1f2e09-68c4-42ac-bfdf-afdb8d4bf631", "DefaultPulsewidth":230, "DefaultRate":40, "CreatedDate":
    "\/Date(1373465393870)\/", "IndicationType":1
    }]
}

at bold statement i am getting an exception. need help to solve this

6
  • 1
    what exception?paste logcat Commented Mar 18, 2014 at 13:16
  • 1
    show the format of your JSON; obviously your response is not JSONArray Commented Mar 18, 2014 at 13:20
  • In catch block it showing exception as a null Commented Mar 18, 2014 at 13:20
  • Just a question, why you instantiate StringBuffer sb = , and then, after you concat line to result variable. Where you define result ? try to replace result += line with sb.append(line); and then jar = new JSONArray(sb.toString()); Commented Mar 18, 2014 at 13:23
  • Json updated in question can u check once Commented Mar 18, 2014 at 13:23

1 Answer 1

1

try this:

JSONObject jo = new JSONObject(result)
jar = jo.getJSONArray("d");
Sign up to request clarification or add additional context in comments.

3 Comments

same exception getting at new JsonObject(result);
you are welcome :-) and pay attention for the next iteration, because I don't see any key named "ChannelID" in the json you posted. Have a good day.
i did changes in that.thank you and you to have a great day @LuS

Your Answer

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