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数据获取的部分内容,应该算是与后台通信最基本的内容

阅读全文
0 0