尝试记录改进JSON响应时NoSuchMethodError

时间:2016-08-17 02:38:26

标签: java android json rest retrofit

我正在第一次尝试记录Retrofit响应,我知道OKHttp会自动包含在Retrofit 2.x版本中。

我创建了我的代码并收到了包含的错误:

的活动:

public class MainActivity extends AppCompatActivity {

private EditText mSearchTerm;
private Button mRequestButton;
private String mQuery;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mSearchTerm = (EditText) findViewById(R.id.ediText_search_term);
    mRequestButton = (Button) findViewById(R.id.request_button);
    mRequestButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            mQuery = mSearchTerm.getText().toString();
            HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
            interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
            OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();
            Retrofit retrofit = new Retrofit.Builder()
                    .baseUrl("http://api.flickr.com/services/rest/")
                    .client(client)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();


            ApiInterface apiInterface = retrofit.create(ApiInterface.class);
            Call<List<Photo>> call = apiInterface.getPhotos(mQuery);
            call.enqueue(new Callback<List<Photo>>() {
                @Override
                public void onResponse(Call<List<Photo>> call, Response<List<Photo>> response) {

                }

                @Override
                public void onFailure(Call<List<Photo>> call, Throwable t) {

                }
            });

        }
    });



}

//Synchronous vs. Asynchronous
public interface ApiInterface {
    @GET("?&method=flickr.photos.search&api_key=1c448390199c03a6f2d436c40defd90e&user_id=tccpg288&format=json")  //
    Call<List<Photo>> getPhotos(@Query("q") String photoSearchTerm);
    }

  }

错误:

  java.lang.NoSuchMethodError: No virtual method log(Ljava/lang/String;)V in class Lokhttp3/internal/Platform; or its super classes (declaration of 'okhttp3.internal.Platform' appears in /data/app/com.troychuinard.flickr_test-1/base.apk)
                                                                            at okhttp3.logging.HttpLoggingInterceptor$Logger$1.log(HttpLoggingInterceptor.java:109)
                                                                            at okhttp3.logging.HttpLoggingInterceptor.intercept(HttpLoggingInterceptor.java:157)
                                                                            at okhttp3.RealCall$ApplicationInterceptorChain.proceed(RealCall.java:190)
                                                                            at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:163)
                                                                            at okhttp3.RealCall.access$100(RealCall.java:30)
                                                                            at okhttp3.RealCall$AsyncCall.execute(RealCall.java:127)
                                                                            at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
                                                                            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
                                                                            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
                                                                            at java.lang.Thread.run(Thread.java:818)

的build.gradle:

    dependencies {

        compile fileTree(dir: 'libs', include: ['*.jar'])
        testCompile 'junit:junit:4.12'
        compile 'com.android.support:appcompat-v7:24.1.1'
        compile 'com.google.code.gson:gson:2.6.2'
        compile 'com.squareup.retrofit2:retrofit:2.1.0'
        compile 'com.squareup.retrofit2:converter-gson:2.1.0'
        compile 'com.squa

reup.okhttp3:logging-interceptor:3.2.0'
}

1 个答案:

答案 0 :(得分:1)

我认为Retrofit 2.1.0依赖于OkHttp 3.3.0,因此您可以尝试使用相同版本的Logging Interceptor

尝试编译&#39; com.squareup.okhttp3:logging-interceptor:3.3.0&#39;或编译&#39; com.squareup.okhttp3:okhttp:3.4.1&#39;