最近接触到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


更多相关文章

  1. ReactNative学习笔记之调用原生模块(进阶)之Callback、Promise使用
  2. 个人解读Activity之四-Part1
  3. Android的内外存储、SharedPreferences(偏好设置)及获取空间使用情
  4. Android中获取手机IMEI,IMSI, MAC(Android(安卓)6.0、支持4G环境获
  5. Android(安卓)studio 获取每次编译apk时的日期
  6. Android(安卓)获取当前地理位置信息
  7. Android调试器:获取非Root手机的应用数据库
  8. android 根据文件Uri获取图片url
  9. Android(安卓)Studio 基础 之 简单获取手机端所有图片

随机推荐

  1. 在PyQt5里面玩Android(安卓)的Material D
  2. Android(安卓)so逆向基本知识总结
  3. TensorFlow模型移植Android识别手写数字(M
  4. Android下新增JNI过程中启动新增系统服务
  5. Intent传递集合数据
  6. AndroidJNI实践(1)-使用.h头文件-静态注
  7. Android(安卓)ConstraintLayout
  8. Android(安卓)自定义圆环倒计时控件
  9. android:点击popupwindow以外区域 popupwi
  10. android帐号管理token问题