0

I have server that takes list of int or one int. When I send int, it works good, but when Im trying send List<Integer> or int[], response is error(code 400) always. How I can send list of int? Here is my code:

  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();

I tried list like a:

  int myInt = 1;
        List<Integer> list = new ArrayList<Integer>();
        list.add(myInt);

and

int[] list = new int[5];

but it always results in an error with code:400.

mnille
  • 1,328
  • 4
  • 16
  • 20
MrStuff88
  • 327
  • 3
  • 11

2 Answers2

1

HTTP status 400 means bad request: you send to server something, that it can't parse (you send list wrapped in object, and server waits pure list).

At the beginning, try to log json with list at client side. To send list in json format better to wrap it in a class:

{
    "list": [0, 4, 5]
}
1

You fist need to parse your list to json and then send it. This is a similar answer on how to parse a list to json format.

Community
  • 1
  • 1
Joakim Ericsson
  • 4,616
  • 3
  • 21
  • 29