总结2

public interface Api {
    String BASEURL = "https://www.zhaoapi.cn/";
}
import io.reactivex.Observable;
import retrofit2.http.GET;

public interface AdApiService {
    @GET("ad/getAd")
    Observable<OneBean> getAd();
}

import io.reactivex.Observable;

public class AdApi {
    private static AdApi adApi;
    private AdApiService adApiService;

    private AdApi(AdApiService adApiService) {
        this.adApiService = adApiService;
    }

    public static AdApi getAdApi(AdApiService adApiService) {
        if (adApi == null) {
            adApi = new AdApi(adApiService);
        }
        return adApi;
    }

    public Observable<OneBean> getAd() {
        return adApiService.getAd();
    }
}
import java.util.concurrent.TimeUnit;
import dagger.Module;
import dagger.Provides;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

@Module
public class HttpModule {
    @Provides
    OkHttpClient.Builder provideOkHttpClientBuilder() {
        return new OkHttpClient.Builder()
                .writeTimeout(20, TimeUnit.SECONDS)
                .readTimeout(20, TimeUnit.SECONDS)
                .connectTimeout(10, TimeUnit.SECONDS);
    }


    @Provides
    AdApi provideAdApi(OkHttpClient.Builder builder) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Api.BASEURL)
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .client(builder.build())
                .build();
        AdApiService adApiService = retrofit.create(AdApiService.class);
        return AdApi.getAdApi(adApiService);
    }
    
}

public interface BaseContract {
    //抽取所有Presenter共性,比如绑定,解绑
    interface BasePresenter<T extends BaseView> {
        void attchView(T view);

        void detachView();
    }

    //抽取所有View的共性,比如显示进度条和关闭进度
    interface BaseView {
        void showLoading();

        void dismissLoading();
    }
}

public interface HomPageContract {
    interface View extends BaseContract.BaseView {
        void getAdSuccess(OneBean oneBean);

       // void getCatagorySuccess(CatagoryBean catagoryBean);
    }

    interface Presenter extends BaseContract.BasePresenter<View> {
        void getAd();

      //  void getCatagory();
    }

}
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class HomePagePresenter extends BasePresenter<HomPageContract.View> implements HomPageContract.Presenter {
    private AdApi adApi;

    @Inject
    public HomePagePresenter(AdApi adApi ) {
        this.adApi = adApi;

    }

    @Override
    public void getAd() {
        adApi.getAd()
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Observer<OneBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(OneBean oneBean) {
                        Log.d("HomePagePresenter", oneBean.toString());
                        mView.getAdSuccess(oneBean);
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }


}

@Component(modules = HttpModule.class)
public interface HttpComponent {
 //   void inject(LoginActivity loginActivity);

    void inject(OneFragment oneFragment);

   // void inject(ClassifyFragment classifyFragment);

    void inject(ListActivity listActivity);
}
public class OneFragment extends BaseFragment<HomePagePresenter> implements HomPageContract.View {

  

    @Override
    public int getContentLayout() {
        return R.layout.fragment1;
    }

    @Override
    public void inject() {
        DaggerHttpComponent.builder()
                .httpModule(new HttpModule())
                .build()
                .inject(this);
    }

    @Override
    public void initView(View view) {
        mPresenter.getAd();
       //获取id
    }



    @Override
    public void getAdSuccess(OneBean oneBean) {
        Log.d("OneFragment", oneBean.toString());
       
    }
}






阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页