0

I'm writing an Android app that should get data from a certain web application. That web app is based on Servlets and JSP, and it's not mine; it's a public library's service. What is the most elegant way of getting this data?

I tried writing my own Servlet to handle requests and responses, but I can't get it to work. Servlet forwarding cannot be done, due to different contexts, and redirection doesn't work either, since it's a POST method... I mean, sure, I can write my own form that access the library's servlet easily enough, but the result is a jsp page.. Can I turn that page into a string or something? Somehow I don't think I can.. I'm stuck.

Can I do this in some other way? With php or whatever? Or maybe get that jsp page on my web server, and then somehow extract data from it (with jQuery maybe?) and send it to Android? I really don't want to display that jsp page in a browser to my users, I would like to take that data and create my own objects with it..

Ivan
  • 867
  • 1
  • 8
  • 15

1 Answers1

0

Just send a HTTP request programmatically. You can use Android's builtin HttpClient API for this. Or, a bit more low level, the Java's java.net.URLConnection (see also Using java.net.URLConnection to fire and handle HTTP requests). Both are capable of sending GET/POST requests and retrieving the response back as an InputStream, byte[] or String.

At most simplest, you can perform a GET as follows:

InputStream responseBody = new URL("http://example.com").openStream();
// ...

A POST is easier to be performed with HttpClient:

List<NameValuePair> params = new ArrayList<NameValuePair>(2);
params.add(new BasicNameValuePair("name1", "value1"));
params.add(new BasicNameValuePair("name2", "value2"));

HttpPost post = new HttpPost("http://example.com");
post.setEntity(new UrlEncodedFormEntity(params));

HttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(post);
InputStream responseBody = response.getEntity().getContent();
// ...

If you need to parse the response as HTML (I'd however wonder if that "public library service" (is it really public?) doesn't really offer XML or JSON services which are way much easier to parse), Jsoup may be a life saver as to traversing and manipulating HTML the jQuery way. It also supports sending POST requests by the way, only not as fine grained as with HttpClient.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thanks a lot!! I'll try it out! Jsoup sounds like something I could really use. IIRC, i tried HtppClient and it didn't work no matter what I tried, but that was a long time ago, I'll def try it again once I find enough time.. – Ivan Mar 24 '12 at 13:06
  • Oh, and it is basically this that I should get data from: bisis.bgb.rs/index.jsp?locale=en As you can see, it's basically a search engine for this library, and no, it doesn't offer XML or JSON, as far as I can see.. It just seems like a regular servlet application, without anything fancy and "modern" like providing data for other apps :) – Ivan Mar 24 '12 at 13:13