尝试将arraylist发送到服务器,但似乎格式不正确。
我在StringRequest中的参数
try:
print('3'+euler_str[1:int(digits)+2])
except ValueError:
print(digitsError)
我的服务器响应
ValueError: invalid literal for int() with base 10:
arrayString对我没有意义,因为@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<>();
params.put("source", "en");
params.put("target", "zh");
JSONArray jsonArray = new JSONArray();
jsonArray.put("hello world");
params.put("stringArray", jsonArray.toString());
return params;
}
只是一个字符串,而不是一个字符串数组。我希望arrayString应该是这样的{"result":[],"source":"en","target":"zh","arrayString":"[\"hello world\"]"}
有什么建议吗?
修改
试图使用JsonObjectRequest。
"[\"hello world\"]"
服务器响应
"arrayString":[\"hello world\"]
Php代码
Map<String, Object> params = new HashMap();
params.put("source", "en");
params.put("target", "zh");
params.put("stringArray", Arrays.asList("hello world"));
JSONObject parameters = new JSONObject(params);
JsonObjectRequest jsonRequest = new JsonObjectRequest(Request.Method.POST, requestString, parameters, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
//TODO: handle success
Log.d("successful", response.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
//TODO: handle failure
}
});
Volley.newRequestQueue(getActivity()).add(jsonRequest);
编辑2
如果我将JSONObject与其put方法和JSONArray一起使用,我仍然会得到null结果
successful: {"result":null,"source":null,"target":null,"arrayString":null}
打印和服务器响应
<?php
$source = $_POST['source'];
$target = $_POST['target'];
$arrayString = $_POST['stringArray'];
echo json_encode(array('result'=>$results,'source'=>$source,'target'=>$target,'arrayString'=>$arrayString));
?>