ibrahim
ibrahim

Reputation: 205

Volley ClientError Unexpected response code 404

I am getting com.android.volley.ClientError exception in onErrorResponse. Below my code. Initially the code works using volley 'StringRequest'; when I change it to use JsonObjectRequest I got the error. I think the error is related to the request parameters. They are not sent to the service. The test REST service is written in .NET but that won't make any difference.

private static final String ENDPOINT = "http://XXX.XXX.X.XXX/Test.App.REST/api/User/";

HashMap<String, String> params = new HashMap<String, String>();
params.put("EmployeeId", "01123");
params.put("Environment", "DAT");

CustomRequest jsonRequest = new CustomRequest
            (Request.Method.GET, ENDPOINT, params, new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    try {
                        ControlUserResponse resp = (ControlUserResponse) gson.fromJson(response.toString(), ControlUserResponse.class);
                        respTxt.setText(resp.firstName + " -- " + resp.lastName);
                    } catch (JsonSyntaxException e) {
                        respTxt.setText(e.toString());
                        e.printStackTrace();
                    }
                }
            }, new Response.ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError error) {
                    respTxt.setText(error.toString());
                    error.printStackTrace();
                }
            });

Volley.newRequestQueue(this).add(jsonRequest);

I use the helper class CustomRequest from this url Volley JsonObjectRequest Post request not working

I can test that the url below is working fine on my machine from the browser and returning the correct data http://XXX.XXX.X.XXX/Test.App.REST/api/User/?EmployeeId=01123&Environment=DAT

Any help will be greatly appreciated. Thanks

Exception stack trace below:

04-26 14:48:27.180 9441-9574/test.app.resttest E/Volley: [308] BasicNetwork.performRequest: Unexpected response code 404 for http://XXX.XX.X.XXX/Test.App.REST/api/User
04-26 14:48:27.183 9441-9441/test.app.resttest W/System.err: com.android.volley.ClientError
04-26 14:48:27.183 9441-9441/asc.app.resttest W/System.err:     at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:164)
04-26 14:48:27.183 9441-9441/asc.app.resttest W/System.err:     at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:112)

Upvotes: 2

Views: 18162

Answers (1)

ibrahim
ibrahim

Reputation: 205

I have changed my code. Now I am getting the correct result from the REST service

Uri.Builder builder = Uri.parse(ENDPOINT).buildUpon();
builder.appendQueryParameter("EmployeeId", "12345555");
builder.appendQueryParameter("Environment", "DEV");

JsonObjectRequest  jsonRequest = new JsonObjectRequest
            (Request.Method.GET, builder.toString(), null, new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    try {
                        ControlUserResponse resp = (ControlUserResponse) gson.fromJson(response.toString(), ControlUserResponse.class);
                        respTxt.setText(resp.firstName + " -- " + resp.lastName);
                    } catch (JsonSyntaxException e) {
                        respTxt.setText(e.toString());
                        e.printStackTrace();
                    }
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    respTxt.setText(error.toString());
                    error.printStackTrace();
                }
            });

Volley.newRequestQueue(this).add(jsonRequest);

Upvotes: 2

Related Questions