在Android中使用Volley将JSON数组体发布到.NETWeb服务
我试图在Android中使用截击发布一个JSON正文,在花了很多小时都没有成功之后,我正在写这个问题 下面是我的代码片段在Android中使用Volley将JSON数组体发布到.NETWeb服务,android,json,web-services,http-post,android-volley,Android,Json,Web Services,Http Post,Android Volley,我试图在Android中使用截击发布一个JSON正文,在花了很多小时都没有成功之后,我正在写这个问题 下面是我的代码片段 StringRequest residentSyncRequest = new StringRequest(Request.Method.POST, Commons.URL,this,this,ADD_REQ){ @Override public Map<String, String> getHeaders() throws AuthFailure
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);
我尝试使用postman chrome扩展名调用相同的web服务,该服务工作正常
不确定在返回字节[]
之前是否需要进行任何编码
请帮忙
提前感谢。Volley默认情况下将所有post参数作为JSON值,因此,您只需要使用一个请求,而不是StringRequest,只需添加所有要作为常规post值发布的JSON值。Volley不支持像JsonObject请求那样直接发布JsonArray请求。希望他们能在下一版本中添加此函数,因为有很多api只提供JsonArray 编辑: 其实有一个解决办法
E/Volley﹕ [255] BasicNetwork.performRequest: Unexpected response code 400 for ...