I'm trying to make an Http GET and send JSON as input:
https://host/X/Y?input={"foo":"bar","baz":"yak"}
according to these posts:
HttpGet with request body android
Apache's HttpGet (used by Android) does not support this kind of thing.
I've tried some of the alternatives specified in that post and they haven't worked.
First, can someone confirm Apache's HttpGet won't let you send JSON.
Next, can someone suggest any ways around this - alternate libraries, other classes, anything ….
put some json into a file and this example will post it
dont know why a GET would be used with much json info?
Figure out the 'entities' implementation along with POST and choose a better httpclient than you are using.
When submitting JSON, you might prefer using POST instead if GET. In case you do want to use GET, don't forget to encode your URL with URLEncoder.encode(json, "UTF-8"). This might be the cause of your problem.
Related
I am having the following API call:
http://rollout.gr/api/?query={%22search%22:%22places%22,%22page%22:1}
This API call is executed correctly in my browser. But when I use a DefaultHttpClient to execute this url in my Android application, I get a null response.
I suppose the problem is the JSON data in the HTTP url. Thus, I would like to ask which is the proper way to handle such url in an Android application?
Thanks a lot in advance!
The accolades aren't valid URL characters. The browser is userfriendly enough to automatically URL-encode them, but DefaultHttpClient isn't. The correct line to use from code is:
http://rollout.gr/api/?query=http://rollout.gr/api/?query=%7b%22search%22:%22places%22,%22page%22:1%7d
Note the encoding for the accolades (%7b, %7d).
Your problem may be the strictmode here.
I recommend to do http request in threads or asynctasks. strictmode doesnt let app do http reauest in uithread. maybe your console shows a warning and you get null from http response because of this.
This project may solve your problem:
http://loopj.com/android-async-http/
Not knowing your particular HTTP initialization code, I'm going to assume you didn't provide an explicit JSON accept header. A lot of REST endpoints require this.
httpget.setHeader("Accept", "application/json");
My question is pretty similar to that
except but I need only the header of the response in order to reduce battery consomation.
You need to make a HEAD request. In the http protocol this will return to you only the Header of the response.
http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.4
This can be done in android using HttpHead
you can use the HttpHead request
I'm working on an issue which is:
+ I send a HTTP POST request to a server and get the response.
+ I use a DefaultHTTPClient instance to send the request. And I use BasicNameValuePair to add the params for the server to process.
There is a param which is a xml-based string. The problem is that, when I send an "empty" string like this: " ", the server response "SUCCESSFUL". But when I send another value which is much longer. The server response "UNSUCCESSFUL" due to this parameter.
Please tell me that whether Android has a max length limitation on BasicNameValue instance or not? And how can I fix this issue.
Thanks in advance.
I expect the problem is with your web service, not with the BasicNameValue implementation on Android: you are probably just sending it a bad parameter. Do you get SUCCESSFUL when you send the exact same request parameters from another programming language?
I'd like to know how to do a HTTP GET request to a server from which I know I will get redirected at least a couple of times...
I'd like to get the response (body) of the "last" webpage. It should come up with a code in my specific example...
Or, as an alternative, I do know you can start the browser from within your Android app, is it possible to actually retrieve the body from that ?
Any help, tips, source whatever would be helpfull, cause till now I have found (and tried) like 4 methods and to me it seems there is a jungle of HttpGet, HttpClient, HttpResponse etc libraries ?
final HttpParams params = new BasicHttpParams();
HttpClientParams.setRedirecting(params, false);
You can see more on redirect support with HttpClient in other StackOverflow answers.
is it possible to actually retrieve
the body from that ?
No, that would be a security violation.
I have problems getting the HTML code of a website by using this example code.
http://svn.apache.org/repos/asf/httpcomponents/httpclient/branches/4.0.x/httpclient/src/examples/org/apache/http/examples/client/ClientPreemptiveBasicAuthentication.java
I have to use the above one, as I need preemptive authentication for my application in Android.
I thought the solution might be the httpget object, but I still get only errors :(
c.f. HttpGet httpget = new HttpGet("/index.html");
Does anybody have an idea how to get the content of the file, which is specified in the httpget object above using the example code of the link. It definitely called, but for now I can only get status code and so on ...
Thanks 4 help
When I tackled this last year, I gave up on HttpClient's native pre-emptive HTTP authentication and just rolled the header myself.
Alternative 1: Please read Http Basic Authentication with Android that proposes a solution based on the HttpClient 4 official docs. I've not tested it by myself, so I'd be happy to know if it really works.
Edit: I've just tried it and it works like a charm.
Alternative 2:
You can also add the "Authorization" HTTP header as proposed by #CommonsWare:
post.addHeader("Authorization", "Basic " + Base64.encode(username+":"+password));
In this case you need a Base64 encoder to encode the string containing the username and the password. You can find a lot of implementations in the Internet.
For me the example above didn't work on Android. I had to do the following:
post.addHeader("Authorization", "Basic " + Base64.encodeToString((username+":"+password).getBytes(),Base64.NO_WRAP));
Thanks janex.
I had to do the same on Android.
post.addHeader("Authorization", "Basic " + Base64.encodeToString((username+":"+password).getBytes(),Base64.NO_WRAP));
cheers