[Android]OKHttpUtils

地址

https://github.com/hongyangAndroid/okhttputils
http://blog.csdn.net/lmj623565791/article/details/49734867/

导库

compile 'com.zhy:okhttputils:2.6.2'

可能用到的权限




GET

        OkHttpUtils.get().url("http://www.imooc.com")
                .addParams("username","hyman")
                .addParams("password","123")
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Request request, Exception e) {

                    }

                    @Override
                    public void onResponse(String response) {
                        //UI线程
                        textView.setText(response);
                    }
                });

Post

       OkHttpUtils.post().url("http://t2.refineit.cn:98/shequ/api/index.php?api=group&action=hot")
                .addParams("vName","10")
                .addParams("vCode","10")
                .addParams("uuid","ffffffff-c53f-2161-72de-3aa90033c587")
                .addParams("dInfo","ffffffff-c53f-2161-72de-3aa90033c587")
                .addParams("aSign","md5")
                .build().execute(new StringCallback() {
            @Override
            public void onError(Request request, Exception e) {

            }

            @Override
            public void onResponse(String response) {
                textView.setText(response);
            }
        });

带文件的post请求

 OkHttpUtils.post()
                    .url("http://t2.refineit.cn:98/shequ/api/index.php?api=photo&action=uploadPhoto")
                    .addFile("photo",file.getName(),file)
                    .addParams("vName","10")
                    .addParams("vCode","10")
                    .addParams("uuid","ffffffff-c53f-2161-72de-3aa90033c587")
                    .addParams("dInfo","ffffffff-c53f-2161-72de-3aa90033c587")
                    .addParams("aSign","md5")
                    .build().execute(new StringCallback() {
                @Override
                public void onError(Request request, Exception e) {

                }

                @Override
                public void onResponse(String response) {
                    textView.setText(response);
                }
            });

下载文件

               OkHttpUtils.get().url
                ("https://res.wx.qq.com/open/zh_CN/htmledition/res/dev/download/sdk" +
                        "/WXTicket_Android221cbf.zip")
                .build().execute
                (new FileCallBack(Environment.getExternalStorageDirectory().getAbsolutePath(),
                        "okhttptest.zip") {
            @Override
            public void inProgress(float progress) {
                textView.setText(progress+"");
            }

            @Override
            public void onError(Request request, Exception e) {

            }

            @Override
            public void onResponse(File response) {
                textView.setText("文件路径:" + response.getAbsolutePath()
                        + "\n文件名:" + response.getName());
            }
        });

你可能感兴趣的:([Android]OKHttpUtils)