【问题标题】:send "POST" list of int发送 int 的“POST”列表
【发布时间】: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 标头中指定字符集并在该字符集中对字符串进行编码。

标签: java android json


【解决方案1】:

HTTP 状态 400 表示错误请求:您向服务器发送了无法解析的内容(您发送的列表包含在对象中,服务器等待纯列表)。

一开始,尝试在客户端用 list 记录 json。 要以 json 格式发送列表,最好将其包装在一个类中:

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

【讨论】:

    【解决方案2】:

    您首先需要将您的列表解析为 json,然后发送。 This 是关于如何将列表解析为 json 格式的类似答案。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多