gpt4 book ai didi

java - 在Java中以键值对的形式传递json数组

转载 作者:行者123 更新时间:2023-11-30 07:06:05 26 4
gpt4 key购买 nike

我正在尝试使用 Java 中的 REST Web 服务发送数据。我可以在 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 错误消息

最佳答案

我认为您错过了 header 中的一些身份验证 token 。类似于: httpURLConnection.setRequestProperty("tokenKey", "yourAuthToken");

关于java - 在Java中以键值对的形式传递json数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40108402/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com