通过HttpClinet类用get方法获得远程接口返回的Json串
来源:互联网 发布:js string length 编辑:程序博客网 时间:2024/06/06 13:07
public class HttpClientUtil { public static String requestByGetMethod(String s) { CloseableHttpClient httpClient = HttpClients.createDefault(); StringBuilder entityStringBuilder = null; try { HttpGet get = new HttpGet(s); CloseableHttpResponse httpResponse = null; httpResponse = httpClient.execute(get); try { HttpEntity entity = httpResponse.getEntity(); entityStringBuilder = new StringBuilder(); if (null != entity) { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent(), "UTF-8"), 8 * 1024); String line = null; while ((line = bufferedReader.readLine()) != null) { entityStringBuilder.append(line + "/n"); } } } finally { httpResponse.close(); } } catch (Exception e) { e.printStackTrace(); } finally { try { if (httpClient != null) { httpClient.close(); } } catch (IOException e) { e.printStackTrace(); } } return entityStringBuilder.toString(); }}
public class TestUrl { public static void main(String args[]) { String url = "http://suggest.taobao.com/sug?code=utf-8&q=牛奶&callback=cb"; String res = null; try { res = HttpClientUtil.requestByGetMethod(url); JSONObject result = new JSONObject(); System.out.println(res); res = res.substring(res.indexOf("{"), res.indexOf("}") + 1); System.out.println(res); result = JSONObject.fromObject(res); String token = result.getString("result"); System.out.println(token); } catch (Exception e) { e.printStackTrace(); } }}
用淘宝的商品搜索接口搜索“牛奶”的运行结果:
[["牛奶杯","194640"],["牛奶棉","61327"],["牛奶棉毛线","12855"],["牛奶洗面奶","10614"],["牛奶粉","236940"],["牛奶糖","49320"],["牛奶片","56362"],["牛奶1l","11292"],["牛奶棉线","7820"],["牛奶 1212","1600"]]
0 0
- 通过HttpClinet类用get方法获得远程接口返回的Json串
- HttpUrlcon与HttpClinet(输入网络地址返回字符串,只实现Get方法)
- 通过Struts2在视图页面中获得get方法的返回值
- 通过HttpClient调用WebApi的Get、Post方法(返回类型限定为json格式)
- springmvc的controllerAPP接口JSON返回值乱码问题(get方法)
- http请求远程接口获得JSON数据(1) --- 利用HttpURLConnection发送post/get请求
- http请求远程接口获得Json数据(2) --- 利用HttpClient发送post/get请求
- 通过表名获得列名信息,返回json串
- java通过url调用远程接口返回json数据,有用户名和密码验证
- 接口返回json串
- java对远程接口方法的返回值处理
- ThinkPHP通过AJAX返回JSON的两种实现方法
- ThinkPHP通过AJAX返回JSON的两种实现方法
- Android通过Get方法获取Json数据
- GET获得json
- get获得json---解析
- 通过ip获得远程主机的MAC
- java访问远程接口返回json数据接收
- Android 使用ORMLite 操作数据库
- VC编译的除法的一段汇编代码解释
- 锚点平滑定位(js)
- 反调试技巧总结-原理和实现
- 各种分类算法的比较
- 通过HttpClinet类用get方法获得远程接口返回的Json串
- MapServer 之 发布网络覆盖数据服务(WCS-Web Coverage Service)
- 敏捷开发之Scrum扫盲篇
- JQuery中$.ajax()方法参数详解
- iOS9 HTTP请求失效
- 读取SSDT表和原函数地址
- webView后台播放音乐不能关闭的解决办法
- UIProgressView
- 42134123421