KKSU
2024-04-07 ee1667b3e8faa9c7f3cbc921ea7bb87c2e3f4358
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package cc.mrbird.febs.dapp.service.impl;
import okhttp3.*;
import com.google.gson.Gson;
import com.google.gson.JsonParser;
import java.io.IOException;
 
public class GraphQLClient {
 
    private static final String ENDPOINT = "https://bsc-mainnet.nodereal.io/v1/fcb1ae31845147dcabb183db57336218";
//    private static final String ENDPOINT = "https://open-platform.nodereal.io/fcb1ae31845147dcabb183db57336218/pancakeswap/graphql";
    private static final OkHttpClient client = new OkHttpClient();
    public static void main(String[] args) throws IOException {
 
        MediaType mediaType = MediaType.parse("application/json");
        RequestBody body = RequestBody.create(mediaType,
                "{\"id\":1," +
                        "\"jsonrpc\":\"2.0\"," +
                        "\"params\":[\"0xcebfd36e03bd80c7015cbad17effbc33d2923ff3\",\"pending\"]," +
                        "\"method\":\"eth_getBalance\"}");
        Request request = new Request.Builder()
                .url(ENDPOINT)
                .post(body)
                .addHeader("accept", "application/json")
                .addHeader("content-type", "application/json")
                .build();
 
//        Response response = client.newCall(request).execute();
        try {
            Response response = client.newCall(request).execute();
            if (response.isSuccessful()) {
                String responseBody = response.body().string();
                // 在这里处理响应体,比如解析JSON数据等
                System.out.println(responseBody); // 或者其他处理逻辑
            } else {
                // 处理请求失败的情况,比如输出错误信息、重试等
                System.err.println("Request failed with code: " + response.code());
            }
        } catch (IOException e) {
            // 处理网络异常、超时等异常情况
            e.printStackTrace(); // 或者其他异常处理逻辑
        }
    }
}