如何在HttpUrlConnection中摆脱响应错误请求400?

时间:2016-03-15 11:36:30

标签: java parse-platform httpurlconnection

您好我正在编写一个Java程序,用于使用parse rest API创建解析对象。 我使用下面的代码使用HttpUrlConnection,但每次我收到错误的请求。 我在这里研究了很多线程,但没有得到任何相关的解决方案所以提出新的问题

String surl = "https://api.parse.com/1/classes/DrivingTestQuestion";
    StringBuilder sb = new StringBuilder();
    // 2. Open connection
    HttpURLConnection conn =null;
    try {
        URL url = new URL(surl);

        conn = (HttpURLConnection) url.openConnection();

        conn.setRequestMethod("POST");          
        conn.setDoOutput(true);     
        conn.setRequestProperty("X-Parse-Application-Id",
                "87A3v8ImR58torzhrizQwVd6AULIK1dUBxGKqCTf");
        conn.setRequestProperty("X-Parse-REST-API-Key",
                "1tjhBILLSvJE5iEytQ0WvzYf9ig0nQzmuwE2TbXy");
        conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8");     
         conn.connect();

        OutputStream os = conn.getOutputStream();
        os.write(json.toString().getBytes("UTF-8"));
         os.close();

        int HttpResult = conn.getResponseCode();
        if (HttpResult == HttpURLConnection.HTTP_OK) {
            BufferedReader br = new BufferedReader(new InputStreamReader(
                    conn.getInputStream(), "utf-8"));
            String line = null;
            while ((line = br.readLine()) != null) {
                sb.append(line + "\n");
            }
            br.close();

            System.out.println("" + sb.toString());

        } else {
            System.out.println(conn.getResponseMessage());
        }
    } catch (MalformedURLException e) {
        e.printStackTrace();
        conn.disconnect();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        conn.disconnect();
    } 

以下是我的json,它是请求正文

{"QuestionNumber":21,"Answers":["Sí, porque es la manera más correcta de reglarlo.","No; debe hacerse con el vehículo inmovilizado en terreno plano yhorizontal.","Sí, siempre que circule lentamente."],"question":"¿Es correcto reglar el espejo retrovisor interior con el vehículo en marcha?","TestNumber":"2","TestTypeId":1}

我尝试使用邮递员休息客户端并获得响应,但在程序中收到错误请求

0 个答案:

没有答案