【问题标题】:Android volley show Unexpected response code 500Android volley show Unexpected response code 500
【发布时间】:2018-03-02 12:24:22
【问题描述】:

我将方法 POST 与 Volley 库一起使用。一切正常,但不显示吐司“成功”。
下面是我用我的服务器成功更新内容的代码。

String uri = "http://192.168.0.103:3000/api/SampleParticipant";
StringRequest request = new StringRequest(Request.Method.POST, uri,
                    new Response.Listener<String>() {
                        @Override
                        public void onResponse(String response) {

                            Toast.makeText(getApplicationContext(), "Successful" , Toast.LENGTH_SHORT).show();
                        }
                    },
                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            Toast.makeText(getApplicationContext(), "Error: " + error, Toast.LENGTH_SHORT).show();
                        }
                    })
            {//Body
                @Override
                protected Map<String, String> getParams() {
                    Map<String, String> params = new HashMap<String, String>();
                    params.put("participantId", editText2.getText().toString());
                    params.put("name", editText3.getText().toString());
                    return params;
                }
            };
            // Create Volley
            RequestQueue queue = Volley.newRequestQueue(this);
            queue.add(request);


但在 android studio 中显示是这样的。

D/Volley: [1124] BasicNetwork.logSlowRequests: HTTP response for request=<[ ] http://192.168.0.103:3000/api/SampleParticipant 0x85adf657 NORMAL 1> [lifetime=5381], [size=704], [rc=500], [retryCount=1]
E/Volley: [1124] BasicNetwork.performRequest: Unexpected response code 500 for http://192.168.0.103:3000/api/SampleParticipant
D/EGL_emulation: eglMakeCurrent: 0xa3f85420: ver 2 0 (tinfo 0xa3f83350)
D/EGL_emulation: eglMakeCurrent: 0xa3f85420: ver 2 0 (tinfo 0xa3f83350)

【问题讨论】:

  • 您好,我注意到您发布了类似的question 并且这两个问题都已接受答案。如果它不起作用,则无需接受答案,如果您还有更多内容要写,您也可以实际编辑问题:)
  • 感谢您的建议。

标签: android android-studio android-volley


【解决方案1】:

这是服务器端的错误,与凌空无关

https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/500

你可以像这样处理 500 错误

@Override
public void onResponse(Response<YourModel> response) {
    if (response.code() == 200) {
       // Do awesome stuff
    } else if(response.code() == 500){
       Toast.makeText(this, "Error: internal server error", Toast.LENGTH_SHORT).show();
    }
}

【讨论】:

  • 感谢您的回复。但我不清楚,你能解释一下或给我一些例子吗?
  • ,这个类会自动解析响应。您不需要手动解析响应。在您的情况下,响应是字符串,它也可以是 JSON 对象或 JSON 数组,但如果您创建 POJO 类并使用 GSON 解析器,访问响应将变得容易。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-12
  • 2016-02-29
  • 1970-01-01
相关资源
最近更新 更多