android Retrofit 2 HTTP方法注解是必需的(例如,@GET、@POST等)

cxfofazt  于 2023-05-12  发布在  Android
关注(0)|答案(5)|浏览(104)

我的Retrofit配置有什么问题?当我用OkHttpClient添加基本身份验证时,我遇到了这个错误,但是当我使用默认客户端而不使用拦截器时,它可以正常工作。还是我的Gradle依赖项有问题..?

E/AndroidRuntime﹕ FATAL EXCEPTION: main
        java.lang.IllegalArgumentException
    : HTTP method annotation is required (e.g., @GET, @POST, etc.).
            for method APIService.getRegAccrDetails
                    at retrofit.Utils.methodError(Utils.java:177)
                    at retrofit.Utils.methodError(Utils.java:167)
                    at retrofit.RequestFactoryParser.parseMethodAnnotations(RequestFactoryParser.java:135)
                    at retrofit.RequestFactoryParser.parse(RequestFactoryParser.java:59)
                    at retrofit.MethodHandler.create(MethodHandler.java:30)
                    at retrofit.Retrofit.loadMethodHandler(Retrofit.java:151)
                    at retrofit.Retrofit$1.invoke(Retrofit.java:132)
                    at $Proxy0.getRegAccrDetails(Native Method)
                    at alvin.test.myapplication.MainActivity.liferayAccess(MainActivity.java:136)
                    at alvin.test.myapplication.MainActivity.access$000(MainActivity.java:28)
                    at alvin.test.myapplication.MainActivity$1.onClick(MainActivity.java:49)
                    at android.view.View.performClick(View.java:3511)
                    at android.view.View$PerformClick.run(View.java:14105)
                    at android.os.Handler.handleCallback(Handler.java:605)
                    at android.os.Handler.dispatchMessage(Handler.java:92)
                    at android.os.Looper.loop(Looper.java:137)
                    at android.app.ActivityThread.main(ActivityThread.java:4424)
                    at java.lang.reflect.Method.invokeNative(Native Method)
                    at java.lang.reflect.Method.invoke(Method.java:511)
                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
                    at dalvik.system.NativeStart.main(Native Method)

下面是要调用的API服务

@GET("Triu-services-portlet.regaccrdetails/get-all-reg-accr-details-by-num-branch-code/num/{num}/branch-code/{branch-code}")
    public Observable<List<RegAccrDetails>> getRegAccrDetails(@Path("num") String num, @Path("branch-code")String branchCode);

我的OkHttpClient拦截器

private static OkHttpClient createOkHttpClient() {
   String username = "test@liferay.com";
   String password = "TEST";
   String credentials = username + ":" + password;
   final String basic =
           "Basic " + Base64.encodeToString(credentials.getBytes(), Base64.DEFAULT);//no_wrap


    OkHttpClient client = new OkHttpClient();
    client.interceptors().add(new Interceptor() {
        @Override
        public Response intercept(Chain chain) throws IOException {
            Response response = chain.proceed(chain.request());

            Request original = chain.request();

            // Customize the request
            Request request = original.newBuilder()
                    .header("Authorization", basic)
                    .header("Accept", "application/json")
                    //.header("Authorization", "auth-token")//add token for service A4oslsSXZxfbLdk
                    .method(original.method(), original.body())
                    .build();

            response = chain.proceed(request);

            // Customize or return the response
            return response;
        }
    });

   return client;
}

这是我的API调用

private void liferayAccess(){
    Log.d("liferayAccess", "Entered");
    APIService service =  ServiceGenerator.createService(APIService.class);
    Observable<List<RegAccrDetails>> liferayResponse = service.getRegAccrDetails("004589209", "001");

    liferayResponse.subscribeOn(Schedulers.newThread()).map(listResponse -> "response index 0 " + listResponse.get(0).getRegNum())
            .subscribe( response-> Log.d("Liferay Num", response),
                        error -> Log.d("Error", error.toString())
                    );
}

这是我的Gradle依赖项

compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.1.1'

compile 'io.reactivex:rxjava:1.0.16'
compile 'io.reactivex:rxandroid:1.1.0'
compile 'com.squareup.retrofit:adapter-rxjava:2.0.0-beta2'
compile 'com.squareup.retrofit:converter-gson:2.0.0-beta2'

//compile 'com.squareup.retrofit:converter-simplexml:2.0.0-beta2'
/*compile 'com.squareup.okhttp:okhttp:2.2.0'*/
compile ('com.squareup.retrofit2:retrofit:2.0.0-beta3') {//com.squareup.retrofit2:retrofit:2.0.0-beta3
    // exclude Retrofit’s OkHttp peer-dependency module and define your own module import
    //exclude module: 'okhttp'
}
compile 'com.squareup.okhttp3:okhttp:3.0.0'
compile 'com.squareup.okhttp3:logging-interceptor:3.0.1'

这是我的App Gradle文件

