Android中okhttp的基本用法1的基础上进行封装

http://blog.csdn.net/zhaihaohao1/article/details/50967137的基础上进行封装:

分装类容主要有两个:

1对请求,使用接口回调封装

2在其中封装了状态对话框

封装的主要代码:

<span style="font-size:18px;">public class OkHttpPackage {

    //      对get请求的封装,对外调用的方法
    HttpGetRequest httpGetRequest;
    public  void httpGetManager(String url,Boolean isProgressDialog,Context context,HttpGetRequest httpGetRequest){
       this.httpGetRequest= httpGetRequest;
        myHttpGet(url,isProgressDialog,context);
    }

    public interface HttpGetRequest{
        public void myOnGetFailure(Request request, IOException e);
        public void myOnGetResponse(String response);

    }
    /**
     * 异步请求
     * 发送网络请求
     * get请求
     * 默认已经把线程分装好了
     */
    private  void myHttpGet(String url,Boolean isProgressDialog, final Context context) {
        if(isProgressDialog) {
            showDialog(context);
        }

        OkHttpClient httpClient = new OkHttpClient();
        Request request = new Request.Builder()
                .url(url)
                .build();
        httpClient.newCall(request).enqueue(new Callback() {
                                                @Override
                                                public void onFailure(Request request, IOException e) {
                                                    if(httpGetRequest!=null) {
                                                        httpGetRequest.myOnGetFailure(request, e);
                                                        dismissDialog(context);

                                                    }
                                                }

                                                @Override
                                                public void onResponse(Response response) throws IOException {
                                                    if (response.isSuccessful()) {
                                                        String responseGet = response.body().string();
                                                        httpGetRequest.myOnGetResponse(responseGet);
                                                        try {
                                                            Thread.sleep(2000);
                                                            dismissDialog(context);
                                                        } catch (InterruptedException e) {
                                                            e.printStackTrace();
                                                        }

                                                    }
                                                }
                                            }
        );


    }
//  -----------------对post请求进行封装---------------------
//  对外调用的方法
    HttpRequest httpRequest;
    public  void httpPostManager(String url,FormEncodingBuilder formEncodingBuilder,Boolean isProgressDialog,Context context,HttpRequest httpRequest){
       this.httpRequest=httpRequest;
       myHttpPost(url,formEncodingBuilder,isProgressDialog,context);

    }
    public interface HttpRequest{
        public void myOnFailure(Request request, IOException e);
        public void myOnResponse(String response);

    }
    /**
     * 异步请求
     * 发送网络请求
     * post请求
     * 默认已经把线程分装好了
     * 参数1路径
     * 参数2参数
     */
    private void myHttpPost(String url,FormEncodingBuilder formEncodingBuilder,Boolean isProgressDialog, final Context context) {
        if(isProgressDialog) {
            showDialog(context);
        }
        OkHttpClient httpClient = new OkHttpClient();
        RequestBody body = formEncodingBuilder.build();
        Request request = new Request.Builder()
                .url(url)
                .post(body)
                .build();

//      成功,用接口回调,失败用接口回调
        httpClient.newCall(request).enqueue(new com.squareup.okhttp.Callback() {
            @Override
            public void onFailure(Request request, IOException e) {
                if (httpRequest != null) {
                    httpRequest.myOnFailure(request, e);
                    dismissDialog(context);
                }
            }

            @Override
            public void onResponse(Response response) throws IOException {
                if (response.isSuccessful()) {
                    String jsonStr = response.body().string();
                    if (httpRequest != null) {
                        httpRequest.myOnResponse(jsonStr);
                        dismissDialog(context);
                    }
                }
            }
        });

    }
    CustomProgress customProgress;
    private void showDialog(Context context){
        if(customProgress==null) {
            customProgress=new CustomProgress(context, R.style.Custom_Progress);
        }
        customProgress.myShow(customProgress, "玩命加载中...", true, null);

    }

    private void dismissDialog(Context context){
        if(customProgress==null) {
            customProgress=new CustomProgress(context, R.style.Custom_Progress);
        }
        customProgress.dismiss();

    }
}
</span>

在activity中调用:

<span style="font-size:18px;">public class MainActivity extends AppCompatActivity {
    private Button btn_okhttp01;
    private Button btn_okhttp02;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        myOnClick();
    }
    /**
     * 初始化控件
     */
    private void initView() {
        btn_okhttp01 = (Button) findViewById(R.id.btn_okhttp01);
        btn_okhttp02 = (Button) findViewById(R.id.btn_okhttp02);
    }
    /**
     *post请求
     */
    private void myOnClick() {
        btn_okhttp01.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String url = "http://112.124.22.238:8081/course_api/banner/query?type=1";
                FormEncodingBuilder formEncodingBuilder = new FormEncodingBuilder();
                formEncodingBuilder.add("type", "1");
               new OkHttpPackage().httpPostManager(url, formEncodingBuilder, true, MainActivity.this, new OkHttpPackage.HttpRequest() {
                   @Override
                   public void myOnFailure(Request request, IOException e) {

                   }
                   @Override
                   public void myOnResponse(String response) {

                   }
               });

            }
        });
/**
 *get请求
 */
        btn_okhttp02.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String url = "http://112.124.22.238:8081/course_api/banner/query?type=1";
                new OkHttpPackage().httpGetManager(url, true, MainActivity.this, new HttpGetRequest() {
                    @Override
                    public void myOnGetFailure(Request request, IOException e) {

                    }

                    @Override
                    public void myOnGetResponse(String response) {

                    }
                });

            }
        });
    }

}//class</span>
源码下载:
http://download.csdn.net/detail/zhaihaohao1/9584743




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值