java发送int的“POST”列表
我有一个服务器,它接受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");
"安卓.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();
我试着像一个:
int myInt = 1;
List<Integer> list = new ArrayList<Integer>();
list.add(myInt);
及
int[] list = new int[5];
但它总是导致code:400
出错
# 1 楼答案
HTTP状态400意味着错误的请求:您向服务器发送一些它无法解析的内容(您发送用对象包装的列表,服务器等待纯列表)
首先,尝试在客户端用列表记录json。 要以json格式发送列表,最好将其包装在类中:
# 2 楼答案
您首先需要将列表解析为json,然后发送它This是关于如何将列表解析为json格式的类似答案