RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 826151
Accepted
User500S
User500S
Asked:2020-05-11 06:00:52 +0000 UTC2020-05-11 06:00:52 +0000 UTC 2020-05-11 06:00:52 +0000 UTC

Java 中的 JSON 请求

  • 772

需要编写代码来通过 JSON 测试 API。

1)您需要向服务器发出请求,并包括标头(headers)以及用户名和密码的信息。

2)您需要读取服务器的响应并确认用户已登录。

3) 之后,需要向服务器发送一块信息进行处理,并检查是否已经处理完毕。

我是新手,以前没有遇到过。我什至不知道从哪里开始。我尝试使用 Json 之类的库,但没有任何效果,我不明白该怎么做。如果有人知道告诉我或帮助代码。

谢谢你。

java
  • 2 2 个回答
  • 10 Views

2 个回答

  • Voted
  1. Best Answer
    Antonio112009
    2020-05-11T10:43:33Z2020-05-11T10:43:33Z

    如果答案不完整/不理解/不是您需要的 - 在评论中写下

    1) 好的 JSON 视频 - YouTube 链接(俄语)

    2) 示例POST和GET,我将其作为“从交易所提取数据”的基础:

    import java.io.BufferedReader;
    import java.io.DataOutputStream;
    import java.io.InputStreamReader;
    import java.net.HttpURLConnection;
    import java.net.URL;
    
    import javax.net.ssl.HttpsURLConnection;
    
    public class HttpURLConnectionExample {
    
        private final String USER_AGENT = "Mozilla/5.0";
    
        public static void main(String[] args) throws Exception {
    
            HttpURLConnectionExample http = new HttpURLConnectionExample();
    
            System.out.println("Testing 1 - Send Http GET request");
            http.sendGet();
    
            System.out.println("\nTesting 2 - Send Http POST request");
            http.sendPost();
    
        }
    
        // HTTP GET request
        private void sendGet() throws Exception {
    
            String url = "https://api.kraken.com/0/public/Trades?pair=XXBTZEUR&since=0";
    
            URL obj = new URL(url);
            HttpURLConnection con = (HttpURLConnection) obj.openConnection();
    
            // optional default is GET
            con.setRequestMethod("GET");
    
            //add request header
            con.setRequestProperty("User-Agent", USER_AGENT);
    
            int responseCode = con.getResponseCode();
            System.out.println("\nSending 'GET' request to URL : " + url);
            System.out.println("Response Code : " + responseCode);
    
            BufferedReader in = new BufferedReader(
                    new InputStreamReader(con.getInputStream()));
            String inputLine;
            StringBuffer response = new StringBuffer();
    
            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();
    
            //print result
            System.out.println(response.toString());
    
        }
    
        // HTTP POST request
        private void sendPost() throws Exception {
    
            String url = "https://api.kraken.com/0/public/Ticker?pair=XBTCZUSD";
            URL obj = new URL(url);
            HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
    
            //add request header
            con.setRequestMethod("POST");
            con.setRequestProperty("User-Agent", USER_AGENT);
            con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
    
            String urlParameters = "{\"pair\":{\"XBTEUR\"}";
    
            // Send post request
            con.setDoOutput(true);
            DataOutputStream wr = new DataOutputStream(con.getOutputStream());
            wr.writeBytes(urlParameters);
            wr.flush();
            wr.close();
    
            int responseCode = con.getResponseCode();
            System.out.println("\nSending 'POST' request to URL : " + url);
            System.out.println("Post parameters : " + urlParameters);
            System.out.println("Response Code : " + responseCode);
    
            BufferedReader in = new BufferedReader(
                    new InputStreamReader(con.getInputStream()));
            String inputLine;
            StringBuffer response = new StringBuffer();
    
            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();
    
            //print result
            System.out.println(response.toString());
    
        }
    
    }
    
    • 2
  2. zolt
    2020-05-11T10:22:46Z2020-05-11T10:22:46Z
    • 了解什么是 HTTP
    • 然后我们了解什么是 JSON
    • 然后我们看看如何在Java中生成并执行一个HTTP请求
    • ??????
    • 利润!

    向Apache HttpClient、Jackson / GSON 学习

    我希望你知道如何编写一个简单的 Java 控制台程序?

    • 1

相关问题

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    是否可以在 C++ 中继承类 <---> 结构?

    • 2 个回答
  • Marko Smith

    这种神经网络架构适合文本分类吗?

    • 1 个回答
  • Marko Smith

    为什么分配的工作方式不同?

    • 3 个回答
  • Marko Smith

    控制台中的光标坐标

    • 1 个回答
  • Marko Smith

    如何在 C++ 中删除类的实例?

    • 4 个回答
  • Marko Smith

    点是否属于线段的问题

    • 2 个回答
  • Marko Smith

    json结构错误

    • 1 个回答
  • Marko Smith

    ServiceWorker 中的“获取”事件

    • 1 个回答
  • Marko Smith

    c ++控制台应用程序exe文件[重复]

    • 1 个回答
  • Marko Smith

    按多列从sql表中选择

    • 1 个回答
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Suvitruf - Andrei Apanasik 什么是空? 2020-08-21 01:48:09 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5