[Android]快递查询——API的简单应用
16lz
2021-01-26
最近接触到android的网络请求、获取数据等方面,于是乎做了一个小demo
展示:
API:全国快递查询
主要分为4个接口:快递公司列表、快递单号查询、网点查询、快递公司查询
我的demo里主要用到快递公司列表和快递单号查询
快递公司列表:可直接获取快递公司名称、简称、电话等信息
快递单号查询:给出快递公司简称、快递单号,返回快递信息
我的做法是先从网上把快递公司列表的信息拷贝下来存在raw里
网页上的信息:
保存到raw下
通过解析json文件来获取信息:
InputStream in = context.getResources().openRawResource(R.raw.company); int length = 0; JSONArray expressList = null; try { length = in.available(); byte[] buffer = new byte[length]; in.read(buffer); String text = new String(buffer,"utf-8");//防止乱码 JSONObject jsonObject= new JSONObject(text); JSONObject showapi_res_body = jsonObject.getJSONObject("showapi_res_body") ; expressList= showapi_res_body.getJSONArray("expressList"); } catch (IOException e) { e.printStackTrace(); } catch (JSONException e) { e.printStackTrace(); } for(int i=0;i
输入自动提示:
edittext_name = (AutoCompleteTextView)findViewById(R.id.edittext_name); ArrayAdapter arrayAdapter;//输入提示 arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_dropdown_item_1line, list); edittext_name.setAdapter(arrayAdapter);
快递单号查询:
public GetContext(String url){//给定访问的网址,返回网址内容 HttpURLConnection connection=null; try{ URL myurl = new URL(url); connection =(HttpURLConnection) myurl.openConnection(); connection.setRequestMethod("POST"); connection.setDoInput(true); connection.setDoOutput(true); String responseCode = connection.getResponseMessage(); connection =(HttpURLConnection) myurl.openConnection(); connection.setRequestMethod("GET"); connection.setConnectTimeout(8000); connection.setReadTimeout(8000); InputStream in=connection.getInputStream(); BufferedReader reader= new BufferedReader(new InputStreamReader(in)); response=new StringBuilder(); String line; while((line=reader.readLine())!=null){ response.append(line); //获取信息存放在response里 } }catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
再用到json解析获取需要的数据(略,跟获取公司列表的json解析类似)
项目下载地址:https://github.com/kukajenny/QueryExpress
更多相关文章
- ReactNative学习笔记之调用原生模块(进阶)之Callback、Promise使用
- 个人解读Activity之四-Part1
- Android的内外存储、SharedPreferences(偏好设置)及获取空间使用情
- Android中获取手机IMEI,IMSI, MAC(Android(安卓)6.0、支持4G环境获
- Android(安卓)studio 获取每次编译apk时的日期
- Android(安卓)获取当前地理位置信息
- Android调试器:获取非Root手机的应用数据库
- android 根据文件Uri获取图片url
- Android(安卓)Studio 基础 之 简单获取手机端所有图片