我想使用Volley以下列格式将数据发送到服务器。我怎么能这样做?
[ [' CID' => 1,' pid' => 1,'数量' => 5,'摆脱' => 1,' eid' => 1,'做了#39; => 1,' takeAt' => ' 2017-07-24 04:03:23'], [' CID' => 1,' pid' => 1,'数量' => 5,'摆脱' => 2,' eid' => 1,'做了#39; => 1,' takeAt' => ' 2017-07-24 04:03:23'], [' CID' => 1,' pid' => 1,'数量' => 5,'摆脱' => 3,' eid' => 1,'做了#39; => 1,' takeAt' => ' 2017-07-24 04:03:23'] ];
到目前为止,我已经做到了这一点,但它引发了异常
StringRequest stringRequest = new StringRequest(Request.Method.POST, Constant.API_SUBMIT_ORDER, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.e("DATA", "RESPONSE===" + response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
}) {
@Override
protected Map<String, String> getParams() {
Calendar cal = Calendar.getInstance();
String time2 = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss a ").format(cal.getTime());
Map<String, String> orders = new HashMap<>();
for (int i = 0; i < myProductArrayList.size(); i++) {
orders.put("cid", "1");
orders.put("skuid", myProductArrayList.get(i).getProductId());
orders.put("qty", "50");
orders.put("rid", "1");
orders.put("eid", "mukulsingh");
orders.put("did", "1");
orders.put("takenAt", time2);
}
return orders;
}
};
stringRequest.setRetryPolicy(new DefaultRetryPolicy(
MY_SOCKET_TIMEOUT_MS,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
答案 0 :(得分:0)
尝试使用这些类
<强> VollyRequests 强>
public class VollyRequests {
public void MakeStrRequest(final String Tag, String url, final ArrayList<RequestModel> list, final ResponceLisnter responceLisnter, final String HeaderKey) {
MyLog.ShowLog("Url", url);
StringRequest sr = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
responceLisnter.getResponce(response, Tag);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
if (error instanceof TimeoutError || error instanceof NoConnectionError) {
responceLisnter.getResponceError(VollyError.TIMEOUT_ERROR, Tag);
} else if (error instanceof AuthFailureError) {
responceLisnter.getResponceError(VollyError.AUTH_ERROR, Tag);
} else if (error instanceof ServerError) {
responceLisnter.getResponceError(VollyError.SERVER_ERROR, Tag);
} else if (error instanceof NetworkError) {
responceLisnter.getResponceError(VollyError.NETWORK_ERROR, Tag);
}
}
}) {
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
if (list.size() > 0) {
for (int i = 0; i < list.size(); i++) {
//MyLog.ShowLog(list.get(i).getKey(), list.get(i).getValue());
params.put(list.get(i).getKey(), list.get(i).getValue());
}
}
return params;
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put(WebKeys.Headerkey, HeaderKey);
return params;
}
};
AbcApplication.getInstance().getRequestQueue().add(sr);
sr.setRetryPolicy(new DefaultRetryPolicy(50000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
}
}
<强> VollyError 强>
public class VollyError {
public static String TIMEOUT_ERROR = "Please try again.";
public static String NETWORK_ERROR = "Please check your network and try again.";
public static String SERVER_ERROR = "Please try again.";
public static String AUTH_ERROR = "Please try again.";
}
<强> RequestModel 强>
public class RequestModel {
String Key;
String Value;
public ArrayList<JSONObject> getJsonObjects() {
return jsonObjects;
}
public void setJsonObjects(ArrayList<JSONObject> jsonObjects) {
this.jsonObjects = jsonObjects;
}
ArrayList<JSONObject> jsonObjects;
public RequestModel(String key, String value) {
this.Key = key;
this.Value = value;
}
public RequestModel(String key, ArrayList<JSONObject> jsonObjects) {
this.Key = key;
this.jsonObjects = jsonObjects;
}
public String getValue() {
return Value;
}
public void setValue(String value) {
Value = value;
}
public String getKey() {
return Key;
}
public void setKey(String key) {
Key = key;
}
}
<强> ResponceLisnter 强>
public interface ResponceLisnter {
void getResponce(String str, String Tag) throws JSONException;
void getResponceError(String errorStr, String Tag);
}
如何拨打凌空班:
public void callingrequest(){
VollyRequests vollyRequest =new VollyRequests();
ArrayList<RequestModel> list = new ArrayList<>();
list.add(new RequestModel(key, value));
vollyRequests.MakeStrRequest(Tag, WebUrl, list, responceLisnter, HeaderKey);
}
private ResponceLisnter responceLisnter = new ResponceLisnter() {
@Override
public void getResponce(final String responce, String Tag) {
// here is responce
}
@Override
public void getResponceError(String errorStr, String Tag) {
// here is error
}
};
申请类
public class AbcApplication extends Application {
//Static value Holders.
private static AbcApplication mInstance;
public static Context context;
private RequestQueue mRequestQueue;
public static int captureImgCount = 0;
public static synchronized AbcApplication getInstance() {
return mInstance;
}
@Override
public void onCreate() {
super.onCreate();
mInstance = this;
mRequestQueue = Volley.newRequestQueue(this);}
public RequestQueue getRequestQueue() {
return mRequestQueue;
}