通过HttpClient获取服务器接口json数据已及解析
2015-11-12 16:17
901 查看
1.获取服务器接口数据方法:记得导入httpclient包
http://download.csdn.net/detail/china1988s/3791514public static String getdata(String url, List<NameValuePair> params) { String tag = ""; HttpClient httpClient = new DefaultHttpClient(); HttpPost post = new HttpPost(url); try { UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params,"utf-8"); post.setEntity(entity); try { HttpResponse httpResponse = httpClient.execute(post); tag = "get string false"; if (httpResponse.getStatusLine().getStatusCode() == 200) { HttpEntity entity2 = httpResponse.getEntity(); tag=EntityUtils.toString(entity2); //tag = Decodtool.decodeUnicode(URLDecoder.decode(EntityUtils.toString(entity2, "uft-8"), "utf-8")); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return tag; }
2.通过Gson解析:记得导入Gson包
public static void main(String[] args) { String data = getdata(); System.out.println(parase(data).toString()); } private static String getdata() { String url = "http://192.168.2.171:9898/Service_Resource.ashx"; ArrayList<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("method", "GetAll")); return NetworkUtil.getdata(url, params); } private static ArrayList<ResourceBean> parase(String data) { Gson gson = new Gson(); JSONObject jsonObject = JSONObject.fromObject(data); String datapart = jsonObject.getString("data"); Type type = new TypeToken<ArrayList<ResourceBean>>() {}.getType(); ArrayList<ResourceBean> resourcebeans = gson.fromJson(datapart, type); return resourcebeans; }
相关文章推荐
- iOS9适配之Http请求失败
- 使用HttpURLConnection实现在android客户端和服务器之间传递对象
- 使用HttpURLConnection实现在android客户端和服务器之间传递对象
- 使用HttpURLConnection实现在android客户端和服务器之间传递对象
- 使用HttpURLConnection实现在android客户端和服务器之间传递对象
- Http协议详解(转)
- iOS AHTTPSessionManger下载功能
- java从http url下载文件
- 踪视通惊艳亮相首届网络实时通信大会, 完美技术引爆全场
- iOS 解决iOS 9下的http请求发送失败问题
- android webview加载网络连接
- 使用Fiddler进行IOS APP的HTTP抓包
- 网络后台开发面试题
- 【转】HttpClient 教程 (一)
- muduo库阅读(40)——Net部分:TCP服务器TcpServer
- http headers
- socket发送http报文的疑惑(求高手指点一二)
- Wireshark工具——网络抓包
- 【转】RTMP/RTP/RTSP/RTCP协议对比与区别介绍
- Android网络通信篇:Socket TCP(简易聊天室)