gpt4 book ai didi

java - 发布请求Json,代码已完成但不起作用

转载 作者:太空宇宙 更新时间:2023-11-04 09:18:24 24 4
gpt4 key购买 nike

我想创建一个带有“json”键的 POST-Request 并使用“getorders”命令来获取所有订单。

使用我的代码我得到返回:

{
"getorders"
}
POST Response Code : 200
POST Response Message : OK

我做错了什么?我的命令是假的吗?首先,我创建与 URL 的连接,然后创建请求。之后我写了一个回复。所以“语法”应该很好,我唯一能想到的就是命令。

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;

import org.json.JSONArray;
import org.json.JSONObject;

public class JSONTester {

private static String dirPath = ;

public static void main(String[] args) {
// TODO Auto-generated method stub
File file = new File(dirPath + "//array_complex.json");
try {
final String POST_PARAMS = "{\n" + "\"getorders\" \r\n" + "\n}";
System.out.println(POST_PARAMS);
URL obj = new URL();
HttpURLConnection postConnection = (HttpURLConnection) obj.openConnection();
postConnection.setRequestMethod("POST");
postConnection.setRequestProperty("connection", "Keep-Alive");
postConnection.setDoOutput(true);
java.io.OutputStream os = postConnection.getOutputStream();
os.write(POST_PARAMS.getBytes());
os.flush();
os.close();
int responseCode = postConnection.getResponseCode();
System.out.println("POST Response Code : " + responseCode);
System.out.println("POST Response Message : " + postConnection.getResponseMessage());
if (responseCode == HttpURLConnection.HTTP_CREATED) { //success
BufferedReader in = new BufferedReader(new InputStreamReader(postConnection.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
// print result
System.out.println(response.toString());
}
} catch (Exception e) {
System.out.println();
}
}
}

最佳答案

您的代码有一个错误的假设:

    if (responseCode == HttpURLConnection.HTTP_CREATED) { //success

这是不正确的,responseCode 是 200,而 HTTP_CREATED 是 201。如果您将其更改为

    if (responseCode == HttpURLConnection.HTTP_OK) { //success

您的代码将进入 if block 并读取 inputStream。使用 HTTP_OK 运行代码后我得到:

POST Response Code :  200
POST Response Message : OK
{"error":true,"errortext":"field \u00b4json\u00b4 not found in post"}

也许您应该阅读 API,了解如何调用此服务、需要哪些参数等。

更新:

我尝试了不同的 json 消息,我理解了“key“json”和命令“getorders”的含义,所以我想出了以下内容:

final String POST_PARAMS = "json={\n" + "\"key\":\"getorders\" \r\n" + "\n}";

以上返回:

POST Response Code :  200
POST Response Message : OK
{"error":true,"errortext":"no userid found"}

那么剩下的就是添加缺少的 json 字段,或者读取此服务的 API 并将字段和值添加到 POST_PARAMS。

更新2:

在您发表评论后,我设法使用以下方式获得订单

 final String POST_PARAMS = "json={\"bid\":\"bid\", \"getorder\":\"1\"}";

我尝试使用 userId/userID/userid 设置出价,但没有成功,我不确定 bid:bid 是否正确,但它有效,我想您从现在开始就明白了。

结果:

POST Response Code :  200
POST Response Message : OK
{"Bestellnummer":"1","Besteller":"8195529","Zeit":"2019-09-27 15:50:07","Artikel":[{"Artikelnummer":"76194","Anzahl":"1","Preis":"2.968"},{"Artikelnummer":"61681","Anzahl":"1","Preis":"7.147"},{"Artikelnummer":"111756","Anzahl":"1","Preis":"9.29"},{"Artikelnummer":"14227","Anzahl":"1","Preis":"0"}]}

关于java - 发布请求Json,代码已完成但不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58661555/

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