Android之智慧北京二

    xiaoxiao2024-07-24  12

    Android之智慧北京二

    Http

    Requst (请求)

    GET /zhbj/categories.json HTTP/1.1 Host: localhost:8080 Connection: keep-alive Cache-Control: max-age=0 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36 Accept-Encoding: gzip, deflate, sdch Accept-Language: zh-CN,zh;q=0.8,en;q=0.6 RA-Ver: 2.8.8 RA-Sid: 74195216-20150210-033823-d1a3bf-9cedaa If-None-Match: W/"1171-1412422055000" If-Modified-Since: Sat, 04 Oct 2014 11:27:35 GMT name=xxx&pwd=xxx {json:} 请求消息行 : 请求方式,请求的url,http版本请求消息头 : key-value请求实体内容:

    response (响应)

    HTTP/1.1 304 Not Modified Server: Apache-Coyote/1.1 ETag: W/"1171-1412422055000" Date: Mon, 09 Mar 2015 01:49:42 GMT {"retcode":200,"data":[{"id":10000,"title":"新闻","type":1,"children":[{"id":10007,"title":"北京","type":1,"url":"/10007/list_1.json"},{"id":10006,"title":"中国","type":1,"url":"/10006/list_1.json"},{"id":10008,"title":"国际","type":1,"url":"/10008/list_1.json"},{"id":10010,"title":"体育","type":1,"url":"/10010/list_1.json"},{"id":10091,"title":"生活","type":1,"url":"/10091/list_1.json"},{"id":10012,"title":"旅游","type":1,"url":"/10012/list_1.json"},{"id":10095,"title":"科技","type":1,"url":"/10095/list_1.json"},{"id":10009,"title":"军事","type":1,"url":"/10009/list_1.json"},{"id":10093,"title":"时尚","type":1,"url":"/10093/list_1.json"},{"id":10011,"title":"财经","type":1,"url":"/10011/list_1.json"},{"id":10094,"title":"育儿","type":1,"url":"/10094/list_1.json"},{"id":10105,"title":"汽车","type":1,"url":"/10105/list_1.json"}]},{"id":10002,"title":"专题","type":10,"url":"/10006/list_1.json","url1":"/10007/list1_1.json"},{"id":10003,"title":"组图","type":2,"url":"/10008/list_1.json"},{"id":10004,"title":"互动","type":3,"excurl":"","dayurl":"","weekurl":""}],"extend":[10007,10006,10008,10014,10012,10091,10009,10010,10095]} 响应消息行: http版本, 响应状态码,响应的描述响应的消息头: key-value响应的内容 :

    Gson的使用\先把gson-2.2.1.jar拷贝到lib下

    json:

    key-valuekey 永远是Stringvalue: 集合,数字,String, boolean

    Gson:(google)

    Gson 类的使用: toJson: 将java对象变成 json的stringfromJson: 将json串转化为java对象

    1、解析json串数据变成一个javabean对象存储数据,jsonString为服务器返回的字符串

    // 1、解析json串 Gson gson = new Gson(); NewsListBean bean = gson.fromJson(jsonString, NewsListBean.class);

    访问服务器ip的设置

    在虚拟机中设置:

    //服务器地址 //服务器地址,本机ip、虚拟ip String SERVICE_URL="http://10.207.116.24:8888/zhbj"; String NEWS_CENTER_URL=SERVICE_URL+"/categories.json";

    10.207.116.24:是联网的本机ip,8888是设置的端口号,categories.json是文件名

    开源框架 xUtils的使用(部分)

    网络请求访问模块

    1、

    //通过网络获取数据,加载进来 HttpUtils utils=new HttpUtils(); utils.send(HttpMethod.GET, Constans.NEWS_CENTER_URL, new RequestCallBack<String>(){ //访问网络成功 @Override public void onSuccess(ResponseInfo<String> responseInfo) { //取出结果值 String result=responseInfo.result; Log.i(TAG, "访问成功"+result); } //访问网络失败 @Override public void onFailure(HttpException error, String msg) { //打印栈错误信息,使用第三方开源库需要打印,不然看不到错误信息 error.printStackTrace(); Log.i(TAG, "访问失败"+msg); }});

    2、

    //通过网络获取数据,加载进来 HttpUtils utils=new HttpUtils(); RequestParams params=new RequestParams(); //消息头 params.addHeader("", ""); //1、请求参数 //post NameValuePair pair=new BasicNameValuePair("", ""); params.addBodyParameter(pair); //get NameValuePair pair=new BasicNameValuePair("", ""); params.addQueryStringParameter(pair); utils.send(HttpMethod.GET, Constans.NEWS_CENTER_URL, params, new RequestCallBack<String>(){ //访问网络成功 @Override public void onSuccess(ResponseInfo<String> responseInfo) { //取出结果值 String result=responseInfo.result; Log.i(TAG, "访问成功"+result); processJson( result); } //访问网络失败 @Override public void onFailure(HttpException error, String msg) { //打印栈错误信息 error.printStackTrace(); Log.i(TAG, "访问失败"+msg); }});

    2、注入模块的使用

    1、对id注入的写法

    @ViewInject(R.id.news_tab_indicator) private TabPageIndicator tabPagerIndicator; @ViewInject(R.id.news_center_pager) private ViewPager mViewPager; //在这里可以直接使用了,不用再写findViewById()了 View view=View.inflate(mContext, R.layout.news_center_tab, null); //使用ViewUtils注入 ViewUtils.inject(mContext, view); return view;

    2、对控件点击事件使用注入的写法

    @OnClick(R.id.news_arrow) public void onClick(View view){ //点击箭头到下一个 int item=mViewPager.getCurrentItem(); mViewPager.setCurrentItem(++item); }

    加载图片模块: BitmapUtils

    加载图片的功能也比较简单,首先在初始化中创建一个BitmapUtils出来

    BitmapUtils mBitmapUtils = new BitmapUtils(context);

    在需要加载图片的地方调用display(View ,url)这个方法就可以了。View是承载图片的控件例如:ImageView

    这只是Bitmap的一个加载图片数据的简单方法,里面还封装了很多方法,想了解更多的就去看看xUtils中BitmapUtils模块的源码吧。

    开源框架 ViewPagerIndicator的使用

    1、开源框架中有六种样式,想了解更多的的去看看开源库吧!

    主要介绍的是tab与viewpager的搭配时用。

    转载请注明原文地址: https://ju.6miu.com/read-1290974.html
    最新回复(0)