NoHttp的使用

來源:互聯網
上載者:User

標籤:影響   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的使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.