OkHttp connection leak log line even when OkHttp is not a dependency - android

I keep seeing the following log line in Logcat while I use my app:
19098-19147/<package> W/OkHttpClient: A connection to <my server> was leaked. Did you forget to close a response body?
I did some research on this bug and found that it can happen when you do things like forget to close a response body in an interceptor. I commented out all of interceptors to see if one of them was causing this issue, but I still saw the log line. I eventually commented out all uses of OkHttp and I still somehow got the error. I even went as far as to removing all OkHttp dependencies from my Gradle file and added an explicit line to make sure that it was excluded as a transitive dependency. I ran gradle app:dependencies in order to generate my dependency tree so I could make sure that OkHttp was not included. Somehow I am STILL seeing this log line. I don't understand how this is possible.
Does anyone know of any common libraries that may have copied and pasted this log line out of the OkHttp library and into theirs? I searched all of the source code of my other dependencies, but didn't find a similar log line in any of them.
Could this have something to do with the new profiling code that Android Studio injects? See this article for more information.
Update: Turns out that my Fresco dependency must have been using OkHttp provided by the system or something like that. #Selvin commented that Android uses it internally. Anyways, apparently when Fresco receives an HTTP response with an error when trying to load an image (in my case HTTP response code 401), it logs this error. I don't see any good way for handling HTTP errors with Fresco/OkHttp. I opened the following issue with Fresco: HTTP error response when loading image results in leaked connection #1983

I know it's very late response but maybe anyone is still getting crazy with this issue and finally I found out what happened. Yes: OkHttp is used internally in HttpURLConnection/HttpsURLConnection components since Android 4.4 so you can get these boring messages in logcat, even if you're not using OkHttp library directly in your project.
The problem is generated by 2 factors:
the socket is reused by the urlConnection object, trying to
optmizing performances on multiple requests on the same host.
the response stream is not correctly closed (see below how to solve)
To prevent these log lines or simply take more control on the flow I made some changes to my code and finally solved this issue.
I disabled persistent connections setting Connection: close header. Default is Connection: keep-alive
Before reading the content (body) of the response, check the http response code. As well described in this article if the response code is >= 400 you got an unsuccess: you have not to read the connection InputStream getInputStream() but the ErrorStream getErrorStream() and close it by calling .close() method on the right stream. The connection leak is here. Finally remember to disconnect() the urlConnection object.

Related

Tasker App: Why my HTTP POST is not sending parameters?

