如何在Android应用中通过VolleyRequest发送Raw格式请求参数?
用Volley发送Raw JSON请求参数的实现方法
嘿,这问题我熟!用Volley发送Raw JSON格式的请求参数其实不难,我给你一步步讲清楚怎么做~
第一步:准备你的JSON请求体
首先得把你给出的参数转换成Volley能识别的JSON对象(或者直接用字符串,不过用JSON对象更不容易出错)。你可以手动构建JSONObject和JSONArray,也可以直接把JSON字符串转成对象,这里我给你写两种方式:
方式A:手动构建JSON对象
// 先构建category数组里的单个对象 JSONObject categoryItem = new JSONObject(); try { categoryItem.put("itemcat_id", "a382e6f2-86d9-44e9-9e95-ad0007629000"); categoryItem.put("itemcat_outlet_id", "10"); categoryItem.put("itemcat_name", "Oreo Enak"); categoryItem.put("itemcat_status", "1"); categoryItem.put("itemcat_is_editable", "1"); categoryItem.put("itemcat_cdate", "2018-04-17 15:35:20"); categoryItem.put("itemcat_ldate", JSONObject.NULL); // 注意null值要用JSONObject.NULL,不能直接传null } catch (JSONException e) { e.printStackTrace(); } // 把单个对象放进数组里 JSONArray categoryArray = new JSONArray(); categoryArray.put(categoryItem); // 最后构建完整的请求体JSON JSONObject requestBody = new JSONObject(); try { requestBody.put("category", categoryArray); } catch (JSONException e) { e.printStackTrace(); }
方式B:直接用JSON字符串转换
String jsonString = "{ \"category\" : [ { \"itemcat_id\": \"a382e6f2-86d9-44e9-9e95-ad0007629000\", \"itemcat_outlet_id\": \"10\", \"itemcat_name\": \"Oreo Enak\", \"itemcat_status\": \"1\", \"itemcat_is_editable\": \"1\", \"itemcat_cdate\" : \"2018-04-17 15:35:20\", \"itemcat_ldate\" : null } ] }"; JSONObject requestBody = new JSONObject(jsonString);
第二步:用Volley发送请求
Volley提供了两种常用的方式来发送Raw JSON请求,你可以根据自己的需求选:
方式一:用JsonObjectRequest(推荐,更简洁)
JsonObjectRequest本身就支持把JSON对象作为请求体发送,只需要在构造方法里传入我们准备好的requestBody,同时别忘了设置正确的请求头(告诉服务器我们发的是JSON格式):
// 替换成你的实际接口地址 String apiUrl = "https://your-api-url.com/endpoint"; JsonObjectRequest jsonRequest = new JsonObjectRequest( Request.Method.POST, apiUrl, requestBody, // 这里传入我们的JSON请求体 new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject response) { // 请求成功!在这里处理服务器返回的响应 Log.d("VolleySuccess", "响应数据:" + response.toString()); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { // 请求失败,处理错误信息 Log.e("VolleyError", "请求出错:" + error.getMessage()); } }) { // 重写getHeaders方法,设置Content-Type为application/json @Override public Map<String, String> getHeaders() throws AuthFailureError { Map<String, String> headers = new HashMap<>(); headers.put("Content-Type", "application/json; charset=utf-8"); // 如果你的接口需要其他请求头(比如token),也在这里加 // headers.put("Authorization", "Bearer your-token"); return headers; } }; // 获取Volley的RequestQueue并添加请求 RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext()); requestQueue.add(jsonRequest);
方式二:自定义StringRequest(适合需要更灵活控制的场景)
如果你的接口返回的不是JSON格式,或者你需要更精细地控制请求体,也可以用StringRequest,重写getBody和getBodyContentType方法:
StringRequest stringRequest = new StringRequest( Request.Method.POST, apiUrl, new Response.Listener<String>() { @Override public void onResponse(String response) { // 处理字符串格式的响应 Log.d("VolleySuccess", "响应数据:" + response); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Log.e("VolleyError", "请求出错:" + error.getMessage()); } }) { @Override public String getBodyContentType() { return "application/json; charset=utf-8"; } @Override public byte[] getBody() throws AuthFailureError { try { // 把JSON对象转成字节数组作为请求体 return requestBody.toString().getBytes("utf-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); return null; } } @Override public Map<String, String> getHeaders() throws AuthFailureError { Map<String, String> headers = new HashMap<>(); headers.put("Content-Type", "application/json; charset=utf-8"); return headers; } }; requestQueue.add(stringRequest);
最后别忘了加权限!
在你的AndroidManifest.xml里一定要加上网络权限,不然请求会失败:
<uses-permission android:name="android.permission.INTERNET" />
另外,建议把RequestQueue做成单例模式,避免每次请求都创建新的队列,这样性能更好哦~
内容的提问来源于stack exchange,提问作者Fikri Imaduddin
相关产品推荐
相关产品推荐

