获取网络json文件

public static String getjson(String urlString){
try {
//url对象封装接口字符串
URL url = new URL(urlString);
//用url打开连接, 返回值我们用HttpURLConnection
HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
//获取状态码
int responseCode = urlConnection.getResponseCode();
if (responseCode == 200) {
InputStream inputStream = urlConnection.getInputStream();
InputStreamReader streamReader = new InputStreamReader(inputStream);
BufferedReader reader=new BufferedReader(streamReader);
//可拼接的字符串
String start="";
String end="";
while ((start=reader.readLine())!=null){
end+=start;
}
return end;

        }

    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
};

猜你喜欢

转载自blog.csdn.net/qq_41423726/article/details/84671453