[英]Internal server error while connecting to Server in android using volley?
這里我的 function 代碼使用 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 是內部服務器錯誤。 在這里閱讀更多。 這通常意味着服務器無法處理請求並提出響應。 這意味着您的應用程序的代碼可能沒問題,而服務器在處理當前請求正文時可能會遇到一些問題。 我看到您在請求正文中發送String
。 在請求正文中發送String
時我注意到的一件特別的事情是,我們還需要檢查String
是否為null
,最好在字符串末尾也使用.trim()
方法,這將刪除開頭和結尾空格。 對於您嘗試插入服務器數據庫的字段,諸如 not escaping 單引號 ( ' ) 之類的簡單操作可能會導致此問題。 因此,服務器端字段驗證和諸如 Prepared Statements 之類的最佳實踐也至關重要。 如果您絕對確定您的客戶端 [android 應用程序] 沒問題,那么服務器可能在您訪問的端點遇到了一些問題。 Test your api with a rest client like POSTMAN or INSOMNIA to be absolutely sure that your server and api layer is working as intended. 祝你好運
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.