码迷,mamicode.com
首页 > Web开发 > 详细

NoHttp的使用

时间:2017-04-24 21:19:08      阅读:594      评论:0      收藏:0      [点我收藏+]

标签:影响   activity   store   ogg   final   htm   get请求   依赖   als   


1.添加依赖: compile ‘com.yanzhenjie.nohttp:nohttp:1.1.1‘


2.注册初始化
  NoHttp.initialize(this);
        Logger.setDebug(true);// 开启NoHttp的调试模式, 配置后可看到请求过程、日志和错误信息。上线后改为false   不然影响性能
        Logger.setTag("NoHttpSample");// 设置NoHttp打印Log的tag。

3.Nohttp的get请求
    RequestQueue requestQueue = NoHttp.newRequestQueue();
        Request<String> request = NoHttp.createStringRequest("http://c.m.163.com/nc/article/headline/T1348647909107/" + START + "-" + SUM + ".html", RequestMethod.GET);
        requestQueue.add(0, request, new OnResponseListener<String>() {
            @Override
            public void onStart(int what) {

            }

            @Override
            public void onSucceed(int what, Response<String> response) {
                String json = response.get();//得到请求数据
              
            }

            @Override
            public void onFailed(int what, Response<String> response) {

            }

            @Override
            public void onFinish(int what) {

            }
        });

4.Nohttp请求图片
  final Request<Bitmap> request = NoHttp.createImageRequest("https://ps.ssl.qhimg.com/sdmt/166_135_100/t01b77a38c118a9967e.jpg", RequestMethod.GET);
        requestQueue.add(2, request, new OnResponseListener<Bitmap>() {
            @Override
            public void onStart(int what) {

            }

            @Override
            public void onSucceed(int what, Response<Bitmap> response) {
                Bitmap bitmap = response.get();
                Log.e("ABC",bitmap.toString());
                icon.setImageBitmap(bitmap);

            }

            @Override
            public void onFailed(int what, Response<Bitmap> response) {

            }

            @Override
            public void onFinish(int what) {

            }
        });

5.Nohttp的post请求
  Request<String> request = NoHttp.createStringRequest("http://apicloud.mob.com/appstore/horoscope/day", RequestMethod.POST);
        request.add("key", "1d120467e646b");
        request.add("date", "1994-11-09");
        request.add("hour", "1");
        requestQueue.add(1, request, MainActivity.this);
 
    @Override
    public void onStart(int what) {

    }
 
    @Override
    public void onSucceed(int what, Response<String> response) {
        String json = response.get();
        switch (what){
            case 0:
                Log.e("TAG","get:::"+json);
                show.setText(json);
                break;
            case 1:
                Log.e("TAG","post:::"+json);
                show.setText(json);
                break;
        }
    }

    @Override
    public void onFailed(int what, Response<String> response) {

    }
   
    @Override
    public void onFinish(int what) {

    }

6.NoHttp网络请求只包含四个方法:
1.开始请求
2.得到数据
3.得到请求数据
4.请求结束

NoHttp的使用

标签:影响   activity   store   ogg   final   htm   get请求   依赖   als   

原文地址:http://www.cnblogs.com/livelihood/p/6758764.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!