POST JSON问题

时间:2016-04-26 10:40:41

标签: android json http-post

我正在尝试向JSON文件发送一个http POST请求,其中包含1个int和2个list []。服务器得到我的请求,但json总是空的。我的代码怎么了?当我发布HashMap登录+传递时,它运行良好,但是json出错了......

 try {
               JSONObject jsonParam = new JSONObject();
                jsonParam.put("categories_1", y);
               jsonParam.put("categories_1", x);
               jsonParam.put("price", 3000);

               url = new URL(adress);
            conn = (HttpURLConnection) url.openConnection();
            conn.setReadTimeout(15000);
            conn.setConnectTimeout(15000);
            conn.setRequestMethod("POST");
            conn.setDoInput(true);
            conn.setDoOutput(true);

            OutputStream os = conn.getOutputStream();
            BufferedWriter writer = new BufferedWriter(
                    new OutputStreamWriter(os, "UTF-8"));
               writer.write(jsonParam.toString());

            writer.flush();
            writer.close();
            os.close();
            int responseCode=conn.getResponseCode();

            if (responseCode == HttpsURLConnection.HTTP_OK) {

                String line = null;
                BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                while ((line = br.readLine()) != null) {

                    buffer.append(line);
                }
                resultJson = buffer.toString();

                Log.d(LOG_TAG, "response - " + response);
                Log.d(LOG_TAG, "Good response - " + responseCode);
            }
            else {
                response="";
                Log.d(LOG_TAG, "bad responce - " + responseCode);

            }

0 个答案:

没有答案