尝试记录改造JSON响应时出现NoSuchMethodError [英] NoSuchMethodError When Trying to Log Retrofit JSON Response
问题描述
在记录Retrofit响应时,我的第一个步骤是,我知道OKHttp会自动包含在Retrofit 2.x的发行版中.
I am taking my first stab at logging a Retrofit response, and I understand that OKHttp is automatically included in releases of Retrofit 2.x.
我已经创建了代码,并收到了包含的错误:
I have created my code and am receiving the included error:
活动:
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:
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'
}
推荐答案
我认为Retrofit 2.1.0依赖于OkHttp 3.3.0,因此您可以尝试使用相同版本的Logging Interceptor
I think Retrofit 2.1.0 relies on OkHttp 3.3.0, so you may try using same version of Logging Interceptor
尝试编译'com.squareup.okhttp3:logging-interceptor:3.3.0'或编译'com.squareup.okhttp3:okhttp:3.4.1'
try compile 'com.squareup.okhttp3:logging-interceptor:3.3.0' or compile 'com.squareup.okhttp3:okhttp:3.4.1'
这篇关于尝试记录改造JSON响应时出现NoSuchMethodError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!