Retrofit封装

2018-01-08

Android

原CSDN博客已弃用,文章会逐渐迁移过来。

Api.java

package com.onion.retrofit;  

        import retrofit2.Call;  
        import retrofit2.http.Field;  
        import retrofit2.http.FormUrlEncoded;  
        import retrofit2.http.POST;  

/** 
 * Created by OnionMac on 2018/5/17. 
 */  

public interface Api {  

    @POST("xxx/xxx")  
    @FormUrlEncoded  
    Call<String> get(@Field("111") String name);  

}  

RetrofitUtil.java

public class RetrofitUtil {  

    private static Retrofit sRetrofit;  
    private static OkHttpClient mOkHttpClient;  

    private RetrofitUtil(){}  

    public static class Hide{  
        static RetrofitUtil sRetrofitUtil = new RetrofitUtil();  
    }  

    public static RetrofitUtil getInstance() {  
        return Hide.sRetrofitUtil;  
    }  

    public Api build(){  
        if(mOkHttpClient == null){  
            mOkHttpClient = new OkHttpClient.Builder()  
                    .connectTimeout(10, TimeUnit.SECONDS)  
                    .readTimeout(10, TimeUnit.SECONDS)  
                    .writeTimeout(10, TimeUnit.SECONDS)  
                    .build();  
        }  

        if(sRetrofit == null){  
            sRetrofit = new Retrofit.Builder()  
                    .baseUrl("www.baidu.com")  
                    .client(mOkHttpClient)  
                    .addConverterFactory(ScalarsConverterFactory.create()) //数据转换器  
                    .addConverterFactory(GsonConverterFactory.create())//数据转换器  
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())//rxjava转换器  
                    .build();  
        }  
        return sRetrofit.create(Api.class);  
    }  
}  

MyCallBack.java

package com.onion.retrofit;  

        import java.util.concurrent.TimeUnit;  

        import okhttp3.OkHttpClient;  
        import retrofit2.Retrofit;  
        import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;  
        import retrofit2.converter.gson.GsonConverterFactory;  
        import retrofit2.converter.scalars.ScalarsConverterFactory;  

/** 
 * Created by OnionMac on 2018/5/17. 
 */  


        package com.onion.retrofit;  

        import android.app.Activity;  

        import retrofit2.Call;  
        import retrofit2.Callback;  
        import retrofit2.Response;  

/** 
 * Created by OnionMac on 2018/5/17. 
 */  

public abstract class MyCallBack<T> implements Callback<T> {  

    private Activity mActivity;  

    public MyCallBack(Activity activity){  
        mActivity = activity;  
    }  

    @Override  
    public void onResponse(final Call<T> call, final Response<T> response) {  
        /** 
         * 运行在主线程 
         */  
        mActivity.runOnUiThread(new Runnable() {  
            @Override  
            public void run() {  
                onSuccess(call,response);  
            }  
        });  

    }  

    @Override  
    public void onFailure(final Call<T> call, final Throwable t) {  
        /** 
         * 运行在主线程 
         */  
        mActivity.runOnUiThread(new Runnable() {  
            @Override  
            public void run() {  
                onFailure(call,t);  
            }  
        });  
    }  

    protected abstract void onSuccess(Call<T> call, Response<T> response);  
    protected void onFailed(Call<T> call, Throwable t){}  
}  

MainActivity.java

package com.onion.retrofit;  

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

        import retrofit2.Call;  
        import retrofit2.Response;  

public class MainActivity extends AppCompatActivity {  

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


        /** 
         * 请求接口 
         */  
        RetrofitUtil.getInstance().build()  
                .get("张琦")  
                .enqueue(new MyCallBack<String>(this){  //传进去activity  
                    @Override  
                    protected void onSuccess(Call<String> call, Response<String> response) {  
                        // 这里已经是主线程  
                    }  

                    @Override  
                    protected void onFailed(Call<String> call, Throwable t) {  
                        //这里已经是主线程  
                    }  
                });  
    }  
}