我已经编写了一个代码来发送一个文本,我通过volley将其输入edittext到服务器,但是它无法正常工作,我在第一次尝试使用volley库进行一些研究以便更快地进行网络调用。
发布数据时收到错误
我试过的代码:
CustomJsonRequest.class
public class CustomJsonRequest extends Request<JSONArray>{
private Response.Listener<JSONArray> listener;
private Map<String, String> params;
public CustomJsonRequest(int method, String url, Map<String, String> params,
Response.Listener<JSONArray> reponseListener, Response.ErrorListener
errorListener) {
super(method, url, errorListener);
this.listener = reponseListener;
this.params = params;
}
protected Map<String, String> getParams()
throws com.android.volley.AuthFailureError {
return params;
};
@Override
protected Response<JSONArray> parseNetworkResponse(NetworkResponse response)
{
try {
String jsonString = new String(response.data,
HttpHeaderParser.parseCharset(response.headers));
return Response.success(new JSONArray(jsonString),
HttpHeaderParser.parseCacheHeaders(response));
} catch (UnsupportedEncodingException e) {
return Response.error(new ParseError(e));
} catch (JSONException je) {
return Response.error(new ParseError(je));
}
}
@Override
protected void deliverResponse(JSONArray response) {
listener.onResponse(response);
}
}
Answers.class,下面的方法发生在按钮点击,我想将jsonarray发布到服务器
private void postAnswers(){
String answers = editAnswers.getText().toString();
Map<String,String> answersMap = new HashMap<String,String>();
answersMap.put("answers",answers);
jsObjRequest = new CustomJsonRequest(Request.Method.POST, postAnswerUrl,
answersMap,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
for (int i = 0; i < response.length(); i++) {
try {
JSONObject obj = response.getJSONObject(i);
obj.put("TransactionId", transId);
obj.put("UserType", userType);
obj.put("QId", questionId);
obj.put("Answer", answers);
obj.put("Date", date1);
obj.put("Time", time1);
obj.put("DeviceId", deviceId);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d("Error: " + error.getMessage());
Log.d("Error", "Error: " + error.getMessage());
}
}){
@Override
protected Response<JSONArray> parseNetworkResponse(NetworkResponse
response) {
try {
String jsonString = new String(response.data,
HttpHeaderParser.parseCharset(response.headers));
return Response.success(new JSONArray(jsonString),
HttpHeaderParser.parseCacheHeaders(response));
} catch (UnsupportedEncodingException e) {
return Response.error(new ParseError(e));
} catch (JSONException je) {
return Response.error(new ParseError(je));
}
}
};
AppController.getInstance().addToRequestQueue(jsObjRequest);
}
我在调试时:所有值都正常,但jsonobject请求显示为空
timeval = {String@4116} "3:43 PM"
date1 = {String@4114} "Mon, Dec 7, 2015"
time1 = {String@4116} "3:43 PM"
datetime = {String@4117} "Mon, Dec 7, 2015T3:43 PM"
transId = {String@4118} "DeviceId, Dec 7, 2015T3:43 PM"
answersMap = {HashMap@4119} size = 1
jsObjRequest = {Write_Answers$4@4174} "[ ] http://gps.traxistar.net/PostAnswers.svc/PostAnswers 0xa3925991 NORMAL null"
任何人都可以指导我哪里出错了,我需要做些什么改变? 谢谢