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

时间:2019-11-01 15:11:50

标签: java json

我想使用键“ json”创建POST请求,并使用命令“ 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();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您的代码有错误的假设:

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

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

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

您的代码将进入if块并读取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"}]}