zoukankan      html  css  js  c++  java
  • 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.请求结束
  • 相关阅读:
    神秘人物之comca —— 是否就是我的前车之鉴
    mfc错误:其原因可能是堆被损坏,这说明**.exe中或它加载的任何DLL
    微信公共平台php用$GLOBALS["HTTP_RAW_POST_DATA"]收不到信息解决方法
    2013蓝桥杯初赛c语言专科组题目与答案
    一道2012腾讯实习生笔试题
    文件版权自动注释,自动备份
    算法学习 三 >> 认识算法的效率(循环设计)
    算法学习 二 >> 结构化与面向对象两种算法设计的简略分析(c++/java)
    HTML入门(一)
    算法学习 一 >> 初识
  • 原文地址:https://www.cnblogs.com/livelihood/p/6758764.html
Copyright © 2011-2022 走看看