在Java中的密钥值对中传递json数组

时间:2016-10-18 12:34:12

标签: java json rest

我尝试使用Java中的rest webservice发送数据。我可以在POST中使用Java发布数据,但现在我需要在rawModeData中发送数据,我还需要在surveysData键中发送JSONArray。

请查看随附的屏幕截图

enter image description here

这是我用来发送数据的代码

try {
        URL url = new URL(webServiceData.getSyncSurveyDataUrl());

        HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();

        // ConnectionType
        httpURLConnection.setRequestMethod("POST");
        httpURLConnection.setDoOutput(true);
        httpURLConnection.setDoInput(true);
        httpURLConnection.setUseCaches(false);
        httpURLConnection.setAllowUserInteraction(false);
        httpURLConnection.setRequestProperty("charset", "utf-8");
        httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

        // Create the output form content
        OutputStream out = httpURLConnection.getOutputStream();
        Writer writer = new OutputStreamWriter(out, "UTF-8");

        JSONObject objects = new JSONObject();

        objects.put("constituency", constituency);
        objects.put("longitude", longitude);
        objects.put("latitude", latitude);
        objects.put("createdOn", createdOn);

        for (Map.Entry<String, String> map : linkedHashMap.entrySet()) {
            objects.put(map.getKey(), map.getValue());
        }

        writer.write("createdByDeviceId");
        writer.write("=");
        writer.write(URLEncoder.encode(loginModel.getDeviceId(), "UTF-8"));
        writer.write("&");
        writer.write("createdByMobileNumber");
        writer.write("=");
        writer.write(URLEncoder.encode(loginModel.getMobileNumber(), "UTF-8"));
        writer.write("&");
        writer.write("state");
        writer.write("=");
        writer.write(URLEncoder.encode(loginModel.getLoggedInState(), "UTF-8"));
        writer.write("&");
        writer.write("eventId");
        writer.write("=");
        writer.write(URLEncoder.encode(loginModel.getEventId(), "UTF-8"));
        writer.write("&");
        writer.write("surveysData");
        writer.write("=");
        writer.write(URLEncoder.encode(objects.toString(), "UTF-8"));

        if (httpURLConnection.getResponseCode() != 200) {
            System.out.println("Exception in 200: " + httpURLConnection.getResponseCode());
            System.out.println("Exception Message: " + httpURLConnection.getResponseMessage());
            /*errorLabel.setText(httpURLConnection.getResponseMessage());
            errorLabel.setVisible(true);*/
        }
    } catch (Exception e) {
        return false;
    }

我收到401错误消息

2 个答案:

答案 0 :(得分:1)

我认为你错过了标题中的一些身份验证令牌。类似的东西:httpURLConnection.setRequestProperty(“tokenKey”,“yourAuthToken”);

答案 1 :(得分:0)

  • 您未连接到连接。另外我认为你需要在连接上设置setFixedLengthStreamingMode并将输出写为字节数组。例如:
//...
    String queryParameters = formatQueryParameters(requestData);
    byte[] output = queryParameters.getBytes(Charset.forName("UTF-8"));
    connection.setFixedLengthStreamingMode(output.length);
    connection.connect();
    DataOutputStream dataOutputStream = new DataOutputStream(connection.getOutputStream());
    dataOutputStream.write(output);
//...
public static String formatQueryParameters(Map<String, String> requestData) throws UnsupportedEncodingException {
    String equals = URLEncoder.encode("=", "UTF-8");
    String ampersand = URLEncoder.encode("&", "UTF-8");
    StringBuilder builder = new StringBuilder();

    for (Map.Entry<String, String> entry : requestData.entrySet()) {
        String encodedKey = URLEncoder.encode(entry.getKey(), "UTF-8");
        String encodedValue = URLEncoder.encode(entry.getValue(), "UTF-8");
        builder.append(encodedKey).append(equals).append(encodedValue).append(ampersand);
    }
    builder.deleteCharAt(builder.lastIndexOf(ampersand));
    return builder.toString();
}