Ashik.Aust
Ashik.Aust

Reputation: 43

Volley request always get Error Listener

  JsonObjectRequest movieReq = new JsonObjectRequest(Request.Method.GET,url, null,
        new Response.Listener<JSONObject>() {
    @Override
    public void onResponse(JSONObject response) 
    {
        try {
            JSONArray images = response.getJSONArray("images");
            for(int i = 0; i<images.length(); i++)
            {
                try {
                    JSONObject obj =(JSONObject)images.get(i);

                    //JSONObject obj = images.getJSONObject(i);
                    Movie movie = new Movie();
                    movie.setTitle(obj.getString("itemid"));
                    movie.setThumbnailUrl(obj.getString("imagepath"));
                    movieList.add(movie);
                } catch (Exception e) {
                    // TODO: handle exception
                }
            }
        } 
        catch (JSONException e) {


        }
        adapter.notifyDataSetChanged();
    };

        } 
        , new Response.ErrorListener(

                ) {

                    @Override
                    public void onErrorResponse(VolleyError error) {
                        // TODO Auto-generated method stub
                        Toast.makeText(getApplicationContext(), "Error in Json parse", Toast.LENGTH_LONG).show();
                        hidePDialog();
                    }
        });

JSON part:

{ 
"images": 
{ 
"imageid":"1",
"itemid":"item1",
"imagepath":"http:\/\/127.0.0.1\/resturant\/image\/chinese1.jpg"
},
{ 
"imageid":"2",
"itemid":"item2",
"imagepath":"http:\/\/127.0.0.1\/resturant\/image\/chinese2.jpg"
}
]
}

Here I want to retrieve itemid and imagepath tag from images JSON array. But my JSON is not resopnding and it's showing the toast of error listener. Can anybody help, please? I take permission of Internet and I check the URL and it's working.

Upvotes: 1

Views: 2648

Answers (1)

Simulant
Simulant

Reputation: 20112

Please show the content of the url you are calling. If your URL contains 127.0.0.1 your are calling the local host of the android emulator (and your server does not run on the emulator).

To call the localhost of your PC from the emulator you must replace 127.0.0.1 by 10.0.2.2

Upvotes: 1

Related Questions