Android之智慧北京二
 发布时间 
 阅读量: 
 阅读量 
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:}
    
         
         
         
         
         
         
         
         
         
         
         
         
         
         
         
    代码解读
        - 消息条目:获取方式、地址及HTTP协议。
 - 字段定义:键值对。
 - 实体内容:响应字段。
 
    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:
 
1. key-value
2. key 永远是String
3. value: 集合,数字,String, boolean 
        - Gson:(google)
 
1. Gson 类的使用:   
        toJson: 将Java对象编码为JSON字符串; fromJson: 解析JSON字符串为Java对象
- 将json串数据解析为javabean对象并存储到数据库中。
 - 将jsonString作为服务器返回的数据字符串处理。
 - 解析并生成javabean对象用于存储接收到的数据。
 - 将接收到的jsonString转换为javabean对象并将其存储到数据库中。
 
    // 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);
    
    
      
    
    代码解读
        当需要加载图片时被调用显示(View, url)此方法即可使用。其中View被设计为显示图像的组件。
此为一个Bitmap读取图片数据的简单方法,在内部还集成了很多功能,请直接查看xUtils中的BitmapUtils模块源代码。
开源框架 ViewPagerIndicator的使用
1、开源框架中有六种样式,想了解更多的的去看看开源库吧!
主要介绍的是tab与viewpager的搭配时用。
全部评论 (0)
 还没有任何评论哟~ 
