【发布时间】:2016-04-26 12:47:40
【问题描述】:
我有一个接收 int 或一个 int 列表的服务器。当我发送 int 时,它工作得很好,但是当我尝试发送 List<Integer> 或 int[] 时,响应总是错误(code 400)。我如何发送 int 列表?这是我的代码:
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 出错。
【问题讨论】:
-
When I send int, it works good。请出示该代码。 -
jsonParam.toString()。请说出内容是什么。 -
查看此答案stackoverflow.com/a/34150284/1458740 您可能应该在 Content-Type 标头中指定字符集并在该字符集中对字符串进行编码。