JAVA调用第三方接口

import net.sf.json.JSONArray;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

//参考:https://blog.csdn.net/sinat_21946155/article/details/51144945?spm=1001.2101.3001.6661.1&utm_medium=distribute.pc_relevant_t0.none-task-blog-2%7Edefault%7ECTRLIST%7Edefault-1.essearch_pc_relevant&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-2%7Edefault%7ECTRLIST%7Edefault-1.essearch_pc_relevant

public class gateTest {
public static void main(String[] args){
    String url = "https://data.gateapi.io/api2/1/pairs";

    System.out.println("URL:"+url);
    StringBuffer json = new StringBuffer();
    try {
        //实例一个URL和URLConnection
        URL oracle = new URL(url);
        //打开链接
        URLConnection yc = oracle.openConnection();
        //输入流作参数传进InputStreamReader并用BufferedReader接受
        BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
        String inputLine = null;
        //一直读到空,并设置流程编码是UTF8
        while ((inputLine = in.readLine()) !=null){
            json.append(new String(inputLine.getBytes(),"GBK"));
        }
        in.close();
    } catch (Exception e){
        e.printStackTrace();
    }
    try {
        JSONArray jn = JSONArray.fromObject(json.toString());
//            if(jn.size()>0){
//                for (int i=0;i

你可能感兴趣的:(JAVA调用第三方接口)