【发布时间】:2017-06-29 07:03:22
【问题描述】:
我有一个 Android 客户端和一个使用 ASP Web API 构建的 Rest API。我的问题仅在一个请求中,onSuccess() 和 onFailure() 方法不执行,API 的返回值为 Null。但是请求工作正常(我使用 OkHttp 进行了检查)。但其他请求完美运行。我尝试使用execute() 运行它,但我使用的是NetworkOnMainThreadException。
这是我的 OkHttp 部分:
OkHttpClient.Builder client = new OkHttpClient.Builder();
HttpLoggingInterceptor loggingInterceptor = new
HttpLoggingInterceptor();
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
client.addInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
Request.Builder requestBuilder = request.newBuilder();
requestBuilder.addHeader("Accept", "application/json");
requestBuilder.addHeader("Content-type", " application/json");
if (SharedPrefs.isAuthorized(CApplication.appContext)) {
String token = "bgbqdjsbfjsadnvas";
requestBuilder.addHeader("Authorization", token);
}
requestBuilder.method(request.method(), request.body());
return chain.proceed(requestBuilder.build());
}
}).addInterceptor(loggingInterceptor);
mRetrofit = new Retrofit.Builder()
.baseUrl(ClientConfig.BASE_URL)
.client(client.build())
.addConverterFactory(GsonConverterFactory.create())
.build();
restInterface = mRetrofit.create(RestInterface.class);
这是我的请求:
M_Contact mContact = new M_Contact();
Call<M_Contact> call = rest.getAllContacts(userId);
call.enqueue(new Callback<M_Contact>() {
@Override
public void onResponse(Call<M_Contact> call, Response<M_Contact> response) {
if (null != response) {
if (response.isSuccessful())
Toast.makeText(mContext, "Success: " + response.code(), Toast.LENGTH_SHORT).show()
else
Toast.makeText(mContext, "Something Happened: " + response.code(), Toast.LENGTH_SHORT).show();
}
else Toast.makeText(mContext, "No Data", Toast.LENGTH_SHORT).show();
}
@Override
public void onFailure(Call<M_Contact> call, Throwable t) {
Toast.makeText(mContext, "Failed: " + t.getMessage(), Toast.LENGTH_SHORT).show();
}
});
System.out.println("request finished");
这是我的 build.gradle 文件:
compile('com.squareup.retrofit2:retrofit:2.2.0') {
exclude module: 'okhttp'
}
compile 'com.squareup.okhttp3:okhttp:3.6.0'
compile 'com.squareup.okhttp3:logging-interceptor:3.6.0'
compile 'com.squareup.retrofit2:converter-gson:2.2.0'
这是我的日志文件:
D/OkHttp: --> GET http://192.168.1.58:315/api/Main/RestoreContacts?userId=22 http/1.1
D/OkHttp: Accept: application/json
D/OkHttp: Authorization: upktW1SlDkq0vbl5AjtQeA==
D/OkHttp: --> END GET
D/OkHttp: <-- 200 OK http://192.168.1.58:315/api/Main/RestoreContacts?userId=22 (541ms)
D/OkHttp: Cache-Control: no-cache
D/OkHttp: Pragma: no-cache
D/OkHttp: Content-Type: application/json; charset=utf-8
D/OkHttp: Expires: -1
D/OkHttp: Server: Microsoft-IIS/10.0
D/OkHttp: X-AspNet-Version: 4.0.30319
D/OkHttp: X-Powered-By: ASP.NET
D/OkHttp: Date: Thu, 29 Jun 2017 06:55:40 GMT
D/OkHttp: Content-Length: 842
D/OkHttp: {"userId":22,"contactList":[{"id":4589,"name":"gholam","phoneNumberList":[{"id":6148,"phoneNumber":"+989114514225","isPrimary":"","type":null}]},{"id":4590,"name":"karim","phoneNumberList":[{"id":6149,"phoneNumber":"+9821444","isPrimary":"","type":null},{"id":6150,"phoneNumber":"+9822444","isPrimary":"","type":null}]},{"id":4591,"name":"test1","phoneNumberList":[{"id":6151,"phoneNumber":"+98911791","isPrimary":"","type":null},{"id":6152,"phoneNumber":"+9815246","isPrimary":"","type":null}]},{"id":4592,"name":"hamid","phoneNumberList":[{"id":6153,"phoneNumber":"1564164","isPrimary":"0","type":null},{"id":6154,"phoneNumber":"+619111158","isPrimary":"0","type":null}]}],"contactCount":4,"phoneNumberCount":7,"userDevice":{"model":"Genymotion Custom Phone - 4.2.2 - API 17 - 768x1280","serial":"000000000000000"},"isCacheAvailable":false}
D/OkHttp: <-- END HTTP (842-byte body)
我非常感谢任何形式的帮助。谢谢
更新:
再次结束,请求结果是成功的,但是,这两种方法(onSuccess() 和onFailure())不起作用。他们甚至没有开始。
我刚刚更新了请求部分。就好像call.enqueue(...行开始后,突然跳到System.out.println('...');行,没有经过onSuccess()和onFailure();。
更新
这是我的响应数据结构:
class M_Contact{
public int userId;
public List<ContactModel> contactList;
public int contactCount;
public int phoneNumberCount;
public M_Device userDevice;
public boolean isCacheAvailable;
}
class ContactModel{
public int id;
public String name;
public List<M_ContactPhoneNumber> phoneNumberList;
}
class M_ContactPhoneNumber{
public int id;
public String phoneNumber;
public String isPrimary;
public String label;
}
class M_Device{
public String model;
public String serial;
}
为了便于阅读,我删除了 getter 和 setter 等方法。
【问题讨论】:
-
序列化
M_Contact有错误吗? -
你的问题无法理解。详细贴一些logcat
-
您的日志显示成功,是什么问题?
-
@EricB。不,我检查了它很好
-
@MD,唯一重要的日志猫是我发布的。别人没用
标签: android rest retrofit retrofit2