Android调用接口,获取并解析数据(json格式)
16lz
2021-01-25
Android中通过HttpPost、HttpResponse两个类,访问某一接口。
再使用JSONObject类,来从返回的json数据中解析出数据给Bean实体类。
不啰嗦,见方法块代码:
public static AboutModel getAboutMsg(Context ctx) {String requestUrl = WXConstants.getAboutMsgURL;try {HttpPost request = new HttpPost(requestUrl);HttpResponse httpResponse = new DefaultHttpClient().execute(request);if (httpResponse.getStatusLine().getStatusCode() == 200) {String retSrc = EntityUtils.toString(httpResponse.getEntity());JSONObject json = new JSONObject(retSrc);String qqMsg = json.getString("qqmsg");String url = json.getString("url");AboutModel model = new AboutModel();model.qqMsg = qqMsg;model.companyUrl = url;return model;}} catch (Exception e) {// TODO Auto-generated catch blockExceptionThrowUtil.ThrowException(ctx, e);e.printStackTrace();}return null;}
注意:网络访问要try-catch{}
注:解析的json如下
{"qqmsg":"客服QQ:111222333","url":"http://www.baidu.com"}
更多相关文章
- “罗永浩抖音首秀”销售数据的可视化大屏是怎么做出来的呢?
- Nginx系列教程(三)| 一文带你读懂Nginx的负载均衡
- 不吹不黑!GitHub 上帮助人们学习编码的 12 个资源,错过血亏...
- Android实现文件选择
- Android中打电话的数据流程分析
- android的正向传值与反向传值
- Android(安卓)与 SQLite
- android简单的多人聊天程序--借助Bmob后台数据库
- Android(安卓)使用Intent和Bundle传递数据及如何传递enum