封装Okhttp卡哇伊伊伊的娃

import android.os.Handler;
import android.os.Looper;

import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

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

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

public class Okhehe {
    private OkHttpClient client;
    private Handler handler;
    private MediaType lx= MediaType.parse("application/json;charset=utf-8");
    private Okhehe(){
        client=new OkHttpClient.Builder().build();
        handler=new Handler(Looper.getMainLooper());
    }
    public static Okhehe getInstance(){
        Okhehe hehe=new Okhehe();
        return hehe;
    }
    //Get String
    public void GetString(String url, final FuncString callback){
        Request request=new Request.Builder().url(url).build();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response!=null&&response.isSuccessful()){
                    ChengGongString(response.body().string(),callback);
                }
            }
        });
    }
    //Get token String
    public void GetTokenString(String url,String token, final FuncString callback){
        Request request=new Request.Builder().url(url).addHeader("Authorization",token).build();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response!=null&&response.isSuccessful()){
                    ChengGongString(response.body().string(),callback);
                }
            }
        });
    }
    //Post JSONObejct
    public void PostJSONObejct(String url, Map map, final FuncJSONObejct callback){
        RequestBody requestBody=RequestBody.create(lx,new Gson().toJson(map));
        Request request=new Request.Builder().url(url).post(requestBody).build();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response!=null&&response.isSuccessful()){
                    ChengGongJSONObejct(response.body().string(),callback);
                }
            }
        });
    }
    //put JSONObejct
    public void PutJSONObejct(String url,String token, Map map, final FuncJSONObejct callback){
        RequestBody requestBody=RequestBody.create(lx,new Gson().toJson(map));
        Request request=new Request.Builder().url(url).addHeader("Authorization",token).put(requestBody).build();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response!=null&&response.isSuccessful()){
                    ChengGongJSONObejct(response.body().string(),callback);
                }
            }
        });
    }

    private void ChengGongJSONObejct(final String js, final FuncJSONObejct callback) {
        handler.post(new Runnable() {
            @Override
            public void run() {
                JSONObject jo=null;
                try {
                    jo=new JSONObject(js);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                callback.onResponse(jo);
            }
        });
    }

    private void ChengGongString(final String js, final FuncString callback) {
        handler.post(new Runnable() {
            @Override
            public void run() {
                callback.onResponse(js);
            }
        });
    }

    public interface FuncString {
        void onResponse(String js);
    }
    public interface FuncJSONObejct {
        void onResponse(JSONObject jo);
    }
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值