我创建了以下自定义JsonObjectRequest,我只想发送一个带有参数的头,以便在服务器上访问授权
public class RequestController extends JsonObjectRequest {
public RequestController(int method, String url, JSONObject jsonParams,
Response.Listener<JSONObject> listener, Response.ErrorListener errorListener) {
super(method, url, jsonParams, listener, errorListener);
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> headers = new HashMap<>();
headers.put("Authorization", MyApplication.getWebServiceKey());
headers.put("Content-Type","application/json");
return headers;
}
}
这也是调用customRequest
的RequestManager公共类RequestManager {
private static final String TAG = "RequestManager";
private Context context;
private RequestInterface requestInterface;
private final Gson gson = new Gson();
public RequestManager(Context paramContext, RequestInterface requestInterfaceParam) {
this.context = paramContext;
this.requestInterface = requestInterfaceParam;
}
public void makeRequest(int method, String req_url, JSONObject jsonParams){
String url = NetworkManager.BASE_URL + req_url;
RequestController requestController = new RequestController(method, url, jsonParams,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d(TAG, "onResponse: " + response.toString());
if(response.optBoolean("status"))
requestInterface.onSuccess(response.toString());
else
requestInterface.onError(response.optJSONArray("errors").toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
Log.e(TAG, "onErrorResponse: " + new String(error.networkResponse.data, StandardCharsets.UTF_8));
requestInterface.onNetworkError(error.toString());
}
});
NetworkManager.getInstance(context).getRequestQueue().add(requestController);
}
}
使用GET方法它总是正常但是当我用POST方法发送时,jsonParams不会发送它。我不知道为什么......
我在这里实现了
JSONObject body = new JSONObject();
body.put("type", typeRegister);
body.put("identityDocumentNumber", "123");
body.put("email", "joselo@gmail.com");
body.put("phone", "123");
body.put("password", "12345678");
body.put("passwordConfirmation", "12345678");
RequestManager requestManager = new RequestManager(SignUpActivity.this, this);
requestManager.makeRequest(Request.Method.POST, "/users", body);
我试图实现StringRequest,但我得到了相同的结果,Map但没有运气。它不起作用。我尝试使用getParams方法来实现它,但它不起作用。我不知道这可能是错的。
编辑:我通过将此更改添加到我的RequestController
来解决了这个问题@Override
public byte[] getBody() {
return postString != null ? postString.getBytes(Charset
.forName("UTF-8")) : super.getBody();
}
@Override
public String getBodyContentType() {
return postString !=null?"application/json; charset=utf-8":super.getBodyContentType();
}
这是因为我的RequestController扩展了JsonObjectRequest,它扩展了Request并使用getBody而不是getParams,因此getParams被忽略但是我解决了
答案 0 :(得分:0)
试试这个
try{
JSONObject jsonBody =new JSONObject("{\n" +
"\"type\", typeRegister,\n" +
"\"identityDocumentNumber\":\"123\",\n" +
"\"email\":\"joselo@gmail.com\",\n" +
"\"phone\":\"123\",\n" +
"\"password\":\"12345678\",\n" +
"\"passwordConfirmation\":\"12345678\"\n" +
"}");
RequestQueue mQueue = Volley.newRequestQueue(getApplicationContext());
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(LookUp.url, jsonBody,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d(TAG, "onResponse: " + response.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}){
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> headers = new HashMap<>();
headers.put("Authorization", MyApplication.getWebServiceKey());
headers.put("Content-Type","application/json");
return headers;
}
};
mQueue.add(jsonObjectRequest);
}
catch (JSONException ex){
ex.printStackTrace();
Log.e("ERROR",ex.getMessage());
pDialog.cancel();
}