gpt4 book ai didi

java - 在来自 Android Volley 的 POST 请求中发送 JSON 对象接收 java Restful Webservices

转载 作者:太空宇宙 更新时间:2023-11-04 13:27:55 25 4
gpt4 key购买 nike

我想要一个小例子,从 android volley 发送 POST 请求中的 JSON 对象,并且它必须接收 Java Restful Webservices

最佳答案

您可以使用以下工作示例代码。希望这有帮助!

        ...   
try {
RequestQueue queue = Volley.newRequestQueue(this);
jsonBody = new JSONObject();
jsonBody.put("Title", "VolleyApp Android Demo");
jsonBody.put("Author", "BNK");
jsonBody.put("Date", "2015/08/26");
requestBody = jsonBody.toString();

StringRequest stringRequest = new StringRequest(1, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
textView.setText(response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
textView.setText(error.toString());
}
}) {
@Override
public String getBodyContentType() {
return String.format("application/json; charset=utf-8");
}

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

此外,您能否澄清一下必须接收 Java Restful Webservices 是什么意思?我的上述请求收到 String 值。

关于java - 在来自 Android Volley 的 POST 请求中发送 JSON 对象接收 java Restful Webservices,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32472959/

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