我是新來的凌空我試圖讓發佈採購工作,凌空JsonArrayRequest POST請求不工作
JsonArrayRequest
從很多答案我已經在計算器發現,我試過如下 -
private void getCounts() {
String url = "http://192.168.1.100/json/count.php";
JsonArrayRequest req = new JsonArrayRequest(url,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());
hidepDialog();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
Log.d(TAG, "Error: " + error.getMessage());
Toast.makeText(getApplicationContext(),
error.getMessage(), Toast.LENGTH_SHORT).show();
hidepDialog();
}
}) {
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("LastUpdatedAt", "0");
return params;
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/json; charset=utf-8");
headers.put("User-agent", "My useragent");
return headers;
}
@Override
public byte[] getBody() {
Map<String, String> params = new HashMap<String, String>();
params.put("LastUpdatedAt", "0");
if (params != null && params.size() > 0) {
Log.d(TAG, encodeParameters(params, getParamsEncoding()).toString());
return encodeParameters(params, getParamsEncoding());
}
return null;
}
protected byte[] encodeParameters(Map<String, String> params, String paramsEncoding) {
StringBuilder encodedParams = new StringBuilder();
try {
for (Map.Entry<String, String> entry : params.entrySet()) {
encodedParams.append(URLEncoder.encode(entry.getKey(), paramsEncoding));
encodedParams.append('=');
encodedParams.append(URLEncoder.encode(entry.getValue(), paramsEncoding));
encodedParams.append('&');
}
return encodedParams.toString().getBytes(paramsEncoding);
} catch (UnsupportedEncodingException uee) {
throw new RuntimeException("Encoding not supported: " + paramsEncoding, uee);
}
}
};
AppController.getInstance().addToRequestQueue(req);
}
正如人們所看到的,我曾經嘗試都,getParams()
和getBody()
發送LastUpdatedAt
作爲發佈採購信息,但無論我怎麼努力值不貼和服務器返回null。
我甚至嘗試過使用JsonArrayRequest(Request.Method.POST, ...
以及在請求中發送'params'作爲JsonObject,但即使這兩個都不起作用。
在similar question之一,使用下面的類的建議 -
public class CustomJsonRequest extends Request {
Map<String, String> params;
private Response.Listener listener;
public CustomJsonRequest(int requestMethod, String url, Map<String, String> params,
Response.Listener responseListener, Response.ErrorListener errorListener) {
super(requestMethod, url, errorListener);
this.params = params;
this.listener = responseListener;
}
@Override
protected void deliverResponse(Object response) {
listener.onResponse(response);
}
@Override
public Map<String, String> getParams() throws AuthFailureError {
return params;
}
@Override
protected Response parseNetworkResponse(NetworkResponse response) {
try {
String jsonString = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
return Response.success(new JSONObject(jsonString),
HttpHeaderParser.parseCacheHeaders(response));
} catch (UnsupportedEncodingException e) {
return Response.error(new ParseError(e));
} catch (JSONException je) {
return Response.error(new ParseError(je));
}
}
}
但我不能讓工作響應聽者createRequestSuccessListener
和錯誤監聽。
同樣類似的問題,下面的構造建議 -
public JsonArrayRequest(int method, String url, JSONObject jsonRequest,
Listener<JSONArray> listener, ErrorListener errorListener) {
super(method, url, (jsonRequest == null) ? null : jsonRequest.toString(),
listener, errorListener);
}
但是,我無法弄清楚如何使它發揮作用?
我無法理解其中*究竟*您的問題。不過,過去我也有幾個使用抽籤的POST請求。這個答案解決了我的問題,也許你可以看看它是否有幫助:http://stackoverflow.com/questions/19837820/volley-jsonobjectrequest-post-request-not-working/19945676#19945676 – tjiagoM