Android开发--与后台通信(一)--API数据获取
来源:互联网 发布:大数据目前发展情况 编辑:程序博客网 时间:2024/05/22 05:23
API网络请求
对于之前写的2篇关于API的内容和JSON文本解析类,当然需要有着配套的网络请求配合安卓端请求和获取数据,这篇博客介绍与后台通信中网络的基本请求与API数据有关
以下是相关的博客
JSON文本解析:http://blog.csdn.net/qq_34861102/article/details/76762135
常用的API接口:http://blog.csdn.net/qq_34861102/article/details/76755882
相关地址
代码下载地址(奇怪为什么现在上传需要积分,没有积分的留一下邮箱就好):http://download.csdn.net/detail/qq_34861102/9924780
原文地址:http://blog.csdn.net/qq_34861102/article/details/76906330
项目地址:https://github.com/Outliwer/Android-App
准备
这里我们使用现在封装得比较好的包:
okhttp
在配置文件中加入依赖:compile 'com.squareup.okhttp3:okhttp:3.4.1'
如图:
代码编写
首先很重要的一点:Android网络请求因为有延迟,必须要在多线程中进行
先看一个整个的请求代码:
final String address = "http://route.showapi.com/268-1?showapi_appid=42305&keyword="+ searchWord +"&proId=&cityId=&areaId=&page=&showapi_sign=c069130238404775894ba39caf3ac094"; mView = new CatLoadingView(); mView.show(getSupportFragmentManager(), ""); new Thread(new Runnable() { @Override public void run() { try { OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder() .url(address) .build(); Response response = client.newCall(request).execute(); responseData = response.body().string(); Message message = new Message(); message.what = UPDATE_TEXT; mHandler.sendEmptyMessage(message.what); } catch (IOException e) { e.printStackTrace(); } } }).start(); }
可以看到:
这里的
responseData
就是网络请求返回的结果,接下来可以对这个结果进行一个JSON文本的解析同时可以看到:
Message message = new Message();message.what = UPDATE_TEXT;mHandler.sendEmptyMessage(message.what);
这是一个异步消息处理机制,刚才有说到,网络请求在子线程完成,如果想要做到在主线程做出一些修改UI的操作,就需要这个异步消息处理
上面代码是消息的发出,我们看一下消息的获取和相关操作:
protected Handler mHandler = new Handler(){ @Override public void handleMessage(Message msg) { switch (msg.what){ case UPDATE_TEXT: Log.e("responseData----",""+responseData); Gson gs = new Gson(); entity= gs.fromJson(responseData,entityClass); String code= entity.getShowapi_res_code(); SearchBodyEntity showapi_res_body = entity.getShowapi_res_body(); String retCode = showapi_res_body.getRet_code(); SearchBeanEntity pagebean = showapi_res_body.getPagebean(); Log.e("pagebean----",""+pagebean); Log.e("pagebeangetAllPages----",""+pagebean.getAllPages()); Log.e("getContentList----",""+pagebean.getContentlist()); List<SearchListEntity> contentList = pagebean.getContentlist(); if (contentList.size() != 0) { Log.e("contentList----", "" + contentList); SearchListEntity[] contentlist_objects = new SearchListEntity[contentList.size()]; location[] locations = new location[contentList.size()]; String[] lons = new String[contentList.size()]; String[] lats = new String[contentList.size()]; String[] names = new String[contentList.size()]; String[] summarys = new String[contentList.size()]; String[] contents = new String[contentList.size()]; String[] attentions = new String[contentList.size()]; String[] opentimes = new String[contentList.size()]; String[] coupons = new String[contentList.size()]; String[] my_addresss = new String[contentList.size()]; String[] picture = new String[contentList.size()]; for (int i = 0; i < contentList.size(); i++) { SearchListEntity contentlist_object = contentList.get(i); contentlist_objects[i] = contentlist_object; location location = contentlist_object.getLocation(); locations[i] = location; String lon = location.getLon(); lons[i] = lon; String lat = location.getLat(); lats[i] = lat; String name = contentlist_object.getName(); names[i] = name; String summary = contentlist_object.getSummary(); summarys[i] = summary; String content = contentlist_object.getContent(); contents[i] = content; String attention = contentlist_object.getAttention(); attentions[i] = attention; String opentime = contentlist_object.getOpentime(); opentimes[i] = opentime; String coupon = contentlist_object.getCoupon(); coupons[i] = coupon; String my_address = contentlist_object.getAddress(); my_addresss[i] = my_address; List<picList> picList = contentlist_object.getPicList(); picture[i] = picList.get(0).getPicUrl(); Log.e("", "" + picture[i]); } Intent intent = new Intent(SearchActivity.this, SearchDetailActivity.class); intent.putExtra("name", names); intent.putExtra("summary", summarys); intent.putExtra("my_address", my_addresss); intent.putExtra("opentime", opentimes); intent.putExtra("picture", picture); intent.putExtra("coupon", coupons); intent.putExtra("attention", attentions); intent.putExtra("content", contents); intent.putExtra("lon", lons); intent.putExtra("lat", lats); mView.dismiss(); startActivity(intent); }else { Toast.makeText(SearchActivity.this,"查无结果",Toast.LENGTH_LONG).show(); } break; } } };
这里对之前发出的消息做的一个处理,包含界面之间的跳转、JSON文本解析的操作
以上就是API数据获取的部分内容,应该算是与后台通信最基本的内容
- Android开发--与后台通信(一)--API数据获取
- Android开发,后台service与前台activity通信
- 一.angularJs——服务调用与后台数据获取
- Android获取服务器后台数据
- Android BLE与终端通信(一)——Android Bluetooth基础API以及简单使用获取本地蓝牙名称地址
- Android BLE与终端通信(一)——Android Bluetooth基础API以及简单使用获取本地蓝牙名称地址
- xmlhttprequest同步获取后台数据(一)
- aidl ( 一 ) activity获取后台service数据
- 后台数据结构与前台数据获取
- Android应用获取后台基于SSH框架开发的接口数据
- Javascript URL参数获取与调用后台API
- PHP与WCF通信数据获取
- PHP与WCF通信数据获取
- 小程序post提交获取数据,与服务器通信-小程序支付开发-视频教程5
- android内存性能数据获取--api篇
- android api返回的数据获取
- Android 获取网络api数据get请求
- echarts图表动态获取后台数据详解(一)
- HDU—3791 二叉搜索树 (可以用数组,,,,建树)
- SpringMVC
- 基于STM32+机智云物联网平台的家电状态显示及控制系统
- (14)问卷调查:两种方式className、div样式属性值改变、单选按钮性格测试
- git clone 遇到"unable to access '……':error setting certificate verify locations"问题
- Android开发--与后台通信(一)--API数据获取
- Js 执行上下文
- Java对象的序列化与反序列化那点事
- python爬虫----简单的抓取斗鱼弹幕
- Android View
- leetcode--Recover Binary Search Tree
- 【unity 5学习记录】 可编辑地形 网格 原理讲解 17.8.8
- CODE大全告诉你java是否开始没落了
- GAN公式原理推到