我正在使用Square的Retrofit库来处理应用程序的网络。我注意到库没有明显的原因重复进行HTTP调用,我真的不知道为什么。
我已经验证了对接口方法的调用只从android活动执行一次(ie.userAPI.getUserByFacebookIdAsync())。
有没有人尝试过这个?也许与OKhttp库设置有关?
任何帮助都非常感激。
改装记录
08-18 21:25:56.895 30331-30372/com.package.name D/Retrofit﹕ ---> HTTP GET http://server-instance:8080/server-app//user/facebook?facebookId=00000000
08-18 21:25:56.895 30331-30372/com.package.name D/Retrofit﹕ FacebookId: 00000000
08-18 21:25:56.895 30331-30372/com.package.name D/Retrofit﹕ FacebookToken: XXXXX
08-18 21:25:56.895 30331-30372/com.package.name D/Retrofit﹕ ---> END HTTP (no body)
08-18 21:25:57.434 30331-30372/com.package.name D/Retrofit﹕ ---> HTTP GET http://server-instance:8080/server-app//user/facebook?facebookId=00000000
08-18 21:25:57.434 30331-30372/com.package.name D/Retrofit﹕ FacebookId: 00000000
08-18 21:25:57.434 30331-30372/com.package.name D/Retrofit﹕ FacebookToken: XXXXX
08-18 21:25:57.434 30331-30372/com.package.name D/Retrofit﹕ ---> END HTTP (no body)
08-18 21:25:57.441 30331-30359/com.package.name D/Retrofit﹕ ---> HTTP GET http://server-instance:8080/server-app/user/facebook?facebookId=00000000
08-18 21:25:57.441 30331-30359/com.package.name D/Retrofit﹕ FacebookId: 00000000
08-18 21:25:57.441 30331-30359/com.package.name D/Retrofit﹕ FacebookToken: XXXXX
08-18 21:25:57.441 30331-30359/com.package.name D/Retrofit﹕ ---> END HTTP (no body)
08-19 03:05:59.448 10069-10345/com.package.name D/Retrofit﹕ <--- HTTP 200 http://server-instance:8080/server-app/user/facebook?facebookId=00000000 (770ms)
08-19 03:05:59.448 10069-10345/com.package.name D/Retrofit﹕ : HTTP/1.1 200 OK
08-19 03:05:59.448 10069-10345/com.package.name D/Retrofit﹕ Content-Type: application/json
08-19 03:05:59.448 10069-10345/com.package.name D/Retrofit﹕ Date: Tue, 19 Aug 2014 02:05:59 GMT
08-19 03:05:59.448 10069-10345/com.package.name D/Retrofit﹕ OkHttp-Received-Millis: 1408413959446
08-19 03:05:59.448 10069-10345/com.package.name D/Retrofit﹕ OkHttp-Response-Source: CONDITIONAL_CACHE 200
08-19 03:05:59.448 10069-10345/com.package.name D/Retrofit﹕ OkHttp-Selected-Protocol: http/1.1
08-19 03:05:59.448 10069-10345/com.package.name D/Retrofit﹕ OkHttp-Sent-Millis: 1408413959001
08-19 03:05:59.448 10069-10345/com.package.name D/Retrofit﹕ Server: Apache-Coyote/1.1
08-19 03:05:59.448 10069-10345/com.package.name D/Retrofit﹕ Transfer-Encoding: chunked
08-19 03:05:59.456 10069-10345/com.package.name D/Retrofit﹕ {"id":4,"facebookId":"00000000","gender":"male","city":"Barcelona","country":"Spain","firstName":"John","premium":false}
08-19 03:05:59.456 10069-10345/com.package.name D/Retrofit﹕ <--- END HTTP (390-byte body)
也收到对额外2个呼叫的响应,它们根本不包含在记录样本中。
网络电话(下面包括的活动代码)
userAPI.getUserByFacebookIdAsync(user.getId(), new Callback<User>() {
@Override
public void success(User user, Response response) {}
@Override
public void failure(RetrofitError error) {}
改造API接口方法
@GET("/user/facebook")
void getUserByFacebookIdAsync(@Query("facebookId") String facebookId, Callback<User> cb);
改装API处理程序
public class APIHandler {
static RequestInterceptor requestInterceptor = new RequestInterceptor() {
@Override
public void intercept(RequestFacade request) {
request.addHeader("FacebookId", Constants.FACEBOOK_ID);
request.addHeader("FacebookToken", Constants.FACEBOOK_TOKEN);
}
};
private static RestAdapter restAdapter;
private static RestAdapter getRestAdapter() {
if (restAdapter == null) {
restAdapter = new RestAdapter.Builder().
setRequestInterceptor(requestInterceptor).
setLogLevel(RestAdapter.LogLevel.FULL).
setClient(Constants.OK_CLIENT).
setEndpoint(BaseAPI.getInstance().BASE_URL).build();
}
return restAdapter;
}
public static UserAPI getUserAPI() {
UserAPI userAPI = null;
try {
if (restAdapter == null) {
restAdapter = getRestAdapter();
}
userAPI = restAdapter.create(UserAPI.class);
} catch (Exception e) {
e.printStackTrace();
}
return userAPI;
}}
OKHTTP设置(在应用程序启动时)
int cacheSize = 10 * 1024 * 1024; // 10 MiB
File cacheDirectory = new File(getApplicationContext().getCacheDir().getAbsolutePath(), "HttpCache");
Cache cache = null;
try {
cache = new Cache(cacheDirectory, cacheSize);
} catch (IOException e) {
e.printStackTrace();
}
OkHttpClient okHttpClient = new OkHttpClient();
okHttpClient.setCache(cache);
Constants.OK_CLIENT = new OkClient(okHttpClient);
ANDROID ACTIVITY
public class CurrentUserProfileActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.current_user_profile_activity_layout);
userAPI = APIHandler.getUserAPI();
clubAPI = APIHandler.getClubAPI();
visitAPI = APIHandler.getVisitAPI();
picasso = PabloPicasso.withDebug(this);
userProfilePicture = (ImageView) findViewById(R.id.userProfilePicture);
makeMeRequest(Session.getActiveSession());
userProfilePicture.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
Intent intent = new Intent(getBaseContext(), PictureViewerActivity.class);
intent.putExtra("facebookId", mUser.getFacebookId());
startActivity(intent);
return false;
}
});
}
private void makeMeRequest(final Session session) {
Request request = Request.newMeRequest(session,
new Request.GraphUserCallback() {
@Override
public void onCompleted(GraphUser user, com.facebook.Response response) {
// If the response is successful
if (session == Session.getActiveSession()) {
if (user != null) {
userAPI.getUserByFacebookIdAsync(user.getId(), new Callback<User>() {
@Override
public void success(User user, Response response) {
mUser = user;
}
@Override
public void failure(RetrofitError error) {
Log.d(TAG, "userApi call to get User object from server failed!");
}
});
}
}
if (response.getError() != null) {
Intent introActivityIntent = new Intent(CurrentUserProfileActivity.this, IntroActivity.class);
introActivityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(introActivityIntent);
finish();
}
}
}
);
request.executeAsync();
}}
答案 0 :(得分:2)
有关于媒体的帖子,解释了所有内容
您只能为整个
OkHttpClient
实例全局禁用请求重试。这是通过使用OkHttpClient.Builder
并将retryOnConnectionFailure
设置为false来完成的。