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.
Related
Given a WebResourceRequest which is a post request and has body how to i make an http call and get the response?
I know this sounds pretty basic butI'm having a problem getting the answer
Have you tried looking at Volley. It is quite powerful and easy to use and gets all your needs handled.
After implementing it if you are still getting nothing try PostMan with the same parameters as you are using to make the http call. If PostMan fails to show data then parameters are wrong.
Volley - https://developer.android.com/training/volley/
PostMan - https://www.getpostman.com/
I can't really understand this concept . My knowledge on webservices and oop is limited. Lets say that I set up a string request and pass a very simple database connection php script url to it. What would the volley response and error contain?
Volley follows standard [HTTP response codes][1].
[1]: https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html .
When the server responds with a HTTP OK - 200 Code it goes through the onResponse Callback . For all error code it would go through the onError Callback.
I am using RoboSpice to access some rest services I developed using resteasy. I'm returning a JSON object when everything is ok and in the case of an exception I'm returning an http error code and a JSON object describing the nature of the exception. I'm able to get the JSON object when everything is working fine but I'm only able to get the exception and the http error code, but not the JSON I just returned in case of an exception. I have tried doing the same in iOS and I can get everything in every case, anyone knows how to do this using RoboSpice?.
By the way I'm using Jackson and Spring.
Thanks!
Take a look at the method explained in this thread for intercepting the http status for a failing request.
Please note that the HttpClientErrorException has a getResponseBodyAsString() method that should help you reading the JSON response.
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");
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.