使用 volley 将 ArrayList 发送到服务器时在响应中收到 null
Posted
技术标签:
【中文标题】使用 volley 将 ArrayList 发送到服务器时在响应中收到 null【英文标题】:Received null in Response when using volley to send ArryList to server 【发布时间】:2019-06-07 13:59:10 【问题描述】:我尝试通过 volley 向服务器发送一个数组列表,但在我的响应方法中收到 null 我用这段代码制作了 ArrayList:
final ArrayList<HashMap<String, String>> arrayList = new ArrayList<HashMap<String, String>>();
for (int i = 0; i < questions.size(); i++)
//addToServer(questions.get(i),questions.size());
HashMap<String, String> params = new HashMap<String, String>();
params.put("TestId", questions.get(i).getTestId() + "");
params.put("Number", questions.get(i).getNumber() + "");
arrayList.add(params);
listOfQ = new JSONArray(arrayList);
并通过此代码将其发送到服务器:
public void addToServer2()
if(requestQueue == null)
requestQueue = Volley.newRequestQueue(myContext);
request = new StringRequest(Request.Method.POST, ServiceApi.URL_send_question2, new Response.Listener<String>()
@Override
public void onResponse(String response)
try
JSONArray jsonArray = new JSONArray(response);
catch (JSONException e)
e.printStackTrace();
catch (Exception e)
, new Response.ErrorListener()
@Override
public void onErrorResponse(VolleyError error)
)
@Override
protected Map<String, String> getParams() throws AuthFailureError
Map<String, String> params = new HashMap<String, String>();
params.put("test", listOfQ.toString());
return params;
@Override
public Map<String, String> getHeaders() throws AuthFailureError
return super.getHeaders();
;
requestQueue.add(request);
这是我的 php 代码,它获取 arraylist 并将一个值返回到 android 端:
$arr = array();
$jsonarray = array();
$json = $_POST['test'];
$jsonarray = json_decode($json,true);
foreach((array) $jsonarray as $key)
$TestId = $key["TestId"];
$arr[] = array('id' => $TestId);
当我调试我的应用程序时,它会向我显示这个响应:id:null
【问题讨论】:
什么是listOfQ
?是 JsonArray 吗?
是的,我将其转换为 JsonArray
【参考方案1】:
我认为你犯了一些错误。
试试这个方法,
1. 创建 JSON 对象。
2. 创建一个 JSON Array 并使用 for 循环将所有列表数据存储到其中。
3. 将您的 JSON 数组存储到 JSON 对象中。(在 for 循环结束后)。
4. 由于您需要以 json 类型但以字符串正文格式发送此数据,所以String requestBody=your_json_object.toString()
.
5.当你使用StringRequest
时,你必须以字符串格式发送url,响应将是字符串格式。
6. 在错误监听器之后,您需要添加这些行,(在一个大括号中)
@Override
public String getBodyContentType()
return "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", mRequestBody, "utf-8");
return null;
7。在此方法下方,您需要添加标头参数以进行身份验证。
@Override
public Map<String, String> getHeaders() throws AuthFailureError
Map<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "multipart/form-data");
headers.put("Authorization", "sometoken_or_id");
return headers;
requestQueue.add(request);
完成
【讨论】:
感谢您的回答,如何将我的 JSON 数组存储到 JSON 对象中?我使用 myObject.put("test",myJArray) 但我有这个错误: org.json.jsonobject$1 类型的 value null 无法转换为 jsonarray以上是关于使用 volley 将 ArrayList 发送到服务器时在响应中收到 null的主要内容,如果未能解决你的问题,请参考以下文章
Android Kotlin Volley 如何发送 JSON 数据
为啥 Volley 不在 getParams() 中发送变量?