2014-01-20 22 views
0

我想在Android中使用Volley發佈一個JSON體,並花了很多小時沒有成功之後我正在寫這個問題。POST JSON數組體使用.NET中的web服務在Android中使用Volley

以下是我的代碼片段

StringRequest residentSyncRequest = new StringRequest(Request.Method.POST, Commons.URL,this,this,ADD_REQ){ 
    @Override 
    public Map<String, String> getHeaders() throws AuthFailureError { 

     HashMap<String,String> params = new HashMap<String, String>(); 
     params.put("Content-Type","application/json"); 

     return params; 
    } 

    @Override 
    public byte[] getBody() throws AuthFailureError { 
     JSONArray jsonArray = new JSONArray(); 
     JSONObject jsonObject = new JSONObject(); 

     SharedPreferences sharedPreferences = getActivity().getSharedPreferences(Commons.PREF_NAME, Context.MODE_PRIVATE); 

     try { 
      jsonObject.put("RowID","0"); 
      jsonObject.put("LocalID","100"); 
      jsonObject.put("LoginUserID",sharedPreferences.getString(Commons.pref_loginUserId,""));      
      jsonObject.put("AppToken",sharedPreferences.getString(Commons.pref_appToken,"")); 

     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 

     jsonArray.put(jsonObject); 

     return jsonArray.toString().getBytes(); 
    } 
}; 
VollyRequest.getRequestQueue(getActivity()).add(residentSyncRequest); 

和我得到如下回應

E/Volley﹕ [255] BasicNetwork.performRequest: Unexpected response code 400 for ... 

我試着用郵差的Chrome擴展程序調用相同的Web服務和服務工作就好了。

不知道我是否需要在返回byte[]之前做任何編碼。

請幫忙。

在此先感謝。

回答

0

Volley不直接支持JsonArray請求,比如JsonObject request.Hope他們可以在下一個版本中添加這個函數,因爲有很多API只提供JsonArray。

編輯:

其實有一個解決方案here

0

Volley默認情況下會將所有後置參數設置爲JSON值,因此您只需使用Request而不是StringRequest,並只需添加想要作爲常規發佈值發佈的所有JSON值。