对OkHttp3的单独封装 get和post

1.首先在gradle里配置文件

 

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:24.2.1'

    compile 'com.squareup.okhttp3:okhttp:3.0.1'
}

2.mainfast加权限

 

 

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

3.贴代码

 

下面是utils请求类

 

package com.yueniu.myokhttputil;

import java.io.IOException;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * Created by maoshenbo on 2017/3/9 9:07.
 */
public class OkHttp3Util {
    private static int TimeOut = 120;
    //单例获取ohttp3对象
    private static OkHttpClient okHttp = null;
    public static synchronized OkHttpClient getInstance(){
        if(okHttp == null){
            okHttp = new OkHttpClient.Builder()
                    .readTimeout(TimeOut, TimeUnit.SECONDS)
                    .connectTimeout(TimeOut, TimeUnit.SECONDS)
                    .writeTimeout(TimeOut, TimeUnit.SECONDS)
                    .build();
        }
        return okHttp;
    }

    /**
     * 自定义callback
     */
    static class MyCallBack implements Callback {
        private OkHttpCallBack okHttpCallBack;

        public MyCallBack(OkHttpCallBack okHttpCallBack) {
            this.okHttpCallBack = okHttpCallBack;
        }

        @Override
        public void onFailure(Call call, IOException e) {
            okHttpCallBack.onFailure(e);
        }

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            okHttpCallBack.onSuccess(response);
        }
    }

    /**
     * 接口方法
     */
    public interface OkHttpCallBack{
        void onFailure(IOException e);
        void onSuccess(Response response);
    }

    /**
     * post请求
     * @param url
     * @param map
     * @param callback
     */
    public static void doPost(String url, Map<String, Object> map, OkHttpCallBack callback){
        MyCallBack myCallback = new MyCallBack(callback);
        FormBody.Builder formBody = new FormBody.Builder();
        //解析转化map
        if (null != map && map.size()>0) {
            for (Map.Entry<String, Object> entry : map.entrySet()) {
                formBody.add(entry.getKey(), entry.getValue().toString());
            }
        }
        RequestBody requestBody = formBody.build();
        Request request = new Request.Builder()
                .url(url)
                .post(requestBody)
                .build();
        okHttp.newCall(request).enqueue(myCallback);
    }

    /**
     * get请求
     * @param url
     * @param callback
     */
    public static void doGet(String url, OkHttpCallBack callback){
        MyCallBack myCallback = new MyCallBack(callback);
        Request request = new Request.Builder()
                .url(url)
                .get()
                .build();
        okHttp.newCall(request).enqueue(myCallback);
    }
}

 

使用方式

 

package com.yueniu.myokhttputil;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Response;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        OkHttp3Util.getInstance().doGet("", new OkHttp3Util.OkHttpCallBack() {
            @Override
            public void onFailure(IOException e) {

            }

            @Override
            public void onSuccess(Response response) {

            }
        });

        Map<String, Object> map = new HashMap<>();
        map.put("www", "eeee");
        OkHttp3Util.getInstance().doPost("", map, new OkHttp3Util.OkHttpCallBack() {
            @Override
            public void onFailure(IOException e) {

            }

            @Override
            public void onSuccess(Response response) {

            }
        });
    }
}

这是单纯的okhttp3的简单封装,学习使用

 

 

    原文作者:Dijkstra算法
    原文地址: https://blog.csdn.net/bsmmaoshenbo/article/details/60956545
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