Okhttp encapsulate notes and okhttp encapsulate notes

Source: Internet
Author: User

Okhttp encapsulate notes and okhttp encapsulate notes
1. Go to okhttp to find the latest version:

Implementation 'com. squareup. okhttp3: okhttp: 3.10.0'

Encapsulate the callback interface:

public abstract class OkhttpCallBack {    public  abstract void FailureBack(Call request, Exception e);    public  abstract void  SuccessBack(Response response) throws IOException;}

OkHttp get request:

package com.example.administrator.okhttpencapsulation;import android.content.Context;import java.io.File;import java.io.IOException;import java.util.concurrent.TimeUnit;import okhttp3.Cache;import okhttp3.Call;import okhttp3.Callback;import okhttp3.OkHttpClient;import okhttp3.Request;import okhttp3.Response;/** * Created by Administrator on 2018/3/26. */public   class OkhttpMamager  {   private  static  OkhttpMamager mInstance;    private final OkHttpClient mokhttpClient;    public  static OkhttpMamager getinstance(Context context){         if (mInstance==null){             synchronized (OkhttpMamager.class){                 if (mInstance==null){                     mInstance = new OkhttpMamager(context);                 }             }         }         return  mInstance;     }    public OkhttpMamager(Context context) {        File sdcache=  context.getExternalCacheDir();        int cache=10*1024*1024;        OkHttpClient.Builder builer = new OkHttpClient.Builder().connectTimeout(15, TimeUnit.SECONDS)                .readTimeout(20, TimeUnit.SECONDS)                .writeTimeout(20, TimeUnit.SECONDS)                .cache(new Cache(sdcache.getAbsoluteFile(), cache));             mokhttpClient=builer.build();    }    public  void  getASynHttp(String url, final OkhttpCallBack callBack){                        final Request request=new Request.Builder().url(url).build();                  mokhttpClient.newCall(request).enqueue(new Callback() {            @Override            public void onFailure(Call call, IOException e) {                 callBack.FailureBack(call,e);              }            @Override            public void onResponse(Call call, Response response) throws IOException {                 callBack.SuccessBack(response);              }        });    }}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.