diff --git a/mastodon/src/main/java/org/joinmastodon/android/api/MastodonAPIController.java b/mastodon/src/main/java/org/joinmastodon/android/api/MastodonAPIController.java index 297f8a9491..af2aa07934 100644 --- a/mastodon/src/main/java/org/joinmastodon/android/api/MastodonAPIController.java +++ b/mastodon/src/main/java/org/joinmastodon/android/api/MastodonAPIController.java @@ -54,6 +54,8 @@ public class MastodonAPIController{ .create(); private static WorkerThread thread=new WorkerThread("MastodonAPIController"); private static OkHttpClient httpClient=new OkHttpClient.Builder() + .connectTimeout(30, TimeUnit.SECONDS) + .writeTimeout(30, TimeUnit.SECONDS) .readTimeout(30, TimeUnit.SECONDS) .build(); @@ -122,15 +124,16 @@ else if(req.token!=null) } if(BuildConfig.DEBUG) - Log.d(TAG, "["+(session==null ? "no-auth" : session.getID())+"] Sending request: "+hreq); + Log.d(TAG, logTag(session)+"Sending request: "+hreq); call.enqueue(new Callback(){ @Override public void onFailure(@NonNull Call call, @NonNull IOException e){ - if(call.isCanceled()) + if(req.canceled) return; if(BuildConfig.DEBUG) - Log.w(TAG, "["+(session==null ? "no-auth" : session.getID())+"] "+hreq+" failed", e); + Log.w(TAG, logTag(session)+""+hreq+" failed", e); + synchronized(req){ req.okhttpCall=null; } @@ -139,10 +142,10 @@ public void onFailure(@NonNull Call call, @NonNull IOException e){ @Override public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException{ - if(call.isCanceled()) + if(req.canceled) return; if(BuildConfig.DEBUG) - Log.d(TAG, "["+(session==null ? "no-auth" : session.getID())+"] "+hreq+" received response: "+response); + Log.d(TAG, logTag(session)+hreq+" received response: "+response); synchronized(req){ req.okhttpCall=null; } @@ -153,7 +156,7 @@ public void onResponse(@NonNull Call call, @NonNull Response response) throws IO try{ if(BuildConfig.DEBUG){ JsonElement respJson=JsonParser.parseReader(reader); - Log.d(TAG, "["+(session==null ? "no-auth" : session.getID())+"] response body: "+respJson); + Log.d(TAG, logTag(session)+"response body: "+respJson); if(req.respTypeToken!=null) respObj=gson.fromJson(respJson, req.respTypeToken.getType()); else if(req.respClass!=null) @@ -175,7 +178,7 @@ else if(req.respClass!=null) return; } if(BuildConfig.DEBUG) - Log.w(TAG, "["+(session==null ? "no-auth" : session.getID())+"] "+response+" error parsing or reading body", x); + Log.w(TAG, logTag(session)+response+" error parsing or reading body", x); req.onError(x.getLocalizedMessage(), response.code(), x); return; } @@ -184,20 +187,19 @@ else if(req.respClass!=null) req.validateAndPostprocessResponse(respObj, response); }catch(IOException x){ if(BuildConfig.DEBUG) - Log.w(TAG, "["+(session==null ? "no-auth" : session.getID())+"] "+response+" error post-processing or validating response", x); + Log.w(TAG, logTag(session)+response+" error post-processing or validating response", x); req.onError(x.getLocalizedMessage(), response.code(), x); return; } if(BuildConfig.DEBUG) - Log.d(TAG, "["+(session==null ? "no-auth" : session.getID())+"] "+response+" parsed successfully: "+respObj); + Log.d(TAG, logTag(session)+response+" parsed successfully: "+respObj); req.onSuccess(respObj); }else{ try{ JsonObject error=JsonParser.parseReader(reader).getAsJsonObject(); - Log.w(TAG, "["+(session==null ? "no-auth" : session.getID())+"] "+response+" received error: "+error); - if(error.has("details")){ + Log.w(TAG, logTag(session)+response+" received error: "+error); if(error.has("details")){ MastodonDetailedErrorResponse err=new MastodonDetailedErrorResponse(error.get("error").getAsString(), response.code(), null); HashMap> details=new HashMap<>(); JsonObject errorDetails=error.getAsJsonObject("details"); @@ -231,7 +233,7 @@ else if(req.respClass!=null) }); }catch(Exception x){ if(BuildConfig.DEBUG) - Log.w(TAG, "["+(session==null ? "no-auth" : session.getID())+"] error creating and sending http request", x); + Log.w(TAG, logTag(session)+"error creating and sending http request", x); req.onError(x.getLocalizedMessage(), 0, x); } }, 0); @@ -244,4 +246,8 @@ public static void runInBackground(Runnable action){ public static OkHttpClient getHttpClient(){ return httpClient; } + + private static String logTag(AccountSession session){ + return "["+(session==null ? "no-auth" : session.getID())+"] "; + } } diff --git a/mastodon/src/main/java/org/joinmastodon/android/api/MastodonAPIRequest.java b/mastodon/src/main/java/org/joinmastodon/android/api/MastodonAPIRequest.java index 09cb7ca60b..99b0beb480 100644 --- a/mastodon/src/main/java/org/joinmastodon/android/api/MastodonAPIRequest.java +++ b/mastodon/src/main/java/org/joinmastodon/android/api/MastodonAPIRequest.java @@ -180,6 +180,8 @@ public String getMethod(){ } public RequestBody getRequestBody() throws IOException{ + if(requestBody instanceof RequestBody rb) + return rb; return requestBody==null ? null : new JsonObjectRequestBody(requestBody); }