如何设置body获取截击请求?
How to set body to get volley request?
你好,我在 Android Volley 中将 body 设置为 Request.Method.GET
时遇到问题,我的代码如下所示:
public void getRideItem(String userToken) {
RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
String sendRequest;
JSONObject requestBody = new JSONObject();
try {
requestBody.put("lat", "-64.9631");
requestBody.put("lng", "40.719296");
} catch (JSONException e) {
e.printStackTrace();
}
sendRequest = requestBody.toString();
String mURL = "https://taxiapp.webfumeprojects.online/api/ride/getHome";
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, mURL,
null,
response -> {
Log.i(TAG, "Mahdi: HomeFragment: getCar: res 0 " + response);
}, error -> {
error.printStackTrace();
}) {
@Override
public String getBodyContentType() {
return "application/json; charset=utf-8";
}
@Override
public byte[] getBody() {
byte[] body = new byte[0];
try {
body = sendRequest.getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
e.fillInStackTrace());
}
return body;
}
@Override
public Map<String, String> getHeaders() {
Map<String, String> params = new HashMap<>();
params.put("token", userToken);
return params;
}
@Override
protected Response parseNetworkResponse(NetworkResponse response) {
try {
Log.i(TAG, "Mahdi: HomeFragment: getCar: res 1 " + response.data);
String jsonString = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
return Response.success(new JSONObject(jsonString), HttpHeaderParser.parseCacheHeaders(response));
} catch (UnsupportedEncodingException | JSONException e) {
return Response.error(new ParseError(e));
}
}
};
requestQueue.add(jsonObjectRequest);
}
但是当我 运行 我的应用程序时,我从 Volley com.android.volley.TimeoutError
收到了这个错误,还有一些来自邮递员的图片以获取更多信息:
授权选项卡:
Body 选项卡:
所以我首先要做的是测试 Json Volley 请求正在生成 jsonObject.toString();
然后个人针对我的 Volley 请求,我的正文请求如下所示
@Override
public byte[] getBody()
{
byte[] body;
body = mRequestBody.getBytes(StandardCharsets.UTF_8);
return body;
}
你好,我在 Android Volley 中将 body 设置为 Request.Method.GET
时遇到问题,我的代码如下所示:
public void getRideItem(String userToken) {
RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
String sendRequest;
JSONObject requestBody = new JSONObject();
try {
requestBody.put("lat", "-64.9631");
requestBody.put("lng", "40.719296");
} catch (JSONException e) {
e.printStackTrace();
}
sendRequest = requestBody.toString();
String mURL = "https://taxiapp.webfumeprojects.online/api/ride/getHome";
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, mURL,
null,
response -> {
Log.i(TAG, "Mahdi: HomeFragment: getCar: res 0 " + response);
}, error -> {
error.printStackTrace();
}) {
@Override
public String getBodyContentType() {
return "application/json; charset=utf-8";
}
@Override
public byte[] getBody() {
byte[] body = new byte[0];
try {
body = sendRequest.getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
e.fillInStackTrace());
}
return body;
}
@Override
public Map<String, String> getHeaders() {
Map<String, String> params = new HashMap<>();
params.put("token", userToken);
return params;
}
@Override
protected Response parseNetworkResponse(NetworkResponse response) {
try {
Log.i(TAG, "Mahdi: HomeFragment: getCar: res 1 " + response.data);
String jsonString = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
return Response.success(new JSONObject(jsonString), HttpHeaderParser.parseCacheHeaders(response));
} catch (UnsupportedEncodingException | JSONException e) {
return Response.error(new ParseError(e));
}
}
};
requestQueue.add(jsonObjectRequest);
}
但是当我 运行 我的应用程序时,我从 Volley com.android.volley.TimeoutError
收到了这个错误,还有一些来自邮递员的图片以获取更多信息:
授权选项卡:
Body 选项卡:
所以我首先要做的是测试 Json Volley 请求正在生成 jsonObject.toString();
然后个人针对我的 Volley 请求,我的正文请求如下所示
@Override
public byte[] getBody()
{
byte[] body;
body = mRequestBody.getBytes(StandardCharsets.UTF_8);
return body;
}