我正在尝试从Firebase获取特定数据。在客户端使用REST API和Retrofit 2。
这是我在Firebase上的JSON结构:
{
"profiles" : {
"-KAG0XPBVNNF_RT55lwV" : {
"GCMTocken" : "rtdjhsrfjt546456",
"firstName" : "P",
"gender" : 1,
"lastName" : "Strongman",
"likes" : 0,
"nickname" : "drake1",
"uid" : "facebook:957484"
}
}
}
请求界面:
@GET("/profiles.json")
Observable<Profile> getProfile(@Query("orderBy") String key, @Query("equalTo") String uid);
根据这个要求,我总是得到:
java.lang.IllegalArgumentException: No Retrofit annotation found. (parameter #1)
for method FirebaseAPI.getProfile
修改
我需要这个请求:
https://incandescent-torch-4.firebaseio.com/profiles.json?orderBy="uid"&equalTo="facebook:95748485767896"
我的改造设置:
String BASE_FIREBASE_URL = "https://incandescent-torch-4.firebaseio.com";
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
.build();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(Constants.BASE_FIREBASE_URL)
.client(okHttpClient)
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.build();
restAPI = retrofit.create(FirebaseAPI.class);
来自RxJava的请求:
RestFirebaseClient.getInstance().getProfile(authData.getUid())
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Subscriber<Profile>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
}
@Override
public void onNext(Profile profile) {
}
});
答案 0 :(得分:3)
I observe in your code request @GET request just put "/" & make sure in you url you have to remove "/" at the end of url like -
like this -
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://incandescent-torch-4.firebaseio.com")
.addConverterFactory(GsonConverterFactory.create())
.build();
@GET("/profiles.json")
Observable<Profile> getProfile(@Query("orderBy") String key, @Query("equalTo") String uid);
这包含在改造的documentation中。
答案 1 :(得分:0)
向Gradle添加Retrofit 2依赖:
compile 'com.squareup.retrofit2:retrofit:2.0.0-beta3'
compile 'com.squareup.retrofit2:converter-jackson:2.0.0-beta3'
compile 'com.squareup.okhttp3:logging-interceptor:3.0.0-RC1'
初始化改造:
OkHttpClient client = new OkHttpClient.Builder()
.addInterceptor(new HttpLoggingInterceptor()
.setLevel(HttpLoggingInterceptor.Level.BODY))
.addInterceptor(new TokenInterceptor())
.addInterceptor(new AuthenticationInterceptor())
.build();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://your_url/")
.addConverterFactory(JacksonConverterFactory.create())
.client(client)
.build();
创建改造服务,例如:
import retrofit2.Call;
import retrofit2.http.GET;
public interface ProfileService {
@GET("profile")
Call<User> get();
}
我使用@GET("profile")
,最后网址为:http://your_url/profile
您可以尝试将Observable<Profile>
更改为Call<Profile>
并从.json
@GET("profiles.json")