使用dagger 2.0和retrofit 2.0库进行Android开发

时间:2016-10-07 10:44:38

标签: android-studio foursquare retrofit2 dagger-2

我正在开发一个使用foursquare api,google geocode maps api和ola api的应用程序来显示指定位置的cab可用性。 我正在使用retrofit 2.0库来解析背景中的端点。我也在使用Dagger 2.0库并生成对象图。我在app / build / generated /文件夹中获取生成的dagger内容,但是当我首先在清理项目并重建它之后尝试导入DaggerAPIManagerComponent时,我得到错误:

  

无法找到符号DaggerAPIManagerComponent

这是我的代码:

/*ApiManager class*/

import android.content.Context;

import com.faraz.bookcabs.ApplicationControl;
import com.faraz.bookcabs.data.network.APIService;
import com.faraz.bookcabs.injection.module.APIManagerModule;
import com.faraz.bookcabs.models.User;
import com.faraz.bookcabs.models.retrofit.AuthResponse;
import com.faraz.bookcabs.models.retrofit.OffersResponse;
import com.faraz.bookcabs.models.retrofit.ProductResponse;
import com.faraz.bookcabs.models.retrofit.requests.Action;

import org.json.JSONObject;
import javax.inject.Inject;
import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class APIManager {

@Inject

  protected APIService apiService;

  public APIManager(Context paramContext) {
    injectDependencies(paramContext);
  }

  public Observable<OffersResponse> fetchOffers()
  {
    return this.apiService.fetchOffers().subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread());
  }

  public Observable<ProductResponse> getEstimates(String paramString1, String paramString2, String paramString3, String paramString4)
  {
    return this.apiService.getEstimates(paramString1, paramString2, paramString3, paramString4);
  }

  public Observable<ProductResponse> getProducts(String paramString1, String paramString2)
  {
    return this.apiService.getProducts(paramString1, paramString2);
  }

  protected void injectDependencies(Context paramContext)
  {
    DaggerAPIManagerComponent.builder().applicationComponent(ApplicationControl.get(paramContext).getComponent()).aPIManagerModule(new APIManagerModule()).build().inject(this);
  }

  public Observable<AuthResponse> loginUser(User paramUser, String paramString)
  {
    return this.apiService.loginUser(paramUser, paramString);
  }

  public Observable<AuthResponse> registerUser(User paramUser)
  {
    return this.apiService.registerUser(paramUser);
  }

  public Observable<JSONObject> saveAction(Action paramAction)
  {
    return this.apiService.saveAction(paramAction).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread());
  }
}


/*Networkhelper Class*/
public class NetworkHelper {

  public APIService newAPIService()
  {
    return new Retrofit.Builder().baseUrl("http://52.192.64.133/app/").addConverterFactory(GsonConverterFactory.create()).addCallAdapterFactory(RxJavaCallAdapterFactory.create()).build().create(APIService.class);
  }
  public FoursquareAPIService newFoursquareAPIService()
  {
    return new Retrofit.Builder().baseUrl("https://api.foursquare.com/v2/").addConverterFactory(GsonConverterFactory.create()).addCallAdapterFactory(RxJavaCallAdapterFactory.create()).build().create(FoursquareAPIService.class);
  }

  public GoogleLocationAPIService newGoogleLocationAPIService()
  {
    return new Retrofit.Builder().baseUrl("https://maps.googleapis.com/maps/api/").addConverterFactory(GsonConverterFactory.create()).addCallAdapterFactory(RxJavaCallAdapterFactory.create()).build().create(GoogleLocationAPIService.class);
  }

  public OlaAPIService newOlaAPIService()
  {
    return new Retrofit.Builder().baseUrl("https://devapi.olacabs.com/").addConverterFactory(GsonConverterFactory.create()).addCallAdapterFactory(RxJavaCallAdapterFactory.create()).build().create(OlaAPIService.class);
  }
}

/*This is my ApiService interface*/

public interface APIService {

  String ENDPOINT = "http://52.192.64.133/app/";

  @GET("api/offers")
  Observable<OffersResponse> fetchOffers();

  @GET("api/price")
  Observable<ProductResponse> getEstimates(@Query("lat1") String paramString1, @Query("long1") String paramString2, @Query("lat2") String paramString3, @Query("long2") String paramString4);

  @GET("api/services")
  Observable<ProductResponse> getProducts(@Query("lat1") String paramString1, @Query("long1") String paramString2);

  @POST("login")
  Observable<AuthResponse> loginUser(@Body User paramUser, @Query("type") String paramString);

  @POST("user")
  Observable<AuthResponse> registerUser(@Body User paramUser);

  @POST("api/action")
  Observable<JSONObject> saveAction(@Body Action paramAction);
}

这是我的app / build.gradle文件:

apply plugin: 'com.android.application'
apply plugin: 'com.neenbedankt.android-apt'


