OkHttpClient工具类及调用

//OkHttpClient

public class OkHttpUtils {
    private static OkHttpUtils okHttpUtils;
    private final Handler myHandler;
    private OkHttpClient client;

    private OkHttpUtils(){
        myHandler = new Handler(Looper.getMainLooper());
        client = new OkHttpClient.Builder()
                .connectTimeout(5000, TimeUnit.MILLISECONDS)
                .readTimeout(5000, TimeUnit.MILLISECONDS)
                .writeTimeout(5000, TimeUnit.MILLISECONDS)
                .build();
    }
    public static OkHttpUtils getInstance(){
        if (okHttpUtils==null){
            synchronized (OkHttpUtils.class){
                if (okHttpUtils==null){
                    return okHttpUtils=new OkHttpUtils();
                }
            }
        }
        return okHttpUtils;
    }
    public void getPost(String url, Map map, final OkCallback okCallback){
        FormBody.Builder builder = new FormBody.Builder();
        for (String key:map.keySet()) {
              builder.add(key,map.get(key));
        }
        FormBody build = builder.build();
        final Request request = new Request.Builder()
                .post(build)
                .url(url)
                .build();
        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, final IOException e) {
                if (okCallback!=null){
                    myHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            okCallback.onFailure(e);
                        }
                    });
                }
            }

            @Override
            public void onResponse(Call call, final Response response) throws IOException {
                myHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            if (response!=null && response.isSuccessful()){
                                String string = response.body().string();
                                if (okCallback!=null){
                                    okCallback.onResponse(string);
                                    return;
                                }
                            }
                        }catch (Exception e){
                            e.printStackTrace();
                        }
                        if (okCallback!=null){
                            okCallback.onFailure(new Exception("网络异常"));
                        }
                    }
                });
            }
        });
    }
    public interface OkCallback{
        void onFailure(Exception e);
        void onResponse(String json);
    }
}




//Mainactivity

public class MainActivity extends AppCompatActivity {

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

    public void login(View view) {
        String url="https://www.zhaoapi.cn/user/login";
        HashMap map = new HashMap<>();
        map.put("mobile","18611112222");
        map.put("password","123456");
        OkHttpUtils.getInstance().getPost(url, map, new OkHttpUtils.OkCallback() {
            @Override
            public void onFailure(Exception e) {
                Toast.makeText(MainActivity.this,"失败",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onResponse(String json) {
                Toast.makeText(MainActivity.this,"成功",Toast.LENGTH_SHORT).show();
            }
        });
    }
}

你可能感兴趣的:(OkHttpClient工具类及调用)