Retrofit2未找到Retrofit注释。 (参数#1)

时间:2018-05-28 11:58:45

标签: android retrofit2

我创建了一个非常简单的改造服务,并且在我的@GET调用中遇到了IllegalArgumentException。出于某种原因,我的@Query注释没有被识别或接受,但从我所看到的,绝对没有错。请看看,告诉我,如果我错过了什么......谢谢。

此处构建的改造对象

public static Retrofit getRequest(@NonNull Context context){

    OkHttpClient client = new OkHttpClient();
Retrofit retrofit = new 
    Retrofit.Builder().baseUrl("https://blah.blah.com/").client(client).addConverterFactory(GsonConverterFactory.create()).build();
    return retrofit;
}

服务

public interface Request
{
    String SEARCHPATH = "...";
final static String KEY = "...";

@GET(SEARCHPATH)
Call<Results> getRes(@Query("api-key") String key, @Query("q") String q);
}

呼叫

private void initSearch(String term){
    retrofit = RequestBuilder.getRequest(this);
    req = retrofit.create(Request.class);
result = req.getRes(Request.KEY, "string");
result.enqueue(new Callback(){
    @Override
    public void onResponse(Call<Results> call, Response<Results> response){
        Log.d("data", String.valueOf(response.body().toString()));
    }
        @Override
    public void onFailure(Call<Results> call, Throwable t) {
    Log.d("data", t.getMessage());
    }
});
}

Gradle build

android {
compileSdkVersion 23
buildToolsVersion "23.0.1"

defaultConfig {
    applicationId "com.mycompany.newssearch"
    minSdkVersion 15
    targetSdkVersion 23
    versionCode 1
    versionName "1.0"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 
'proguard-rules.pro'
    }
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:support-compat:27.1.1'
compile 'com.squareup.retrofit2:retrofit:2.3.0'
compile 'com.squareup.retrofit2:converter-gson:2.3.0'
compile 'com.squareup.okhttp:okhttp:2.7.0'
compile 'com.android.support:support-core-utils:27.1.1'
}

异常

java.lang.IllegalArgumentException: No Retrofit annotation found. 
(parameter #1)

1 个答案:

答案 0 :(得分:0)

以这种方式进行查询..

    @GET("details/json")
Call<PlaceDetailsResponse> getPlaceDetailsData(@Query("placeid") String placeid,@Query("key") String key);

确保key中的define query与..中的url相同。

上面的查询使url像json?placeid = ...&amp; key =