使用截击连接到 android 中的服务器时出现内部服务器错误?
Internal server error while connecting to Server in android using volley?
这里是我的函数代码 post 使用 volley
StringRequest stringRequest = new StringRequest(Request.Method.POST, URL_CHECK_IN,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonObject = new JSONObject(response);
int status = jsonObject.getInt("status");
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError response) {
}
}) {
@Override
public String getBodyContentType() {
return "application/json; charset=utf-8";
}
@Override
public byte[] getBody() throws AuthFailureError {
try {
return requestBody == null ? null : requestBody.getBytes("utf-8");
} catch (UnsupportedEncodingException uee) {
VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s", requestBody, "utf-8");
return null;
}
}
@Override
public Map<String, String> getHeaders () {
Map<String, String> map = new HashMap<String, String>();
map.put("Content-Type", "application/json");
map.put("appid", appids);
map.put("timestamp", timestamps);
map.put("token", tokens);
map.put("signature", signatures);
return map;
}
};
}
我不知道我的代码有什么问题,因为 2 天前一切正常。
当我尝试调试时,错误显示如下
BasicNetwork.performRequest: Unexpected response code 500 for http://api/presence/check_in
有人能帮帮我吗?因为我卡住了,需要帮助或参考来解决我的错误
谢谢
HTTP 代码 500 是内部服务器错误。阅读更多 here。这通常意味着服务器无法处理请求并做出响应。这意味着您的应用程序的代码可能没问题,而服务器在处理当前请求正文时可能会遇到一些问题。我看到您在请求正文中发送了 String
。我在请求正文中发送 String
时注意到的一件奇怪的事情是,我们还需要检查 String
是否为 null
,最好使用 .trim()
方法你的字符串的结尾也是如此,这将删除开始和尾随的空格。一些简单的事情,例如您尝试插入到服务器数据库中的字段的 not escaping 单引号 ( ' ) 可能会导致此问题。因此,服务器端字段验证和最佳实践(如 Prepared Statements)也至关重要。如果您绝对确定您的客户端 [android app] 没问题,则服务器可能在您访问的端点遇到了一些问题。
使用像 POSTMAN or INSOMNIA 这样的休息客户端测试您的 api 以绝对确保您的服务器和 api 层按预期工作。祝你好运
这里是我的函数代码 post 使用 volley
StringRequest stringRequest = new StringRequest(Request.Method.POST, URL_CHECK_IN,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonObject = new JSONObject(response);
int status = jsonObject.getInt("status");
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError response) {
}
}) {
@Override
public String getBodyContentType() {
return "application/json; charset=utf-8";
}
@Override
public byte[] getBody() throws AuthFailureError {
try {
return requestBody == null ? null : requestBody.getBytes("utf-8");
} catch (UnsupportedEncodingException uee) {
VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s", requestBody, "utf-8");
return null;
}
}
@Override
public Map<String, String> getHeaders () {
Map<String, String> map = new HashMap<String, String>();
map.put("Content-Type", "application/json");
map.put("appid", appids);
map.put("timestamp", timestamps);
map.put("token", tokens);
map.put("signature", signatures);
return map;
}
};
}
我不知道我的代码有什么问题,因为 2 天前一切正常。 当我尝试调试时,错误显示如下
BasicNetwork.performRequest: Unexpected response code 500 for http://api/presence/check_in
有人能帮帮我吗?因为我卡住了,需要帮助或参考来解决我的错误 谢谢
HTTP 代码 500 是内部服务器错误。阅读更多 here。这通常意味着服务器无法处理请求并做出响应。这意味着您的应用程序的代码可能没问题,而服务器在处理当前请求正文时可能会遇到一些问题。我看到您在请求正文中发送了 String
。我在请求正文中发送 String
时注意到的一件奇怪的事情是,我们还需要检查 String
是否为 null
,最好使用 .trim()
方法你的字符串的结尾也是如此,这将删除开始和尾随的空格。一些简单的事情,例如您尝试插入到服务器数据库中的字段的 not escaping 单引号 ( ' ) 可能会导致此问题。因此,服务器端字段验证和最佳实践(如 Prepared Statements)也至关重要。如果您绝对确定您的客户端 [android app] 没问题,则服务器可能在您访问的端点遇到了一些问题。
使用像 POSTMAN or INSOMNIA 这样的休息客户端测试您的 api 以绝对确保您的服务器和 api 层按预期工作。祝你好运