String Parameter를 전달하여 JSON 객체를 구문 분석하기 위해 Volley Facebook 라이브러리를 사용하고 있습니다. 하지만 그것은 JSON 예외를 던지고있다.Volley를 사용하여 JSON 요청을 구문 분석하려면 어떻게해야합니까?
StringRequest strReq = new StringRequest(Request.Method.POST,
url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
hidePDialog();
try {
JSONObject first = new JSONObject(response);
String err = first.getString("error");
JSONArray second = first.getJSONArray("newsletter");
fisco_tips.clear();
// Parsing json
for (int i = 0; i < second.length(); i++) {
JSONObject obj = second.getJSONObject(i);
FisoTipsSinglton fisco_obj = new FisoTipsSinglton();
//Set Newsletter ID
fisco_obj.setFisco_id(obj.getString("id"));
//Set Title
fisco_obj.setTitle(obj.getString("title"));
//Set Posted Date
fisco_obj.setPosted_date(obj.getString("date"));
//Set Posted By
fisco_obj.setPosted_by(obj.getString("posted_by"));
//Set Image URL
fisco_obj.setThumbnailUrl(obj.getString("image"));
//Set Short Description
fisco_obj.setShort_description(obj.getString("description"));
fisco_obj.setPost_count(obj.getString("post_count"));
fisco_tips.add(fisco_obj);
}
} catch (JSONException e) {
e.printStackTrace();
Log.d("Json Error", "Here is error: " + e.toString());
}
adapter.notifyDataSetChanged();
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
hidePDialog();
}
}) {
@Override
protected Map<String, String> getParams() {
// Posting params to register url
Map<String, String> params = new HashMap<String, String>();
params.put("country", country);
return params;
}
};
AppController.getInstance().addToRequestQueue(strReq, related_posts);
}
가 어떻게 안드로이드 발리 라이브러리에 문자열 요청을 사용하여 내 JSON 객체를 읽을 수 있습니다
[
{
"error":false,
"newsletter":[
{
"title":"IPS Informa",
"date":"2015-12-02",
"posted_by":"admin",
"image":"1449324052220174144.png",
"description":"Hello World",
"id":"4",
"post_count":"0"
},
{
"title":"IPS Informa",
"date":"2015-11-30",
"posted_by":"admin",
"image":"1449324052220174144.png",
"description":"Hello Worl Two",
"id":"1",
"post_count":"6"
}
]
}
]
그리고 여기 내 안드로이드 코드 내 JSON 요청을 구문 분석하는 것입니다?
질문에 대한 응답은 개체가 아니라 배열입니다. 오류 메시지를 게시하지 않았으므로 이것이 문제인지는 확실하지 않습니다. –
@Billthelizard 오류 메시지가 인 org.json.JSONArray를 JSONObject – Bilal
으로 변환 할 수 없습니다. 그 때가 처음 문제입니다. 응답을 객체로 취급하지만 내부에 객체가있는 배열입니다. 먼저 배열에서 객체를 가져옵니다. –