![](/img/trans.png)
[英]How to send Body Data to GET Method Request android using volley?
[英]How to set body to get volley request?
美好的一天,我在 Android Volley 中将正文设置为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
得到这个错误,以及来自 postman 的一些图像以获取更多信息:
授权标签:
正文选项卡:
所以我首先要做的是测试 Json Volley 请求正在生成jsonObject.toString();
然后亲自为我的 Volley 请求我的身体请求如下所示
@Override
public byte[] getBody()
{
byte[] body;
body = mRequestBody.getBytes(StandardCharsets.UTF_8);
return body;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.