NoHttp的使用

Posted 内涵馒头

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了NoHttp的使用相关的知识,希望对你有一定的参考价值。


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的使用的主要内容,如果未能解决你的问题,请参考以下文章

真狠,为了干掉 HTTP ,Spring团队又开源nohttp了!

厉害了,为了干掉 HTTP ,Spring团队又开源 nohttp 项目!

真狠,为了干掉 HTTP ,Spring团队又开源nohttp了!

NoHttp封装--06 NoHttp之队列队列优先级

NoHttp框架

NoHttp封装--03 cookie