try {
URL url = new URL(path);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//设置
connection.setRequestMethod("GET");
connection.setReadTimeout(5000);
connection.setConnectTimeout(5000);
//获取
int responseCode = connection.getResponseCode();
Log.d("qqq","+++++++++++"+responseCode);
if (responseCode == 200){
InputStream inputStream = connection.getInputStream();
String json = streamToString(inputStream, "utf-8");
//返回
return json;
}else if(responseCode==301 || responseCode==302){
//获取重定向的url
String newurl=connection.getHeaderField("location");
Log.d("qqq","newurl:"+newurl);
//在次进行请求
URL url1 = new URL(newurl);
HttpURLConnection connection1 = (HttpURLConnection) url1.openConnection();
//设置
connection1.setRequestMethod("GET");
connection1.setReadTimeout(5000);
connection1.setConnectTimeout(5000);
//获取
int responseCode1 = connection1.getResponseCode();
Log.d("qqq","+++++++++++"+responseCode1);
if (responseCode1 == 200){
InputStream inputStream = connection1.getInputStream();
String json = streamToString(inputStream, "utf-8");
//返回
return json;
}
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
URL url = new URL(path);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//设置
connection.setRequestMethod("GET");
connection.setReadTimeout(5000);
connection.setConnectTimeout(5000);
//获取
int responseCode = connection.getResponseCode();
Log.d("qqq","+++++++++++"+responseCode);
if (responseCode == 200){
InputStream inputStream = connection.getInputStream();
String json = streamToString(inputStream, "utf-8");
//返回
return json;
}else if(responseCode==301 || responseCode==302){
//获取重定向的url
String newurl=connection.getHeaderField("location");
Log.d("qqq","newurl:"+newurl);
//在次进行请求
URL url1 = new URL(newurl);
HttpURLConnection connection1 = (HttpURLConnection) url1.openConnection();
//设置
connection1.setRequestMethod("GET");
connection1.setReadTimeout(5000);
connection1.setConnectTimeout(5000);
//获取
int responseCode1 = connection1.getResponseCode();
Log.d("qqq","+++++++++++"+responseCode1);
if (responseCode1 == 200){
InputStream inputStream = connection1.getInputStream();
String json = streamToString(inputStream, "utf-8");
//返回
return json;
}
}
} catch (Exception e) {
e.printStackTrace();
}
return null;