Muj*_*ori 2 java android http retrofit
我正在使用com.squareup.retrofit2:retrofit:2.2.0
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
httpClient.addInterceptor(logging);
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://localhost:9000")
.addConverterFactory(GsonConverterFactory.create())
.client(httpClient.build())
.build();
final UserService service = retrofit.create(UserService.class);
Call<User> userCall = service.createUser(user)
Run Code Online (Sandbox Code Playgroud)
这是问题所在:当我运行execute它时,它会生成REST API请求但是当我使用enqueue它时什么都不做(没有例外,没有日志)
userCall.execute(); //this work
//this does not work
userCall.enqueue(new Callback<User>() {
@Override
public void onResponse(Call<User> call, Response<User> response) {
// no response
}
@Override
public void onFailure(Call<User> call, Throwable t) {
//nothing here
}
});
Run Code Online (Sandbox Code Playgroud)
好,
问题是因为我正在使用模拟器并尝试调用localhost但是,如果你想调用localhost你必须使用这个ip 10.0.2.2
这项execute工作是因为我作为单元测试运行但是当它运行时enqueue我认为它需要在android平台上运行.
请参阅此 https://developer.android.com/studio/run/emulator.html#networkaddresses
| 归档时间: |
|
| 查看次数: |
4897 次 |
| 最近记录: |