1
OData
서비스에 액세스하기위한 HTTP 요청을 생성 중입니다. 나는 응답을 얻고 있지만 문자열 객체로 응답을 구문 분석하는 방법을 모르므로 ArrayList
에 추가 할 수 있습니다. 나는이 응답을Java에서 oData 응답을 구문 분석
protected List<StatusResponse> doInBackground(Void... params)
{
// TODO Auto-generated method stub
// Execute HTTP Post Request
mResponseList = new ArrayList<StatusResponse>();
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(myOdataQueryUrl);
try
{
HttpResponse responsenext = httpclient.execute(httpget);
HttpEntity entitynext = responsenext.getEntity();
AddedResult= EntityUtils.toString(entitynext);
jsonArray = new JSONArray(AddedResult);
for (int i = 0; i < jsonArray.length(); i++)
{
JSONObject menuObject = jsonArray.getJSONObject(i);
String createdBy = menuObject.getString("CreatedBy");
String comment = menuObject.getString("Comment");
String location = menuObject.getString("Location");
String slot = menuObject.getString("Slot");
String reachingAt = menuObject.getString("StartTime");
String lunch = menuObject.getString("Lunch");
mResponseList.add(new StatusResponse(createdBy, comment, location, slot, reachingAt, lunch));
}
}
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return mResponseList;
}
: 여기
내 코드입니다 -{
"odata.metadata":"mysite/odata/$metadata#OStatus","value":[
{
"StatusId":2151,"Location":"Office","Slot":"Running late","StartTime":"2014-09-29T12:30:00","Comment":"-","Lunch":null,"CreatedBy":"","ModifiedBy":null,"Created":"2014-09-29T04:39:10.443","Modified":null
}
]
}
내가 위에서처럼 구문 분석하려고하면 나는 다음과 같은 오류가 발생합니다 :
Value {"value":[{"Created":"2014-09-29T04:39:10.443","Modified":null,"StatusId":2151,"ModifiedBy":null,"Slot":"Running late","CreatedBy":"","Comment":"-","Location":"Office","Lunch":null,"StartTime":"2014-09-29T12:30:00"}],"odata.metadata":"https:\/\/mySite\/odata\/$metadata#OStatus"} of type org.json.JSONObject cannot be converted to JSONArray
JSON처럼 보이므로 JSON 구문 분석기를 사용하십시오. – nablex
@nablex 내 편집 코드 및 오류 메시지를 참조하십시오. – user2574903