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");
Related
So basically right now my app is configured to use https because in the "release" it will use a self signed certificate and obviously also use Https.
My current testsystem (few more features) doesn't use https but http instead. I thought it would be kinda nice to have some type of method to check whether the given URL is Http or Https and depending on the result create the right URLConnection.
My current problem is that I don't know what the method should exactly look like. I thought about using if-statements in the methods which connect to my server but there might be a better solution.
Help would be appreciated.
How about this:
URLUtil.isHttpUrl(String url)
URLUtil.isHttpsUrl(String url)
See also: http://developer.android.com/reference/android/webkit/URLUtil.html
If you don't want to do a manual check you can use a 3rd party library like this one: http://square.github.io/okhttp/ which allows you a simple:
Request request = new Request.Builder().url(url).build();
Response response = new OkHttpClient().newCall(request).execute();
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.
I made a test project just testing http request and response behavior, There is a simple http class extending Asynctask to make request and get response. I am performing some string manipulation on http response, there is a problem my code which is using http response is executing before getting the response from class, which results null pointer exception. So to resolve this i made a thread and all code dependent on http response put in that thread. Now every thing is working fine.But i want to know is there any other technique to handle this? or my approach is good? Please suggest.
i want to know is there any other technique to handle this?
Use AsyncTask for Android Http 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 want to know whether it is possible to create an Android application to communicate with a session bean and invoke a method. if so can anybody explain how? or else can i invoke that method in the EJB with a JSP/servelet and call the JSP/Servelet with Android clients.. examples are highly appreciate
Thanks !!!
It is possible to communicate with Servelet in Android using HttpClient, HttpPost and HttpGet classes in android..
It is in theory relatively simple. Servlets can be configured by web.xml or #WebServlet annotation to get executed on a certain request URL. On a HTTP GET request the doGet() method will be executed. On a HTTP POST request, the doPost() method will be executed. The business logic which the servlet executes can depend/rely on the presence of HTTP request parameters and/or the request URI pathinfo.
All you need to do is to fire a HTTP request with the right URL and/or the right request parameters and/or the right pathinfo to let the servlet execute the desired job.
The basic Java API offers the java.net.URL and java.net.URLConnection for this. A simple HTTP GET request can be executed as follows:
InputStream response = new URL("http://example.com/servleturl?foo=bar&bar=foo").openStream();
// ...
Firing HTTP POST requests is a bit more complex. It can be done with java.net.URLConnection as outlined in this mini-tutorial, but Android also ships with Apache HttpComponents Client which allows firing and handling HTTP requests with less lines of code and more self-explaining code.
On http://androidsnippets.org you can find a lot of examples with HttpClient.