android {
    compileSdkVersion 24
    buildToolsVersion "24.0.1"
    defaultConfig {
    applicationId "com.faraz.bookcabs"
    minSdkVersion 15
    targetSdkVersion 24
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner    "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
dataBinding {
    enabled = true
    }
 }
    dependencies {
       compile fileTree(include: ['*.jar'], dir: 'libs')
       androidTestCompile('com.android.support.test.espresso:espresso-  core:2.2.2', {
       exclude group: 'com.android.support', module: 'support-annotations'
      })
   testCompile 'junit:junit:4.12'

   apt 'com.google.dagger:dagger-compiler:2.5'
   compile 'com.google.dagger:dagger:2.5'
   compile 'org.glassfish:javax.annotation:10.0-b28'

   compile 'com.android.support:appcompat-v7:24.2.0'
   compile 'com.android.support:design:24.2.0'

   compile 'com.google.code.gson:gson:2.6.2'
   compile 'com.squareup.retrofit2:retrofit:2.1.0'
   compile 'com.squareup.retrofit2:converter-gson:2.1.0'

   compile 'io.reactivex:rxjava:1.1.6
   compile 'io.reactivex:rxandroid:1.2.1'
   compile 'com.squareup.retrofit2:adapter-rxjava:2.1.0'
   compile 'com.jakewharton:butterknife:8.4.0'
   apt 'com.jakewharton:butterknife-compiler:8.4.0'

   compile 'com.jaredrummler:android-device-names:1.1.0'

   compile 'com.google.android.gms:play-services-ads:9.4.0'
   compile 'com.google.android.gms:play-services-auth:9.4.0'
   compile 'com.google.android.gms:play-services-gcm:9.4.0'
   }

这是我的APIManager Component类:

 package com.inveit.cabto.injection.component;

import com.inveit.cabto.data.APIManager;
import com.inveit.cabto.data.FourSquareAPIManager;
import com.inveit.cabto.data.GoogleAPIManager;
import com.inveit.cabto.data.OlaAPIManager;
import com.inveit.cabto.injection.module.APIManagerModule;
import com.inveit.cabto.injection.scope.PerApp;
import dagger.Component;

@PerApp
@Component(dependencies = {ApplicationComponent.class}, modules =     {APIManagerModule.class})
 public abstract interface APIMangerComponent {
 public abstract void inject(APIManager paramAPIManager);

 public abstract void inject(FourSquareAPIManager paramFourSquareAPIManager);

 public abstract void inject(GoogleAPIManager paramGoogleAPIManager);
 public abstract void inject(OlaAPIManager paramOlaAPIManager);
}

这是我的APIManager模块类:

  package com.inveit.cabto.injection.module;

  import com.inveit.cabto.data.network.APIService;
  import com.inveit.cabto.data.network.FoursquareAPIService;
  import com.inveit.cabto.data.network.GoogleLocationAPIService;
  import com.inveit.cabto.data.network.NetworkHelper;
  import com.inveit.cabto.data.network.OlaAPIService;
  import com.inveit.cabto.injection.scope.PerApp;

  import dagger.Module;
  import dagger.Provides;
  @Module
  public class APIManagerModule {

@PerApp
@Provides
APIService provideAPIService()
{
    return new NetworkHelper().newAPIService();
}


@PerApp
@Provides
FoursquareAPIService provideFoursquareAPIService()
{
    return new NetworkHelper().newFoursquareAPIService();
}

@PerApp
@Provides
GoogleLocationAPIService provideGoogleLocationAPIService()
{
    return new NetworkHelper().newGoogleLocationAPIService();
}


@PerApp
@Provides
OlaAPIService provideOlaAPIService()
{
    return new NetworkHelper().newOlaAPIService();
}



 }

ApplicationComponent接口:

  @Component(modules={ApplicationModule.class})
  @Singleton
  public abstract interface ApplicationComponent {
  public abstract Application application();

  public abstract void inject(BookingsActivity paramBookingsActivity);

  public abstract void inject(DestinationSearchActivity paramDestinationSearchActivity);

  public abstract void inject(LoginActivity paramLoginActivity);

  public abstract void inject(MainActivity paramMainActivity);

  public abstract void inject(OffersActivity paramOffersActivity);

  public abstract void inject(OlaDirectBookingActivity paramOlaDirectBookingActivity);

  public abstract void inject(PickupSearchActivity paramPickupSearchActivity);

  public abstract void inject(ShowVenuesActivity paramShowVenuesActivity);

  public abstract void inject(SlidingTabsFragment paramSlidingTabsFragment);

  public abstract void inject(UserRegistrationActivity paramUserRegistrationActivity);

  public abstract void inject(ProductsManager paramProductsManager);

  public abstract void inject(BookingsViewModel paramBookingsViewModel);

  public abstract void inject(ProductViewModel paramProductViewModel);


 }

任何人都可以帮我摆脱这个错误吗?

0 个答案:

没有答案