gpt4 book ai didi

Android volley post 请求在正文中带有 json 对象并在 String 中获得响应

转载 作者:行者123 更新时间:2023-11-29 15:00:03 26 4
gpt4 key购买 nike

我如何在正文中发送 json 对象并在字符串中获得响应?

Body:
{
"username": "Shozib@gmail.com",
"password": "Shozib123"
}

回应:“k5ix28k9ikhtcqys4swatnfvohrcg0lp

最佳答案

试试这个

try {
RequestQueue requestQueue = Volley.newRequestQueue(this);
JSONObject jsonBody = new JSONObject();
jsonBody.put("username", "Shozib@gmail.com");
jsonBody.put("password", "Shozib123");
final String mRequestBody = jsonBody.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 volley post 请求在正文中带有 json 对象并在 String 中获得响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48424033/

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