发送INT的“POST”列表
问题描述:
我有服务器需要int或一个int的列表。当我发送int时,它运行良好,但是当Im尝试发送List<Integer>
或int[]
时,响应总是出错(code 400
)。我如何发送int列表?这里是我的代码:发送INT的“POST”列表
url = new URL(adress);
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setDoOutput(true);
urlConnection.setRequestMethod("POST");
urlConnection.setUseCaches(false);
urlConnection.setConnectTimeout(10000);
urlConnection.setReadTimeout(10000);
urlConnection.setRequestProperty("Content-Type","application/json");
"android.schoolportal.gr");
urlConnection.connect();
JSONObject jsonParam = new JSONObject();
jsonParam.put("categories_okpd_2", list);
OutputStreamWriter out = new OutputStreamWriter(urlConnection.getOutputStream());
out.write(jsonParam.toString());
out.close();
我试图list
像:
int myInt = 1;
List<Integer> list = new ArrayList<Integer>();
list.add(myInt);
和
int[] list = new int[5];
但它总是导致错误与code:400
。
答
HTTP状态400意味着不好的请求:你发送给服务器一些东西,它不能解析(你发送列表包裹在对象中,服务器等待纯列表)。
开始时,尝试在客户端记录带有列表的json。 以JSON格式发送列表更好地包装在一个类:
{
"list": [0, 4, 5]
}
'当我发送int时,它工作正常。请显示该代码。 – greenapps
400是什么意思? – greenapps
'jsonParam.toString()'。请说出是什么。 – greenapps