供您评论
IMO,而不是您的第一个片段,您可以尝试以下方式(当然,您可以
JSONArray按
JSONObject要求替换请求):
VolleyResponseListener listener = new VolleyResponseListener() { @Override public void onError(String message) { // do something... } @Override public void onResponse(Object response) { // do something... } };makeJsonArrayRequest(context, Request.Method.POST, url, requestBody, listener);
的主体
makeJsonArrayRequest可以如下:
public void makeJsonArrayRequest(Context context, int method, String url, String requestBody, final VolleyResponseListener listener) { JSonObject jsonRequest = null; try { ... if (requestBody != null) { jsonRequest = new JSonObject(requestBody); } ... } catch (JSonException e) { e.printStackTrace(); } JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(method, url, jsonRequest, new Response.Listener<JSONArray>() { @Override public void onResponse(JSonArray jsonArray) { listener.onResponse(jsonArray); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { listener.onError(error.toString()); } }); // Access the RequestQueue through singleton class. MySingleton.getInstance(context).addToRequestQueue(jsonArrayRequest); }
VolleyResponseListener界面如下:
public interface VolleyResponseListener { void onError(String message); void onResponse(Object response);}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)