(一)HttpURLConnection之Get请求(解决乱码)

来源:互联网 发布:科比0506赛季每场数据 编辑:程序博客网 时间:2024/06/11 16:24
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 exceptione.printStackTrace();} finally {if (connection != null) {connection.disconnect();}if (in != null) {try {in.close();} catch (Exception e2) {// TODO: handle exceptione2.printStackTrace();}}}return result;}

0 0
原创粉丝点击