Skip to content

Conversation

@ashishkumar468
Copy link
Collaborator

Description (required)

Fixes #3048

What changes did you make and why?
Extracted out response.body().string() to a variable and used that for further references.
Tests performed (required)

Tested betaDebug on Pixel Api 27

* response.body().string() was called more than once and because response body can be huge so OkHttp doesnot store it in memory, it reads it as a stream from network when we need it, which cannot be done without a new request.
Copy link

@pullrequest pullrequest bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ A review job has been created and sent to the PullRequest network.


Check the status or cancel PullRequest code review here.

Copy link

@tests-checker tests-checker bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please add tests to make sure this change works as expected?

@neslihanturan
Copy link
Collaborator

Hi @ashishkumar468 I get this error:

    java.lang.NumberFormatException: For input string: "okhttp3.internal.http.RealResponseBody@86d3e48"
        at java.lang.Integer.parseInt(Integer.java:608)
        at java.lang.Integer.parseInt(Integer.java:643)
        at fr.free.nrw.commons.mwapi.OkHttpJsonApiClient.lambda$getUploadCount$0$OkHttpJsonApiClient(OkHttpJsonApiClient.java:106)
        at fr.free.nrw.commons.mwapi.-$$Lambda$OkHttpJsonApiClient$9ZpGX0U5Zngts_jcRCFls7FtEIM.call(Unknown Source:4)
        at io.reactivex.internal.operators.single.SingleFromCallable.subscribeActual(SingleFromCallable.java:44)
        at io.reactivex.Single.subscribe(Single.java:3438)
        at io.reactivex.internal.operators.single.SingleSubscribeOn$SubscribeOnObserver.run(SingleSubscribeOn.java:89)
        at io.reactivex.Scheduler$DisposeTask.run(Scheduler.java:578)
        at io.reactivex.internal.schedulers.ScheduledRunnable.run(ScheduledRunnable.java:66)
        at io.reactivex.internal.schedulers.ScheduledRunnable.call(ScheduledRunnable.java:57)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:301)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
        at java.lang.Thread.run(Thread.java:764)

Is this expected?

@ashishkumar468
Copy link
Collaborator Author

It is not, I will look into it, thanks for testing this out @neslihanturan
:-)

Copy link

@pullrequest pullrequest bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm


Reviewed with ❤️ by PullRequest

if(null!=responseBody) {
String responseBodyString = responseBody.toString();
if (!TextUtils.isEmpty(responseBodyString.trim())) {
return Integer.parseInt(responseBodyString.trim());
Copy link

@pullrequest pullrequest bot Jul 3, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that this will throw a NumberFormatException if the response body is not actually a number. It might be okay to allow this to just throw, but if crashing is undesired you could use a try/catch and return 0 in the catch.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a catch for NumberFormatException

return Integer.parseInt(response.body().string().trim());
ResponseBody responseBody = response.body();
if(null!=responseBody) {
String responseBodyString = responseBody.toString();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It could be cleaner to set responseBodyString to responseBody.toString().trim(), since you call trim() in both places that it's used below.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@ashishkumar468
Copy link
Collaborator Author

@neslihanturan I have handled the exception as well. Please look into it now :-)

@codecov-io
Copy link

Codecov Report

Merging #3049 into master will increase coverage by 0.24%.
The diff coverage is 0%.

Impacted file tree graph

@@            Coverage Diff            @@
##           master   #3049      +/-   ##
=========================================
+ Coverage    4.41%   4.66%   +0.24%     
=========================================
  Files         259     264       +5     
  Lines       12309   12359      +50     
  Branches     1056    1052       -4     
=========================================
+ Hits          544     577      +33     
- Misses      11726   11739      +13     
- Partials       39      43       +4
Impacted Files Coverage Δ
...fr/free/nrw/commons/mwapi/OkHttpJsonApiClient.java 1.03% <0%> (-0.03%) ⬇️
...ain/java/fr/free/nrw/commons/upload/FileUtils.java 21.62% <0%> (-1.57%) ⬇️
...e/nrw/commons/category/CategoryImagesActivity.java 0% <0%> (ø) ⬆️
...mmons/contributions/ContributionsListFragment.java 0% <0%> (ø) ⬆️
...ee/nrw/commons/media/MediaDetailPagerFragment.java 0% <0%> (ø) ⬆️
...r/free/nrw/commons/contributions/MainActivity.java 0% <0%> (ø) ⬆️
.../free/nrw/commons/bookmarks/BookmarksActivity.java 0% <0%> (ø) ⬆️
.../nrw/commons/category/CategoryDetailsActivity.java 0% <0%> (ø) ⬆️
.../commons/contributions/ContributionViewHolder.java 0% <0%> (ø) ⬆️
...fr/free/nrw/commons/media/MediaDetailFragment.java 0% <0%> (ø) ⬆️
... and 11 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update cc92773...8abc33d. Read the comment docs.

@maskaravivek maskaravivek merged commit fe540eb into commons-app:master Jul 7, 2019
@ashishkumar468 ashishkumar468 deleted the bugfix/getuploadcount branch July 7, 2019 20:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

IllegalStateException while fetching uploadCount in OkHttpJsonApiClient

4 participants