Volley ClientError意外的响应代码404

时间:2017-04-26 07:32:54

标签: android json android-volley android-6.0-marshmallow

我在onErrorResponse中收到了com.android.volley.ClientError异常。在我的代码下面。最初代码使用volley' StringRequest&#39 ;;当我改变它以使用JsonObjectRequest我得到了错误。我认为错误与请求参数有关。他们没有被送到这项服务。测试REST服务是用.NET编写的,但是没有任何区别。

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);

我使用此url中的帮助程序类CustomRequest Volley JsonObjectRequest Post request not working

我可以通过浏览器测试下面的url在我的机器上工作正常并返回正确的数据 http://XXX.XXX.X.XXX/Test.App.REST/api/User/?EmployeeId=01123&Environment=DAT

任何帮助将不胜感激。感谢

下面的异常堆栈跟踪:

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)

1 个答案:

答案 0 :(得分:1)

我已经改变了我的代码。现在我从REST服务中获得了正确的结果

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);