以下指南我看到了:
JsonArrayRequest movieReq = new JsonArrayRequest(URL,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
// Parsing json
for (int i = 0; i < response.length(); i++) {
try {
JSONObject obj = response.getJSONObject(i);
Movie movie = new Movie();
movie.setTitle(obj.getString("nome_stanza"));
movie.setThumbnailUrl(obj.getString("icona"));
movieList.add(movie);
} catch (JSONException e) {
e.printStackTrace();
}
}
adapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
AppController2.getInstance().addToRequestQueue(movieReq); //i need this movieReq
但我没有一个Jsoncode并置的URL,我从另一个方法接收jsoncode,给我一个这样的字符串:
{"risposta_server": [{"nome_stanza":"via oslavia"},{"icona":"http:\/\/****.**.**.**\/fil***\/****\/images\/icon\/icon2.png"},{"Admin":"s"}]}
然后,如何更改第一个代码才能使用此结果?
答案 0 :(得分:1)
我的反应非常混乱,但我知道它应该来Json Object
。下一步:
JSONOArray array = response.getJSONArrary("risposta_server");
for (int i = 0; i < array.length(); i++) {
try {
JSONObject obj = array.getJSONObject(i);
Movie movie = new Movie();
if(i == 0){
movie.setTitle(obj.getString("nome_stanza"));
}else if(i == 1){
movie.setThumbnailUrl(obj.getString("icona"));
}
movieList.add(movie);
} catch (JSONException e) {
e.printStackTrace();
}
}
...
如果我误解,请查看工作。
答案 1 :(得分:0)
也许你需要在PHP脚本上使用像json_encode($str)
这样的json编码。其中$str
是数组列表。
所以,你的服务器脚本将是这样的
$arr = array('hello', 'world', 'where', 'i', 'life');
echo json_encode($arr);
然后你将从服务器获取json数据