apply plugin: 'com.android.application'
apply plugin: 'me.tatarka.retrolambda'
android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    packagingOptions {
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/NOTICE'
    }



    defaultConfig {
        applicationId "alvin.test.myapplication"
        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:appcompat-v7:23.1.1'

    compile 'io.reactivex:rxjava:1.0.16'
    compile 'io.reactivex:rxandroid:1.1.0'
    compile 'com.squareup.retrofit:adapter-rxjava:2.0.0-beta2'
    compile 'com.squareup.retrofit:converter-gson:2.0.0-beta2'

    //compile 'com.squareup.retrofit:converter-simplexml:2.0.0-beta2'
    /*compile 'com.squareup.okhttp:okhttp:2.2.0'*/
    compile ('com.squareup.retrofit2:retrofit:2.0.0-beta3') {//com.squareup.retrofit2:retrofit:2.0.0-beta3
        // exclude Retrofit’s OkHttp peer-dependency module and define your own module import
        //exclude module: 'okhttp'
    }
    compile 'com.squareup.okhttp3:okhttp:3.0.0'
    compile 'com.squareup.okhttp3:logging-interceptor:3.0.1'

    //compile 'com.squareup.retrofit:retrofit:2.0.0-beta2'
    compile 'com.google.android.gms:play-services-gcm:7.3.0'
    compile 'com.google.android.gms:play-services:7.8.0'
}
retrolambda {
    jdk "C:\\Program Files\\Java\\jdk1.8.0_20"
}

我的亲卫。我也尝试添加和删除它,但同样的错误日志发生

-keepattributes *Annotation*
-keep class retrofit.** { *; }
-keepclasseswithmembers class * {
@retrofit.http.* <methods>; }
-keepattributes Signature

-keep class com.google.gson.** { *; }
-keep class com.google.inject.** { *; }
-keep class org.apache.http.** { *; }
-keep class org.apache.james.mime4j.** { *; }
-keep class javax.inject.** { *; }
-keep class retrofit.** { *; }
jjjwad0x

jjjwad0x1#

* 使用错误的“@GET”*

这可能会帮助一些来自Retrofit 1的人,我得到了同样的错误,修复很简单。在我的界面中,我没有意识到我使用的是来自reflect.http的@GET,而不是来自retrofit2.http的@GET,我所需要做的就是将注解从reflect.http更改为retrofit2.http。

rsl1atfo

rsl1atfo2#

问题

您正在使用2.0.0-beta2版本的Retrofit插件,它依赖于2.0.0-beta2版本的Retrofit核心,该核心仍然存在于retrofit包中,并位于com.squareup.retrofit坐标。

compile 'com.squareup.retrofit:adapter-rxjava:2.0.0-beta2'
compile 'com.squareup.retrofit:converter-gson:2.0.0-beta2'

然后导入2.0.0-beta3版本的Retrofit核心,它位于retrofit2包中,坐标为com.squareup.retrofit2。基本上,它可以与2.0.0-beta2甚至1.x版本一起使用。

compile 'com.squareup.retrofit2:retrofit:2.0.0-beta3'

您根本没有真正使用2.0.0-beta3,因为它与2.0.0-beta2插件不兼容,并且会出现编译时错误。检查您的导入以进行验证。
发生的情况是(很可能)您使用了retrofit包中的所有内容,除了来自retrofit2包的@GET类。尽管它们的名称相同,但这些类并不相同。
阅读更多关于why this happened

解决方案

更新依赖项并修复导入。

compile 'com.squareup.retrofit2:retrofit:2.0.0'
compile 'com.squareup.retrofit2:adapter-rxjava:2.0.0'
compile 'com.squareup.retrofit2:converter-gson:2.0.0'

参见changelog

3xiyfsfu

3xiyfsfu3#

它看起来像你正在使用proguard,它正在剥离注解。要从中保存,请将以下行添加到proguard-rules.pro

-keepattributes *Annotation*
-keep class retrofit.** { *; }
-keepclasseswithmembers class * {
@retrofit.http.* <methods>; }
-keepattributes Signature

如果不使用proguard,请确保您没有在应用程序中编写类似以下内容的内容build.gradle

buildTypes {
    release {
        minifyEnabled true
        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
    }
}

注意:Android默认情况下通常不会附带许多javax.annotation库。
如果不是这样,请尝试将其添加到gradle依赖项(build.gradle)中

provided 'org.glassfish:javax.annotation:10.0-b28'
uqzxnwby

uqzxnwby4#

正如Eugen Pechanec所说,问题通常在于翻新和翻新之间的冲突。在我的例子中,错误“HTTP method annotation is required @GET @POST”是由于使用了错误的 HTTPLoggingInterceptor 结构生成器而导致的。
因此,请确保您正在使用okhttp 3retrofit 2
所以正确的结构看起来像THIS

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

...

Retrofit provideRetrofit(){

// get base url for endpoint
String endpointUrl = BuildConfig.apiEndpointUrl;

// add logging interceptor
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder().addInterceptor(logging).build();

// build retrofit instance
Retrofit retrofit = new Retrofit.Builder()
        .baseUrl(endpointUrl)
        .client(client)
        .addConverterFactory(GsonConverterFactory.create())
        .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
        .build();

return retrofit;
}

而app/build.gradle

compile 'com.squareup.retrofit2:retrofit:2.0.0-beta4'
compile 'com.squareup.retrofit2:adapter-rxjava:2.0.0-beta4'
compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta4'
compile 'com.squareup.okhttp3:logging-interceptor:3.2.0'
compile 'com.squareup.okhttp3:okhttp:3.2.0'
fzwojiic

fzwojiic5#

在我的情况下,一切都很完美,除了我导入了错误的@POST(在导入时,我错误地在项目中创建了POST注解),当我删除它时,它工作了。
所以我的建议是,一旦检查进口正确。

相关问题