2017-03-09 4 views
2

GSON, 개조 및 OkHttpClient단검 2 주사

의 싱글
@Module 
public class MyModule { 

    @Provides 
    @Singleton 
    Gson provideGson() { 
     GsonBuilder gsonBuilder = new GsonBuilder(); 
     return gsonBuilder.create(); 
    } 

    @Provides 
    @Singleton 
    OkHttpClient provideOkHttpClient() { 
     OkHttpClient client = new OkHttpClient(); 
     return client; 
    } 

    @Provides 
    @Singleton 
    Retrofit provideRetrofit(Gson gson, OkHttpClient okHttpClient) { 
     Retrofit retrofit = new Retrofit.Builder() 
       .addConverterFactory(GsonConverterFactory.create(gson)) 
       .baseUrl(BuildConfig.SERVER_BASE_URL) 
       .client(okHttpClient) 
       .build(); 
     return retrofit; 
    } 
} 

활동에 싱글 톤과 단편

@Singleton 
@Component(modules={MyModule.class}) 
public interface MyComponent { 

    void inject(Activity activity); 
    void inject(Fragment fragment); 
    void inject(Application application); 
} 

빌드 메인 애플리케이션 클래스를 주입 허용 성분을 proides 모듈을 작동하지 구성 요소

public class MyApp extends Application{ 


    private MyComponent component; 

    @Inject 
    Retrofit retrofit; 

    @Override 
    public void onCreate() { 
     super.onCreate(); 
     component= DaggerMyComponent.builder() 
       .myModule(new MyModule()).build(); 
     getComponent().inject(this); // inject retrofit here 
    } 

    public MyComponent getComponent() { 
     return component; 
    } 
} 

이것은 내가있는 부분입니다. Retrofit을 주입하려고합니다.

public class MyFragment extends Fragment { 

    @Inject 
    Retrofit retrofit; 

    @Override 
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, 
          @Nullable Bundle savedInstanceState) { 
     ((MyApp)getActivity().getApplication()).getComponent().inject(this); 
     .... 

    } 
} 

MyApp와 MyFragment 모두 개조 인스턴스가 null입니다.

Activiy

사용하는 모든 활동이 구성 요소 :

+0

나에게 잘 보인다. – Raghunandan

+0

같은 생각을했지만 주사를하지 않는 것 같았습니다. ( – Coder

답변

0

당신은 활동을 삽입 할 수, 조각과 같은 Component.You의 응용 프로그램은 각 활동이처럼, 조각 및 구성 요소에 대한 별도의 구성 요소를 작성해야 이 같은 활동

@Singleton 
@Component(modules={MyModule.class}) 
public interface MyActivityComponent { 
    void inject(Activity activity); 
    void inject(AnotherActivity activity); 
} 

를 주입 :

component= DaggerMyActivityComponent.builder() 
       .myModule(new MyModule()).build(); 
     getComponent().inject(this) 

조각

사용하는 모든 조각이 구성 요소 :이 같은 조각에

@Singleton 
    @Component(modules={MyModule.class}) 
    public interface MyFragmentComponent { 

     void inject(Fragment fragment); 
     void inject(AnotherFragmen fragment); 
    } 

를 주입 :

component= DaggerMyFragmentComponent.builder() 
       .myModule(new MyModule()).build(); 
     getComponent().inject(this) 

응용 프로그램

사용이 구성 요소를 사용자의 응용 프로그램 :이 같은 응용 프로그램에서

@Singleton 
    @Component(modules={MyModule.class}) 
    public interface MyAppComponent { 
     void inject(Application application); 
    } 

를 주입 :

component= DaggerMyAppComponent.builder() 
       .myModule(new MyModule()).build(); 
     getComponent().inject(this) 
+0

이렇게 작동합니다. 조각 특정 주입 방법을 만들지 않고 모든 조각에 종속성을 주입 할 수있는 방법이 있습니까? – Coder

+0

또한이 문제를 해결했지만 현재로서는 –

+0

'inject (Activity activity)'대신'inject (MyActivity activity)'를 시도해보십시오. –