這裏是新的Android開發正在嘗試使用GSON LIB我都轉換成JSON JSON值發佈到服務器,但是當我嘗試發送到服務器是拋出400意外的響應代碼在這裏,讓我後我的代碼:Volley Post方法給我意想不到的迴應代碼400?
listobj = account_sf_db.toServer();
Gson gson = new Gson();
RequestQueue queue = Volley.newRequestQueue(getBaseContext());
final String yog = gson.toJson(listobj);
String URL = "http://xxx.xx.xx.xxx/xxx/CRM/AcoountCreatePageLoad.svc/xxt/xxxt/ " +yog ;
StringRequest stringRequest = new StringRequest(Request.Method.POST, URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
String resp = response.toString();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
})
{
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("god", yog);
return params;
} @Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/json");
headers.put("Accept","application/json");
headers.put("User-agent", "My useragent");
return headers;
}
};
queue.add(stringRequest);
stringRequest.setRetryPolicy(new DefaultRetryPolicy(5000,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
}
您的服務器期望什麼參數?我看到你發佈了JSON數據作爲參數'god'。 'god'是否是正確的參數名稱,或者這可能是您獲得'400 - 錯誤請求'的原因? – jaolstad
它說服務器錯誤 – Yog
@Yog我們不能指望完美的答案,即使服務器(網絡開發)可以手動拋出異常,因爲我們可以拋出Andorid/Java。所以最好用Web dev來討論請求類型,頭文件和參數。也給從errorResponse到dev的響應。 –