獲取網路json檔案
阿新 • • 發佈:2018-12-01
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;
};