Android网络请求Retrofit进行二次封装,拿过来就能用

 
  
  • Andrroid开发中,网络请求十分常用
  • 而在Android网络请求库中,Retrofit是当下最热的一个网络请求库
Retrofit简介
这里对Retrofit进行了一下二次封装,把一些固定的代码摘出来了,

import android.util.Log;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by 31962 on 2017/11/2.
 */

public class RetrofitUtils {
    private static RetrofitUtils retrofitUtils;
    private static Retrofit retrofit;
    public RetrofitUtils() {
    }

    public static RetrofitUtils getInstance(){

        if(retrofitUtils == null){
            synchronized (RetrofitUtils.class){
                if(retrofitUtils == null){
                    retrofitUtils = new RetrofitUtils();

                }
            }

        }
        return retrofitUtils;
    }

    public static synchronized Retrofit getRetrofit(String url){
        HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
            @Override
            public void log(String message) {
                Log.i("xxx", "log: "+message);
            }
        });
        httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

        OkHttpClient httpClient = new OkHttpClient.Builder().addInterceptor(httpLoggingInterceptor).connectTimeout(5000, TimeUnit.SECONDS).build();

        if(retrofit == null){
            retrofit = new Retrofit.Builder().baseUrl(url).client(httpClient).addConverterFactory(GsonConverterFactory.create())
                    .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                    .build();

        }
        return retrofit;
    }

    public <T>T getApiService(String url,Class<T> cl){
        Retrofit retrofit = getRetrofit(url);
        return retrofit.create(cl);
    }
}

这里用的时候需要写一个ApiService接口,用来写请求:
package com.bewei.retrofit.inter;


import com.bewei.retrofit.bean.News;
import com.bewei.retrofit.bean.Party;
import com.bewei.retrofit.bean.User;


import retrofit2.Call;
import retrofit2.http.Field;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.GET;
import retrofit2.http.POST;
import retrofit2.http.Path;
import retrofit2.http.Query;


/**
 * 1. 类的用途 网络接口
 * 2. @author forever
 * 3. @date 2017/11/1 14:32
 */




public interface ApiService {
    /**
     * 无参get请求
     * http://service.meiyinkeqiu.com/service/ads/cptj
     *
     * @return
     */
    @GET("service/ads/cptj")
    Call getNoParams();


    /**
     * 有参get请求
     * 拼接参数 /形式
     *
     * @return https://api.github.com/users/baiiu
     */
    @GET("users/{user}")
   Call getHasParams(@Path("user") String user);


    /**
     * http://www.93.gov.cn/93app/data.do
     * channelId
     * startNum
     * 拼接 ? &
     * 为主
     */
    @GET("data.do")


   Call getHasParams2(@Query("channelId")int channelId, @Query("startNum") int startNum );


    /**
     * post请求 http://120.27.23.105/user/reg 注册
     */
    @POST("reg")
    @FormUrlEncoded//支持表单提交
    Call register(@Field("mobile")String mobile,@Field("password")String password);

}
这里边列出了各种方式的网络请求,
这样,就可以使用Retrofit了

你可能感兴趣的:(Android网络请求Retrofit进行二次封装,拿过来就能用)