我有一个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'
这是我的LogFile:
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以及其他方法以便于阅读。
答案 0 :(得分:1)
我知道这很愚蠢,但我只是通过更改请求执行行来解决问题。首先,请求在onCreate()
的最后一行执行,但当我将其删除到onCreate()
的第一行时,它解决了:
如果有人知道发生这种情况的原因,我也很乐意让我知道。
谢谢!