path : 网址
返回数据字符串
//注意编码方式
//该函数调用是在新线程调用不要在GUI线程调用 因为while 你懂的
//获取JSON数据
public static String getJsonByInternet(String path){
try {
URL url = new URL(path.trim());
//打开连接
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
if(200 == urlConnection.getResponseCode()){
//得到输入流
InputStream is =urlConnection.getInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while(-1 != (len = is.read(buffer))){
baos.write(buffer,0,len);
baos.flush();
}
return baos.toString("utf-8");
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}