I have two different domain urls named A.com and B.com.
This is the code to send request: the asyncHttpClient is the same one.
asyncHttpClient.post(context, "A.com", aParams, aResponseHandler);
asyncHttpClient.post(context, "B.com", bParams, bRsponseHandler);
There is a chance to happen: get "B.com" response in aResponseHandler, although it doesn't happen frequently. Why does it mistake the response ?
Whehter i perform either of them solely, it's ok.
I find another guy has the same question.
The issue you linked on github seems to be closed.
The original poster wrote the following:
in my local tests ,no way to reproduce this problem, but truly
happened.....
It seems unlikely that this would occur, and the issue cannot be reproduced. Personally, I wouldn't worry about this happening in your application. You could write a conditional statement to check and throw an exception if the wrong ResponseHandler is triggered.
Related
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.
I make some http requests using Okhttp to download a JSON from my server.
For me and all my alpha testers everything works fine, but out in the wild I
experience some exceptions that occur, when I try to parse the response using
Gson.
As far as I understand the exceptions occur, because the connection gets redirected, in situations where the user is at an airport or using a limited
WIFI connection in an hotel etc.
The exception:
Exception java.lang.IllegalStateException: closed
okhttp3.internal.http.Http1xStream$FixedLengthSource.read (Http1xStream.java:378)
okio.Buffer.writeAll (Buffer.java:956)
okio.RealBufferedSource.readByteArray (RealBufferedSource.java:92)
okhttp3.ResponseBody.bytes (ResponseBody.java:83)
okhttp3.ResponseBody.string (ResponseBody.java:109)
[xxxx].BackendFilter.handleResponse (BackendFilter.java:131)
[xxxx].BackendFilter.access$300 (BackendFilter.java:30)
[xxxx].BackendFilter$2.onResponse (BackendFilter.java:114)
okhttp3.RealCall$AsyncCall.execute (RealCall.java:133)
okhttp3.internal.NamedRunnable.run (NamedRunnable.java:32)
java.util.concurrent.ThreadPoolExecutor.runWorker (ThreadPoolExecutor.java:1133)
java.util.concurrent.ThreadPoolExecutor$Worker.run (ThreadPoolExecutor.java:607)
java.lang.Thread.run (Thread.java:762)
From my logs in firebase I can see that the URL of the response is different
to the URL I used to set in the request.
i.e. I get: http://www.tunisietelecom.tn/tt/themes/html/TT_Theme/infosolde.html
It basically says:
Dear Customer,
Your plan is exhausted, to continue to benefit from your INTERNET
offer, dial * 140 # for packages via your phone or * 145 # for Data
key packages.
So I assume this specific user got redirected, because his data package
got expired.
The response code nevertheless is a 200 and my application tries to parse
the response body, which fails obviously. Not with the excepetion I
expected, but it fails anyhow. (Can anybody explain to me, why this excepetion
occurs. rather than a Gson JsonSyntaxException?)
I read multiple articles on stackoverflow and did a google search beforehand,
but can anybody tell me how to handle this kind of redirects?
What is the best approach to smoothly dismiss this request, if I (how?)
recognize a redirect.
tl;dr
My approach now would be to disable redirects in the first place by setting OkHttpClient client = new OkHttpClient().newBuilder().followRedirects(false).followSslRedirects(false).build();, but I am not sure (and familar with this topic), if there are
situations where redirects are total fine and I want to allow them.
The next approach, without disabling redirects would be to investigate the
response. If the method response.isRedirect() returns true and the
url of the request (response.request.url()) is any different from
my requested url, I would smoothly dismiss the request.
Is any of these approaches suitable? Are there better strategies to handle
these kind of redirects? Does the exception really occur because of a
redirect?
Thank you all in advance!
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.
I'm trying to use the DefaultHttpClient in an Android app I've had for sometime. The server I've been authenticating through recently changed some of their stuff and so it broke my app. I've been trying to figure out why, and I think it's related to their having commas in a cookie value they send me; the sysadmin I'm working with was able to show me a value I should have had and I saw the value my app is getting by printing the cookies out to the log. What he sent me:
Cookie: LastMRH_Session=f9d0e6b4; MRHSession=2ab4fe11c9258eb12ebe60d6f9d0e6b4; F5_ST=1,1,1,1346792510,604800; ...
What I got in my logs was just F5_ST and 1 when I printed name and value.
Has anyone run into this before or have an idea of what to do?
well didnt fix the problem I was having, but I got the entire cookie to be used right by:
params.setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.NETSCAPE);
not sure the implications for the rest of my connections, but it seems to give me the whole cookie back now at least.
This is probably a stupid question; however, I am out of options and would appreciate help. I am trying to mock file downloads using Robolectric. I am referring to the class HttpTest.java, and the Robolectric blog post.
My problem: Maven is unable to find the Http class referred to in the HttpTest.java code. I could manually bring it over as a class in the same package, but I ended up getting a NullPtrException in one of the methods of class Http.
I have searched on the net for a solution. I have also verified the modules configuration in the pom file. I do have com.pivotlabs present there.
Has anyone gotten this working? Please let me know what I am missing here.
I got this working.
Http is a class internal to Robolectric's sample code. It can be used outside; but you might not want to do that if your code is gonna be built non-locally.
The clarification/mention in documentation that would have gone long way in helping me is: "Robolectric will give you the response you create using addPendingHttpResponse() for your HTTP request, no matter how you do the request. Robolectric will NOT do an actual HTTP request." Once I understood this, rest was easy.
I used Apache's DefaultHttpClient for my request, and verified received response against the already created response.
Hope this helps; I'd be happy to clarify further.
Thanks for the help I received here.
[I had asked this question on Robolectric's google group as well.]