gpt4 book ai didi

android - 如何发送{数据:[{id:"11:}]} request to server in using volley and pass ObjectRequest in body

转载 作者:行者123 更新时间:2023-11-29 02:33:06 26 4
gpt4 key购买 nike

I want to pass {"data:"[{"id":"12"}]} in volley request body.

private void reqrej(final String currentLat) {


String insertData = "http://www.xxxxxxxx.com/makeinforequest.php";
final StringRequest stringRequest = new StringRequest(Request.Method.POST, insertData, new Response.Listener<String>() {
@Override
public void onResponse(String response) {

Log.i("json", response.toString());

tv.setText(response.toString());

}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {

}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {

// String mylong = String.valueOf(mCurrentLocation.getLongitude()).toString();

Map<String, String> params = new HashMap<String, String>();
params.put("data", "");


//where I can type request code?

return checkParams(params);


}

private Map<String, String> checkParams(Map<String, String> map) {
Iterator<Map.Entry<String, String>> it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String, String> pairs = (Map.Entry<String, String>) it.next();
if (pairs.getValue() == null) {
map.put(pairs.getKey(), "");
}
}
return map;
}
};

RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);


}

请指导我如何使用 volley 在 android 中传递这些东西,我尝试了很多次但没有成功。

最佳答案

试试这个

try {
RequestQueue requestQueue = Volley.newRequestQueue(this);
try {
JSONObject jsonObject=new JSONObject();
JSONArray jsonArray=new JSONArray();
JSONObject idJson=new JSONObject();
idJson.put("id","12");
jsonArray.put(jsonObject);
jsonObject.put("data",jsonArray);

} catch (JSONException e) {
e.printStackTrace();
}
final String mRequestBody = jsonObject.toString();

StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.i("LOG_RESPONSE", response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("LOG_RESPONSE", error.toString());
}
}) {
@Override
public String getBodyContentType() {
return "application/json; charset=utf-8";
}

@Override
public byte[] getBody() throws AuthFailureError {
try {
return mRequestBody == null ? null : mRequestBody.getBytes("utf-8");
} catch (UnsupportedEncodingException uee) {
VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s", mRequestBody, "utf-8");
return null;
}
}



@Override
protected Response<String> parseNetworkResponse(NetworkResponse response) {
String responseString = "";
if (response != null) {
responseString = String.valueOf(response.statusCode);
}
return Response.success(responseString, HttpHeaderParser.parseCacheHeaders(response));
}
};

requestQueue.add(stringRequest);
} catch (JSONException e) {
e.printStackTrace();
}

关于android - 如何发送{数据:[{id:"11:}]} request to server in using volley and pass ObjectRequest in body,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48543382/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com