음악 응용 프로그램을 개발 중입니다. LastFM에서 아티스트의 이미지를로드하고 싶습니다. 이렇게하면됩니다. 1. 클래스 ArtistImageLoader extends BaseGlideUrlLoader
을 만들었습니다. 2. getUrl
방법에서 retrofit2를 사용하여 LastFM에서 아티스트의 이미지 URL을 getArtistInfo 방법으로 가져 왔습니다.dagger2와 함께 글라이드를 사용하는 방법
내 문제는 내가 개조 서비스를 투입하는 방법을 모른다는 것입니다. ArtistImageLoader
에 요청하십시오. 나는 이런 식으로했지만 NOP 예외가 발생했습니다. lastFmService
은 주입되지 않았습니다.
// GlideModule
glide.register(MLocalArtist.class, InputStream.class, new ArtistImageLoader.Factory());
// Use it in onCreate method of ArtistsFragment
DaggerLastFmComponent.builder().activityModule(new ActivityModule(getActivity()))
.netComponent(getNetComponent())
.build().inject(this);
// use this code in onBindViewHolder method of artists recycler adapter
Glide.with(getContext())
.from(MLocalArtist.class)
.load(localArtist)
.into(localArtistViewHolder.ivArtwork);
ArtistImageLoader는
public class ArtistImageLoader extends BaseGlideUrlLoader<MLocalArtist> {
@Inject
LastfmService lastfmService;
public ArtistImageLoader(Context context) {
super(context);
}
@Override
protected String getUrl(MLocalArtist model, int width, int height) {
Call<List<MArtist>> call = lastfmService.getArtistInfo(model.artistName);
try {
List<MArtist> artists = call.execute().body();
if (artists != null && artists.size() > 0) {
Timber.e(artists.get(0).toString());
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public static class Factory implements ModelLoaderFactory<MLocalArtist, InputStream> {
@Override public ModelLoader<MLocalArtist, InputStream> build(Context context, GenericLoaderFactory factories) {
return new ArtistImageLoader(context);
}
@Override public void teardown() {
}
}
}
당신은 내가 그것을 어떻게 도와 드릴까요? 정말 고맙습니다!
글라이드 버전 : 3.7.0
통합 라이브러리 : OkHttp3 + Dagger2
장치/안드로이드 버전 : 안드로이드 에뮬레이터 + 아수스 zenfone 5
EDIT 1
ActivityComponent.ja VA
@PerActivity
@Component(dependencies = AppComponent.class, modules = ActivityModule.class)
public interface ActivityComponent {
Context context();
}
AppComponent.java
@Singleton
@Component(modules = AppModule.class)
public interface AppComponent {
App app();
}
NetComponent.java
@Singleton
@Component(modules = {NetModule.class, AppModule.class})
public interface NetComponent {
@Named("chartSoundCloud")
Retrofit getSoundcloudChartRetrofit();
@Named("searchSoundCloud")
Retrofit getSoundcloudSearchRetrofit();
@Named("lastFM")
Retrofit getLastFmRetrofit();
}
LastFmComponent.java
@PerActivity
@Component(dependencies = NetComponent.class, modules = {LastFmModule.class, ActivityModule.class})
public interface LastFmComponent extends ActivityComponent {
void inject(ArtistsFragment artistsFragment);
}
ActivityModule.java
,451,515,@Module
public class ActivityModule {
private final Context mContext;
public ActivityModule(Context mContext) {
this.mContext = mContext;
}
@Provides
@PerActivity
Context provideActivityContext() {
return mContext;
}
}
AppModule.java
@Module
public class AppModule {
private App app;
public AppModule(App app){
this.app = app;
}
@Singleton
@Provides
App provideApplication() {
return app;
}
@Singleton
@Provides @Named("applicationContext")
Context provideApplicationContext(){
return app;
}
}
LastFmModule.java
@Module
public class LastFmModule {
@Provides
@PerActivity
LastfmService provideLastFmService(@Named("lastFM") Retrofit retrofit) {
return retrofit.create(LastfmService.class);
}
}
NetModule.java
@Module
public class NetModule {
static final int DISK_CACHE_SIZE = (int) MEGABYTES.toBytes(50);
@Provides
@Singleton
Cache provideOkHttpCache(@Named("applicationContext") Context application) {
Cache cache = new Cache(application.getCacheDir(), DISK_CACHE_SIZE);
return cache;
}
@Provides
@Singleton
ScdClientIdInterceptor provideScdClientIdInterceptor() {
return new ScdClientIdInterceptor();
}
@Provides
@Singleton
LastFMInterceptor provideLastFmInterceptor() {
return new LastFMInterceptor();
}
@Provides
@Singleton
HttpLoggingInterceptor provideHttpLoggingInterceptor() {
return new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY);
}
@Provides
@Singleton
@Named("soundcloud-Http")
OkHttpClient provideOkHttpSoundCloudClient(Cache cache, ScdClientIdInterceptor clientIdInterceptor, HttpLoggingInterceptor httpLoggingInterceptor) {
return createOkHttpClient(cache, clientIdInterceptor, httpLoggingInterceptor);
}
@Provides
@Singleton
@Named("lastFM-Http")
OkHttpClient provideOkHttpLastFmClient(Cache cache, LastFMInterceptor clientIdInterceptor, HttpLoggingInterceptor httpLoggingInterceptor) {
return createOkHttpClient(cache, clientIdInterceptor, httpLoggingInterceptor);
}
private OkHttpClient createOkHttpClient(Cache cache, Interceptor clientIdInterceptor, HttpLoggingInterceptor httpLoggingInterceptor) {
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.cache(cache)
.addInterceptor(clientIdInterceptor)
.addInterceptor(httpLoggingInterceptor)
.connectTimeout(30, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.writeTimeout(30, TimeUnit.SECONDS)
.build();
return okHttpClient;
}
@Provides
@Singleton
Gson provideGson() {
return GsonFactory.create();
}
@Provides
@Singleton
@Named("searchSoundCloud")
Retrofit provideSearchSoundCloudRetrofit(Gson gson, @Named("soundcloud-Http") OkHttpClient okHttpClient) {
Retrofit searchRetrofit = new Retrofit.Builder()
.baseUrl(Constants.BASE_SOUNDCLOUD_API_URL)
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
return searchRetrofit;
}
@Provides
@Singleton
@Named("chartSoundCloud")
Retrofit provideChartSoundCloudRetrofit(Gson gson, @Named("soundcloud-Http") OkHttpClient okHttpClient) {
Retrofit chartRetrofit = new Retrofit.Builder()
.baseUrl(Constants.BASE_SOUNDCLOUD_API_V2_URL)
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
return chartRetrofit;
}
@Provides
@Singleton
@Named("lastFM")
Retrofit provideLastFmRetrofit(Gson gson, @Named("lastFM-Http") OkHttpClient okHttpClient) {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(Constants.LASTFM_API_URL)
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
return retrofit;
}
}
모듈과 구성 요소를 제공하십시오. – Jacob
@Jacob 모듈과 구성 요소를 추가했습니다. –
@KienViThanh이 문제를 해결할 수 있었습니까? 나는 같은 문제가있다. – Woppi