欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

工具类

程序员文章站 2022-03-03 10:26:53
...

package zjq.com;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class Gjb {

public static String getJson(String urlString){

    try {
        URL url=new URL(urlString);

        try {
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

            int responseCode = urlConnection.getResponseCode();

            if (responseCode==200){

                InputStream inputStream = urlConnection.getInputStream();

                BufferedReader reader=new BufferedReader(new InputStreamReader(inputStream));

                StringBuilder builder=new StringBuilder();

                String a="";

                String b="";

                while ((a=reader.readLine())!=null){

                    b+=a;

                }

                return b;
            }

        } catch (IOException e) {
            e.printStackTrace();
        }

    } catch (MalformedURLException e) {
        e.printStackTrace();
    }

    return null;
}

}

相关标签: 工具类