2017-01-30 37 views
2

我有一個是通過使用凌空給200響應代碼郵差但不調用API時POST方法API甚至Retrofit2獲得響應代碼200郵遞員,但不是在Android網絡圖書館

這裏是郵差截圖:

enter image description here

這裏我就是這樣做的凌空庫代碼:

final String url = "http://xxxxxxxx.com/api/mobile/user/post/"; 

    StringRequest stringReq = new StringRequest(Request.Method.POST, url, 
      new com.android.volley.Response.Listener<String>() { 
       @Override 
       public void onResponse(String response) { 
        Log.e("onResponse ===", response + " "); 
       } 
      }, 
      new com.android.volley.Response.ErrorListener() { 
       @Override 
       public void onErrorResponse(VolleyError error) { 
        Log.e("onErrorResponse ===", error + " "); 
       } 
      }) { 
     @Override 
     public Map<String, String> getHeaders() throws AuthFailureError { 
      Map<String, String> params = new HashMap<>(); 
      params.put("Authorization", "xxxxxxxxxxxxx"); 
      return params; 
     } 

     @Override 
     public Map<String, String> getParams() { 
      HashMap<String, String> params = new HashMap<>(); 
      params.put("post_body", "test"); 
      return params; 
     } 
    }; 

    // Adding request to request queue 
    mApp.addToRequestQueue(stringReq); 
    stringReq.setRetryPolicy(new DefaultRetryPolicy(
      REQUEST_TIME, 
      DefaultRetryPolicy.DEFAULT_MAX_RETRIES, 
      DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)); 

錯誤logcat的:

BasicNetwork.performRequest: Unexpected response code 500 for http://xxxxxxxxxxx.com/api/mobile/user/post/ 
+0

能否請您分享您的清單? 您是否添加了以下權限? 「android.permission.INTERNET」 「android.permission.ACCESS_NETWORK_STATE」 –

+0

你這個男人毫不懷疑。 –

+0

500是服務器錯誤。我對這些情況的體驗通常與頭文件有關。郵差默認添加了一些翻新和排除的標題。有時候服務器無法應對這種缺少頭部並投擲500。我建議你檢查一下。這可能是另一種方式。 – Fred

回答

6

問題是,您的端點假設multipart/form-data請求(因爲郵遞員中的橙色針腳設置爲form-data單選按鈕),而Volley的默認內容類型爲StringRequestapplication/x-www-form-urlencoded,這就是爲什麼您會得到500錯誤。

因此,如果您只需要在多部分請求中發送POST參數,請檢查this answer。但是,如果你要發送的文件,那麼請another answer

0

你爲什麼不使用JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, url, JSONObject, response, error);的POST請求很容易使用試一次

+0

這不起作用 –

+0

這不是圖書館的問題,它的標題,你的傳球,你可以檢查與ION網絡庫爲Android或檢查郵遞員在響應標題它顯示10,而你傳球之一 –