francesco bocci francesco bocci - 2 months ago 6
Android Question

How to recieve JSONArray

following guide i see this:

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


But i dont have URl where is collocate Jsoncode, I recive jsoncode from another method that give me string like this:

{"risposta_server": [{"nome_stanza":"via oslavia"},{"icona":"http:\/\/****.**.**.**\/fil***\/****\/images\/icon\/icon2.png"},{"Admin":"s"}]}


Then, how can i change first code to use this result?

Answer

I've got mixed up in response, but I understand it's supposed to come Json Object.The next step:

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();
    }
 }
 ...

See the job if I misunderstand.

Comments