public static String executeHttpGet() {
String result = null;
URL url = null;
HttpURLConnection connection = null;
InputStreamReader in = null;
try {
url = new URL(
"http://api.showji.com/Locating/www.show.ji.c.o.m.aspx?m=18766581234&output=json&callback=querycallback×tamp=1409020493933");
connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("GET");
connection.setRequestProperty("Content-type", "text/html");
connection.setRequestProperty("Accept-Charset", "UTF-8");
connection.setRequestProperty("contentType", "UTF-8");
if(connection.getResponseCode()== HttpURLConnection.HTTP_OK){
in = new InputStreamReader(connection.getInputStream(),"UTF-8");
BufferedReader bufferedReader = new BufferedReader(in);
StringBuffer stringBuffer = new StringBuffer();
String line = null;
while ((line = bufferedReader.readLine()) != null) {
stringBuffer.append(line);
}
result = stringBuffer.toString();
}
else{
result="网络连接错误";
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
} finally {
if (connection != null) {
connection.disconnect();
}
if (in != null) {
try {
in.close();
} catch (Exception e2) {
// TODO: handle exception
e2.printStackTrace();
}
}
}
return result;
}
(一)HttpURLConnection之Get请求(解决乱码)
最新推荐文章于 2023-01-12 13:34:09 发布