I'm making a simple HTTP POST request using Tasker app, it's working fine but somehow it's not sending my arguments. HTTP GET is working fine. What am I doing wrong?
The only thing the server does is JSON stringify all received parameters and return them like: Text: { parameters }
HTTP GET is working:
HTTP POST works, it returns the response "text: {}" but doesn't return any parameter:
Now sometimes it shows "Bad Request Error 400"
I've tested it with another server and this issue persists...
Here's a fiddle to send get and post requests to the same server and both work.
I've exported my task as xml for you to test: You can download it here
Thanks in advance guys!
I have done a little testing with what you have supplied. It appears that your post parameters are making it to the server but %HTTPR is not getting populated. The Tasker documentation says that this variable will be populated if the response is "text-based", but this does not seem to be the case. I may not know what Tasker considers to be "text-based", but I have tried "text/plain" and "text/html" with no luck.
The work-around that I have come up with is to put the response into a file using the "Output File" field of the post task. (Leave "Content Type" blank or this won't work.) You can then read the file into a variable and do what you need to.
This is either a defect in Tasker, incorrect documentation, or just a misunderstanding of what it takes to make it work. This work-around, however, will get you what you need.
Here is a link to a GitHub repository with the updated task export. You may need to change the output file name/location to work with your device.
Update:
Since I posted the above solution, I have run across a Tasker plugin called "RESTask for Tasker". Evidently, Tasker has enough issues with HTML requests that a separate plugin was needed. I have tested this plugin with POST and it does work, so this is another way to go. The plugin is available on the Google Play store.

Retrofit incorrect response error on android

When i call any webService with expired token,the service returns code 498 with error message "your tokken is expired"..BUT the retrofit gives the response code 500 with error message " internel server error".
response.code() //500 instead of 498
I tested the same url with postman and its working fine but the issue is only with retrofit.
I am using same services on IOS with AFNetworking and its also working fine there.
Any body can help to figure out why this is happening? thanks in advance.
This is not a full answer, but it's too big for a comment.
5xx codes are server errors, this means that the server is crashing, not the app nor is retrofit buggy. The issue most likely is in the server, but can be caused by retrofit - yes, that's true.
My experience with all the questions that say: "I tried this insert random network call here with postman and it works, but retrofit returns 500" is because postman adds headers by default, which retrofit doesn't. The server implementation then expects these headers to be set and due to a faulty implementation crashes if said headers are missing.
I would check the headers retrofit is sending and the headers postman is sending and compare both and make sure which one crashes the server.
It can also be that OkHttp (Retrofit uses OkHttp under the hood) is adding some headers which the server cannot cope with. This would be stranger to me, but not impossible. I think it adds for example by default gzip and some servers might not handle this correctly.
If you have access to the server, than it might even be worth checking there the logs. They might point you right away to the issue.
I'm sorry but I cannot point you directly to the problem. These are just tips to get you started. Hope they help.

How to handle http error code 303 in volley

I tried to override deliverError method but still facing the problem.
My request is https but on error response location I am receiving an http url.
What is significance of location in error response?
thanks.
Use this version of volley library here: https://github.com/samkirton/android-volley
Add to your gradle :
compile 'com.mcxiaoke.volley:library:1.0.19'
I hope this should resolve your issue
I'm not sure if I understand your question.
For sdk version greater than or equal to 9, volley internally use HttpURLConnection. As other related answers mentioned, HttpURLConnection will not redirect to a schema with different protocol. That means a 30x from https to http or vise versa is not automatically handled in volley. You are going to get an Error in deliverError if such a response is returned.
The version from https://github.com/samkirton/android-volley seems to have dealt with 301, 302, not all 30x. But I'm not sure.
By the way, 30x is generally treated as error in almost all libs that implements http. This should not be a problem. You may consider retrieving the url from location and making a request by your own

SendLogsRequestListener failure in worklight

I am re-implementing an existing worklight system. We have already have a hybrid version. We have decided to re-implement the Client side in pure native. The native client seems work fine.
However, the server seems to be quite unstable - sometimes we don't get a server response after invoking an adapter, sometimes we do get. I have checked the DDMS/Android console. And a Error/Exception was found. Any comments would be appreciated!
com.worklight.common.Logger$SendLogsRequestListener(4274): Logger$SendLogsRequestListener.onFailure in Logger.java:1726 :: Failed to POST data from file /data/data/${APP_NAME}/files/analytics.log.0.send due to: HTTP response code: 500
On a successful adapter call or WLClient connection to the server, the client will attempt to automatically send logs to the server. It looks like something bad is happening on the autosend for adapters. A temporary workaround would be to disable this auto sending behavior by calling
WL.Logger.setAutoSendLogs(false)
But you can only use this call if you do not wish for logs to be sent automatically. If you do wish to have this working properly, I'd suggest opening a PMR so that it can be determined if this is an error in your application or an actual defect.

Android Retrofit Causes Socket Timeout Exception

I am making a POST call to a tomcat server running Struts2 using the retrofit library on an Android Galaxy S3 (/Nexus 7) device. The POST call fails. The tomcat log shows Socket timeout exception.
The same POST using the exact same headers done via curl does not have any issues. I verified that the data on the wire matches using charles proxy.
Any tips/ideas on debugging this issue.
The post call is as follows
#POST(Constants.URL_GET_ORDER_LIST_BASE)
void getCardOrderList(#Body GetOrderListRequest getOrderListRequest, Callback<GetOrderListResponse> cbGetOrderListResponse);
Please let me know if I need to add more information to explain this better.
Adding Square's OKHTTP library into the libs folder fixed the issue.
I was having SocketTimeoutExceptions too. Pay attention to always add the final slash to your POST call.
Example:
BAD
#POST("/customers")
GOOD
#POST("/customers/")
My mistake was just this :)

Categories

Resources