POST 不使用参数的方法
POST Method not using Params
我不知道为什么 getParams() 在我的方法中不起作用?
System.out.println 在 getHeaders 下工作正常但在 getParams 下不工作?
//---------------------------------------POST request with headers---------------------------------------
public void post(String url, final String param1, final String param2, String source) {
// Request a string response from the provided URL.
JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, url,
getReponse(source), createMyReqErrorListener()) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
HashMap<String, String> params = new HashMap<>();
params.put("loginAlias", "username");
params.put("loginPassword", "12345");
System.out.println("Params are: " + params.toString());
return params;
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<>();
headers.put(ACCEPT, ACCEPT_JSON); //Accepting JSON
headers.put(AUTH_ID, AUTH_ID_VALUE);
headers.put(PLATFORM, PLATFORM_VALUE);
headers.put(CID, "");
headers.put(DEVICE_TYPE_MOBILE, DEVICE_TYPE_MOBILE_VALUE);
System.out.println("Headers are: " + headers.toString());
return headers;
}
};
// Add the request to the RequestQueue.
queue.add(request);
}
我已遵循所有 Google Volley 文档并在 SO 上尝试了几个选项,但出于某种原因,这不起作用?
谢谢
也遇到了同样的问题,最后我发现 volley - JSONObject 请求有问题。 (经过几次谷歌搜索!)
getParams() 不会调用,因为 JsonObjectRequest 扩展了 JsonRequest 调用 getBody() 直接将构造函数的第二个参数(调用 requestBody)编码为 contentType,这就是它忽略您的 getParam() 方法的原因。
试试这个解决方案,它解决了我的问题。
import java.io.UnsupportedEncodingException;
import java.util.Map;
import org.json.JSONException;
import org.json.JSONObject;
import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.toolbox.HttpHeaderParser;
public class CustomRequest extends Request<JSONObject> {
private Listener<JSONObject> listener;
private Map<String, String> params;
public CustomRequest(String url, Map<String, String> params,
Listener<JSONObject> reponseListener, ErrorListener errorListener) {
super(Method.GET, url, errorListener);
this.listener = reponseListener;
this.params = params;
}
public CustomRequest(int method, String url, Map<String, String> params,
Listener<JSONObject> reponseListener, 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<JSONObject> 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));
}
}
@Override
protected void deliverResponse(JSONObject response) {
// TODO Auto-generated method stub
listener.onResponse(response);
}
}
在activity/fragment中使用这个
RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
CustomRequest jsObjRequest = new CustomRequest(Method.POST, url, params, this.createRequestSuccessListener(), this.createRequestErrorListener());
requestQueue.add(jsObjRequest);
Volley JsonObjectRequest Post request not working
我不知道为什么 getParams() 在我的方法中不起作用?
System.out.println 在 getHeaders 下工作正常但在 getParams 下不工作?
//---------------------------------------POST request with headers---------------------------------------
public void post(String url, final String param1, final String param2, String source) {
// Request a string response from the provided URL.
JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, url,
getReponse(source), createMyReqErrorListener()) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
HashMap<String, String> params = new HashMap<>();
params.put("loginAlias", "username");
params.put("loginPassword", "12345");
System.out.println("Params are: " + params.toString());
return params;
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<>();
headers.put(ACCEPT, ACCEPT_JSON); //Accepting JSON
headers.put(AUTH_ID, AUTH_ID_VALUE);
headers.put(PLATFORM, PLATFORM_VALUE);
headers.put(CID, "");
headers.put(DEVICE_TYPE_MOBILE, DEVICE_TYPE_MOBILE_VALUE);
System.out.println("Headers are: " + headers.toString());
return headers;
}
};
// Add the request to the RequestQueue.
queue.add(request);
}
我已遵循所有 Google Volley 文档并在 SO 上尝试了几个选项,但出于某种原因,这不起作用?
谢谢
也遇到了同样的问题,最后我发现 volley - JSONObject 请求有问题。 (经过几次谷歌搜索!)
getParams() 不会调用,因为 JsonObjectRequest 扩展了 JsonRequest 调用 getBody() 直接将构造函数的第二个参数(调用 requestBody)编码为 contentType,这就是它忽略您的 getParam() 方法的原因。
试试这个解决方案,它解决了我的问题。
import java.io.UnsupportedEncodingException;
import java.util.Map;
import org.json.JSONException;
import org.json.JSONObject;
import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.toolbox.HttpHeaderParser;
public class CustomRequest extends Request<JSONObject> {
private Listener<JSONObject> listener;
private Map<String, String> params;
public CustomRequest(String url, Map<String, String> params,
Listener<JSONObject> reponseListener, ErrorListener errorListener) {
super(Method.GET, url, errorListener);
this.listener = reponseListener;
this.params = params;
}
public CustomRequest(int method, String url, Map<String, String> params,
Listener<JSONObject> reponseListener, 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<JSONObject> 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));
}
}
@Override
protected void deliverResponse(JSONObject response) {
// TODO Auto-generated method stub
listener.onResponse(response);
}
}
在activity/fragment中使用这个
RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
CustomRequest jsObjRequest = new CustomRequest(Method.POST, url, params, this.createRequestSuccessListener(), this.createRequestErrorListener());
requestQueue.add(jsObjRequest);
Volley JsonObjectRequest Post request not working