Android - Retrofit 2错误:"需要HTTP方法注释"

时间:2016-04-16 19:34:48

标签: android

有一些问题,但似乎没有任何效果。 我试图获取一个Json对象形式的网页内容。

将此添加到 proguard-rules.pro

<pre><code>-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.** { *; }  </pre></code>

我的 build.gradle

<pre><code> apply plugin: 'com.android.application'
apply plugin: 'realm-android'

android {
    packagingOptions {
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
    }
    compileSdkVersion 23
    buildToolsVersion "23.0.3"
    useLibrary 'org.apache.http.legacy'

    defaultConfig {
        applicationId "com.myApplication"
        minSdkVersion 19
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:24.0.0-alpha1'
    compile 'com.android.support:design:24.0.0-alpha1'
    compile 'com.android.support:support-v4:24.0.0-alpha1'
    compile 'com.google.android.gms:play-services-maps:8.4.0'
    compile 'com.google.android.gms:play-services-appindexing:8.4.0'
    compile 'org.apache.commons:commons-lang3:3.4'
    compile 'com.squareup.okhttp3:okhttp:3.2.0'
    compile 'com.squareup.retrofit2:retrofit:2.0.2'
    compile 'com.google.code.gson:gson:2.4'
    compile 'org.glassfish:javax.annotation:10.0-b28'
}</pre></code>

接口:

<pre><code> public interface LoginInterface {

    @GET("?email=myEmail@gmailcom&password=opensesame")
        Boolean getData(Callback<LoginJsonParser> cb);
}</pre></code>

LoginJsonParser class:

<pre><code>    import javax.annotation.Generated;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

@Generated("org.jsonschema2pojo")
public class LoginJsonParser {

    @SerializedName("success")
    @Expose
    private Integer success;

    @SerializedName("message")
    @Expose
    private String message;

    /**
     *
     * @return The success
     */
    public Integer getSuccess() {
        return success;
    }

    /**
     *
     * @param success The success
     */
    public void setSuccess(Integer success) {
        this.success = success;
    }

    /**
     *
     * @return The message
     */
    public String getMessage() {
        return message;
    }

    /**
     *
     * @param message The message
     */
    public void setMessage(String message) {
        this.message = message;
    }

}</pre></code>

这是错误:

<pre><code>java.lang.RuntimeException: An error occurred while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:309)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
Caused by: retrofit.RetrofitError: LoginInterface.isLoginValid: HTTP method annotation is required (e.g., @GET, @POST, etc.).
at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:400)
at retrofit.RestAdapter$RestHandler.invoke(RestAdapter.java:240)
at java.lang.reflect.Proxy.invoke(Proxy.java:393)
at $Proxy0.isLoginValid(Unknown Source)
at com.myApplication.activity.Login$UserLoginTask.doInBackground(Login.java:344)
at com.myApplication.activity.Login$UserLoginTask.doInBackground(Login.java:318)
at android.os.AsyncTask$2.call(AsyncTask.java:295)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234) 
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) 
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) 
at java.lang.Thread.run(Thread.java:818) 
Caused by: java.lang.IllegalArgumentException: LoginInterface.isLoginValid: HTTP method annotation is required (e.g., @GET, @POST, etc.).
at retrofit.RestMethodInfo.methodError(RestMethodInfo.java:107)
at retrofit.RestMethodInfo.parseMethodAnnotations(RestMethodInfo.java:179)
at retrofit.RestMethodInfo.init(RestMethodInfo.java:117)
at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:294)
at retrofit.RestAdapter$RestHandler.invoke(RestAdapter.java:240) 
at java.lang.reflect.Proxy.invoke(Proxy.java:393) 
at $Proxy0.isLoginValid(Unknown Source) 
at com.myApplication.activity.Login$UserLoginTask.doInBackground(Login.java:344) 
at com.myApplication.activity.Login$UserLoginTask.doInBackground(Login.java:318) 
at android.os.AsyncTask$2.call(AsyncTask.java:295) 
at java.util.concurrent.FutureTask.run(FutureTask.java:237) 
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234) 
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) 
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) 
at java.lang.Thread.run(Thread.java:818) </code></pre>

1 个答案:

答案 0 :(得分:1)

使用错误的“@GET”。

我最初面临同样的问题。从Retrofit1迁移到Retrofit2时非常常见的错误。在界面中,确保您使用正确的GET导入语句。我没有意识到我正在使用retrofit.http中的@GET而不是来自retrofit2.http的@GET,将注释从retrofit.http更改为retrofit2.http将完成这项工作。