![](/img/trans.png)
[英]How to post a string using android-volley and returns json Array of objects for php web service
[英]POST JSON array body to .NET web service using Volley in Android
我試圖在Android中使用Volley發布JSON正文,並且花了很多小時都沒有成功之后,我才寫了這個問題。
以下是我的代碼段
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[]
之前是否需要進行任何編碼。
請幫忙。
提前致謝。
Volley默認情況下將所有發布參數作為JSON值放置,因此您只需要使用Request而不是StringRequest,而只需添加要發布的所有JSON值作為常規發布值即可。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.