Retrofit和Rxjava网络请求、日志拦截器、

URL中:

package shopping.bwie.com.baiduditu.model;

import java.util.List;

import okhttp3.OkHttpClient;
import retrofit2.GsonConverterFactory;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import shopping.bwie.com.baiduditu.intfacex.JsonBean;
import shopping.bwie.com.baiduditu.intfacex.Okhttp_Intreface;
import shopping.bwie.com.baiduditu.intfacex.Retrofit_Intreface;


public class Retrofit_Url {
    public static void getRetrofit_Url(final Okhttp_Intreface> okhttp_intreface){
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        builder.addInterceptor(new LoggingInterceptor());
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://www.xieast.com/api/news/")
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .client(builder.build())
                .build();
        Retrofit_Intreface retrofit_intreface = retrofit.create(Retrofit_Intreface.class);
        retrofit_intreface.getUrl(1)

                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        okhttp_intreface.datasb((Exception) e);
                    }

                    @Override
                    public void onNext(JsonBean jsonBean) {
                        List beanData = jsonBean.getData();
                        okhttp_intreface.datacg(beanData);
                    }
                });
    }
}

interface中:

package shopping.bwie.com.baiduditu.intfacex;

import retrofit2.http.GET;
import retrofit2.http.Query;
import rx.Observable;


public interface Retrofit_Intreface {
    @GET("news.php?type=top&tdsourcetag=s_pcqq_aiomsg")
    Observable getUrl(@Query("page") int page);
}

拦截器

package shopping.bwie.com.baiduditu.model;

import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

public class LoggingInterceptor implements Interceptor {
    @Override
    public Response intercept(Chain chain) throws IOException {
        //拿到Request对象
        Request request = chain.request();
        long t1 = System.nanoTime();
        System.out.println(" request  = " + String.format("Sending request %s on %s%n%s",
                request.url(), chain.connection(), request.headers()));
        //拿到Response对象
        Response response = chain.proceed(request);
        long t2 = System.nanoTime();
        //得出请求网络,到得到结果,中间消耗了多长时间
        System.out.println("response  " + String.format("Received response for %s in %.1fms%n%s",
                response.request().url(), (t2 - t1) / 1e6d, response.headers()));
        return response;
    }
}

依赖


OKhttp依赖
implementation 'com.squareup.okhttp3:okhttp:3.11.0'

fresco依赖
implementation 'com.facebook.fresco:fresco:0.12.0'

日志拦截器依赖:
implementation 'com.squareup.okhttp3:logging-interceptor:3.10.0'

Retrofit的依赖:
implementation'com.squareup.retrofit2:retrofit:2.0.0-beta4'
Retrofit自动生成Gson的依赖:
implementation 'com.squareup.retrofit2:converter-gson:2.0.0-beta3'




implementation 'io.reactivex:rxandroid:1.2.1'
implementation 'com.squareup.retrofit2:adapter-rxjava:2.1.0'
implementation 'io.reactivex:rxjava:1.1.6'
implementation 'com.jakewharton.rxbinding:rxbinding:0.4.0'

你可能感兴趣的:(Retrofit和Rxjava网络请求、日志拦截器、)