android 网络请求 NetWork

android 网络请求 NetWork
public static String getJson(String urlString){
try {
URL url = new URL (urlString);
//得到connection对象。
HttpURLConnection httpURLConnection= (HttpURLConnection) url.openConnection();
//得到响应码
int responseCode = httpURLConnection.getResponseCode ( );
if (responseCode==200){
//得到响应流
InputStream inputStream = httpURLConnection.getInputStream ( );
//相当于字符
String temp;
BufferedReader bufferedReader = new BufferedReader (new InputStreamReader (inputStream));
while ((temp=bufferedReader.readLine ())!=null){
StringBuffer stringBuffer = new StringBuffer ( );
stringBuffer.append (temp);
return stringBuffer.toString ();
}
}

} catch (Exception e) {
    e.printStackTrace ( );
}
return "";

}

猜你喜欢

转载自blog.csdn.net/qq_42609613/article/details/85344931