(一)HttpURLConnection之Get请求(解决乱码)
2014-08-27 16:46
281 查看
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 post 和GET 分别传递2个参数给后台,返回JSON 解决乱码问题
- HttpURLConnection请求后,返回数据出现乱码解决办法!
- android客户端通过Get方式提交参数给服务器,使用URL和HttpURLConnection实现,以及乱码问题解决
- 使用HttpURLConnection采用get方式请求数据-----乱码问题
- Java HttpURLConnection模拟请求Rest接口解决中文乱码问题
- Java HttpURLConnection模拟请求Rest接口解决中文乱码问题
- 转载---HttpUrlConnection发送post请求汉字出现乱码的一个解决方法及其原因
- android客户端通过Get方式提交参数给服务器,使用URL和HttpURLConnection实现,以及乱码问题解决
- 解决HttpURLConnection请求时传中文参数乱码问题
- HttpURLConnection 的get跟post请求
- 解决HttpURLConnection中文乱码问题
- httpUrlConnection用于post请求和get请求
- 解决HttpUrlConnection乱码问题
- 解决HttpUrlConnection乱码问题
- Android_HttpURLConnection_Get和Post请求
- 赵雅智_HttpURLConnection 的 get请求和post请求
- Http学习之使用HttpURLConnection发送post和get请求
- Http学习之使用HttpURLConnection发送post和get请求
- 关于HttpUrlConnection网络请求之返回结果的中文乱码原因的探索
- 用HttpURLConnection请求网页的时候会出现中文乱码