Retrofit+Rxjava+OkHttp+Get请求(含网络判断)

    implementation 'com.google.code.gson:gson:2.8.5'
    implementation 'com.squareup.okhttp3:okhttp:3.7.0'
    implementation 'com.squareup.okio:okio:1.12.0'
        //Retrofit
    implementation 'com.squareup.retrofit2:converter-gson:2.1.0'

    implementation 'com.squareup.retrofit2:retrofit:2.1.0'
    implementation 'io.reactivex.rxjava2:rxjava:2.0.1'
    implementation 'io.reactivex.rxjava2:rxandroid:2.0.1'
    implementation'com.squareup.retrofit2:adapter-rxjava2:2.2.0'
    
    implementation 'com.squareup.okhttp3:okhttp:3.7.0'
    implementation 'com.squareup.okio:okio:1.12.0'
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support:recyclerview-v7:28.0.0'
    implementation 'com.android.support:support-v4:28.0.0'
    implementation 'com.google.code.gson:gson:2.2.4'
    implementation 'com.facebook.fresco:fresco:0.12.0'
    implementation 'jp.wasabeef:glide-transformations:3.0.1'
    implementation 'com.xhb:xbanner:1.3.1'
    // 在 API < 14 上的机器支持 WebP 时,需要添加
    implementation 'com.facebook.fresco:animated-base-support:0.12.0'

    // 支持 GIF 动图,需要添加
    implementation 'com.facebook.fresco:animated-gif:0.12.0'

    // 支持 WebP (静态图+动图),需要添加
    implementation 'com.facebook.fresco:animated-webp:0.12.0'
    implementation 'com.facebook.fresco:webpsupport:0.12.0'

    // 仅支持 WebP 静态图,需要添加
    implementation 'com.facebook.fresco:webpsupport:0.12.0'

    implementation 'org.greenrobot:greendao:3.2.2'

**

  • 这是网络请求封装类

**

public class HttpUtils {
    private final Retrofit retrofit;
    public final Api api;
    private final OkHttpClient okHttpClient;

    private HttpUtils() {
        okHttpClient = new OkHttpClient.Builder()
                .addNetworkInterceptor(new LogginIntecepter())
                .connectTimeout(5, TimeUnit.SECONDS)
                .connectTimeout(5, TimeUnit.SECONDS)
                .build();
        retrofit = new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .baseUrl("http://172.17.8.100/")
                .client(okHttpClient)
                .build();
        api = retrofit.create(Api.class);
    }
网络判断工具类
/**
     * 返回值 -1:没有网络  1:WIFI网络   3:net网络
     */
    public static int getNetype(Context context) {
        int netType = -1;
        ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
        if (networkInfo == null) {
            return netType;
        }
        int nType = networkInfo.getType();
        if (nType == ConnectivityManager.TYPE_MOBILE) {
            netType = 2;
        } else if (nType == ConnectivityManager.TYPE_WIFI) {
            netType = 1;
        }
        return netType;
    }
    public class LogginIntecepter implements Interceptor {

        @Override
        public Response intercept(Interceptor.Chain chain) throws IOException {
            Request request = chain.request();
            Request newrequest = request.newBuilder()
                    .addHeader("userId", "11249")
                    .addHeader("sessionId", "155056366467311249")
                    .build();
            Response response = chain.proceed(newrequest);
      /*      RequestBody body = request.body();
            Log.e("myMessage", "");
            Headers headers = request.headers();
            Response response = chain.proceed(request);
            Headers headers1 = response.headers();*/
            return response;
        }
    }

    public static HttpUtils getInstance() {
        return HttpUtilsgetInstance.httpUtils;
    }

    private static class HttpUtilsgetInstance {

        private static HttpUtils httpUtils = new HttpUtils();
    }

猜你喜欢

转载自blog.csdn.net/qq_40432465/article/details/88756579