HTTP 与 Mvc模式

接口

1. 正常接口

代码
public interface MyOkHttpListener {

    void OnOk(String json);
    void OnNoOk(String message);

}

2. 进度接口

代码
public interface MyProgressListener {

    void Error(String message);
    void Finish();
    void GetProgress(int progress);


}

OkHttp的分装

单例分装

代码
public class OkHttpUtil {

    private OkHttpClient client = null;

    private OkHttpUtil(){

        Interceptor interceptor = new Interceptor() {
            @Override
            public Response intercept(Chain chain) throws IOException {
                Request request = chain.request().newBuilder().header("token", "hasdkdhkasjhdka").build();
                return chain.proceed(request);
            }
        };

        HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
        httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

        client = new OkHttpClient.Builder()
                .callTimeout(1, TimeUnit.MINUTES)
                .readTimeout(1,TimeUnit.MINUTES)
                .addInterceptor(interceptor)
                .build();
    }

    private static OkHttpUtil okHttpUtil = null;

    public static OkHttpUtil GetValue(){
        if (okHttpUtil == null){
            synchronized (Object.class){
                if (okHttpUtil == null){
                    okHttpUtil = new OkHttpUtil();
                }
            }
        }
        return okHttpUtil;
    }

    public void UpGet(String url , final MyOkHttpListener myOkHttpListener){

        Request request = new Request.Builder()
                .get()
                .url(url)
                .build();
        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                myOkHttpListener.OnNoOk(e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                myOkHttpListener.OnOk(response.body().string());
            }
        });

    }
    public void UpPut(String url , Map<String,String> map , final MyOkHttpListener myOkHttpListener){

        FormBody.Builder builder = new FormBody.Builder();
        Set<Map.Entry<String, String>> entries = map.entrySet();

        for (Map.Entry<String, String> entry : entries) {
            String key = entry.getKey();
            String value = entry.getValue();
            builder.add(key,value);
        }

        FormBody formBody = builder.build();

        Request request = new Request.Builder()
                .post(formBody)
                .url(url)
                .build();

        Call call = client.newCall(request);

        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                myOkHttpListener.OnNoOk(e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                myOkHttpListener.OnOk(response.body().string());
            }
        });

    }
    public void UpDown(String url , final String FileName , final MyProgressListener myProgressListener){

        Request request = new Request.Builder()
                .get()
                .url(url)
                .build();
        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                myProgressListener.Error(e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {

                long max = response.body().contentLength();
                InputStream inputStream = response.body().byteStream();
                FileOutputStream fileOutputStream = new FileOutputStream(FileName);
                int len = 0;
                int count = 0;
                byte[] bytes = new byte[1024];

                while((len = inputStream.read(bytes)) != -1){
                    count += len;
                    fileOutputStream.write(bytes,0,len);
                    myProgressListener.GetProgress((int) (count*100/max));
                }
                myProgressListener.Finish();
            }
        });

    }
    public void UpDownload(String url , String FileName , String type , String path , final MyOkHttpListener myOkHttpListener){

        MultipartBody multipartBody = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addFormDataPart("file", FileName, RequestBody.create(MediaType.parse(type), path))
                .build();

        Request request = new Request.Builder()
                .post(multipartBody)
                .url(url)
                .build();

        Call call = client.newCall(request);

        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                myOkHttpListener.OnNoOk(e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                myOkHttpListener.OnOk("上传成功");
            }
        });

    }

}

MVC模式使用OkHttp

接口

添加自己要用的OkHttp方法 以及参数

public interface ModelListener {

    void DoPost(String url , Map<String,String> map ,MyOkHttpListener myOkHttpListener);

}

MVC类

实现你的接口并且添加工具类的方法

public class MyModel implements modelListener {
    @Override
    public void getGet(String url, MyOkHttpIm myOkHttpIm) {
        OkHttpUtil.getInstance().DoGet(url,myOkHttpIm);
    }
}

MVC调用

实例化Model方法 调用

Map<String,String> map = new HashMap<>();
        map.put("itemid", "2");
        map.put("act", "ad_app");
        myModel.DoPost("http://api.yunzhancn.cn/api/app.interface.php?siteid=78703&", map, new MyOkHttpListener() {
            @Override
            public void OnOk(String json) {
                Message obtain = Message.obtain();
                obtain.what = 2;
                obtain.obj = json;
                handler.sendMessage(obtain);
            }

            @Override
            public void OnNoOk(String message) {
                Message obtain = Message.obtain();
                obtain.what = 1;
                obtain.obj = message;
                handler.sendMessage(obtain);
            }
        });
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值