9

I need to explore for my project use of web services on Android. I know that there is no official library for XML - RPC web service.

But there is for REST XML and i need to test it.

I would like to read XML on my web page (Where i have to pass username and Password) from Android with HTTP GET.

OR

Suppose, i follow This link, then where do i pass username and password?

Can anybody help me on this.

Janusz
  • 187,060
  • 113
  • 301
  • 369
Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295
  • How are the username and password required? If it's HTTP Basic Auth then the HttpClient library supports that and you can add to Gianni's code. – Nick Aug 04 '10 at 21:00

3 Answers3

12
HttpGet uri = new HttpGet("http://example.com");    

DefaultHttpClient client = new DefaultHttpClient();
HttpResponse resp = client.execute(uri);

StatusLine status = resp.getStatusLine();
if (status.getStatusCode() != 200) {
    Log.d(tag, "HTTP error, invalid server status code: " + resp.getStatusLine());  
}

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(resp.getEntity().getContent());
Rajat Singhal
  • 11,234
  • 5
  • 38
  • 56
Gianni
  • 4,300
  • 18
  • 24
  • Don't forget to close up your HTTP request! resp.getEntity().consumeContent() - only need to do this in the event that the XML parsing doesn't fully read the file though, so in a finally block in some form. – Nick Aug 04 '10 at 20:57
  • @Nick Yeah! Actually, that is only a snippet of a much bigger code I have running. I just took the relevant parts to get started. – Gianni Aug 04 '10 at 21:37
  • Cool beans - it's just a common error in HTTP requesting so I thought I'd mention it for everyone's benefit. Personally I like using the execute() methods that take a ResponseHandler, since they guarantee everything is released after the handler is called. – Nick Aug 04 '10 at 22:07
  • 2
    just add this line to pass "Username" and "Password" : httpclient.getCredentialsProvider().setCredentials( new AuthScope(null, -1), new UsernamePasswordCredentials("username", "password")); – Paresh Mayani Aug 06 '10 at 12:00
  • @Paresh Mayani: Please tell me the `Uri` value? – Praveen Sep 14 '10 at 04:47
  • 3
    Should be? => if (status.getStatusCode() != 200) { – Łukasz Siwiński Apr 16 '12 at 21:23
  • 2
    For future reference: that `uri` object can be constructed as follows: `new HttpGet("http://example.com")` – Pieter Jun 03 '12 at 15:44
  • @Pieter Updated example. Thanks! – Gianni Jun 04 '12 at 15:33
  • 2
    [`StatusLine.getStatusCode()`](http://developer.android.com/reference/org/apache/http/StatusLine.html#getStatusCode%28%29) returns int, shouldn't be compared to `String`. – Czechnology Feb 10 '13 at 00:32
  • Android says: Deprecated =( @Gianni – Florida May 31 '15 at 14:46
2

This link helped me to get started understanding how to HTTP GET XML and Parse using the SAX Parser.

http://www.anddev.org/parsing_xml_from_the_net_-_using_the_saxparser-t353.html

Hope this helps,

iTom

iTom
  • 61
  • 1
  • 6
  • @iTom...I already read this article....but then how do i pass "Username" and "Password" value.....btw thanx – Paresh Mayani Aug 03 '10 at 09:35
  • 1
    With a GET request the form values are usually appended to the URI request instead of posted i.e. http://www.example.com/webservice.php?username=iTom&password=MyPassword Hope that helps, iTom – iTom Aug 03 '10 at 09:46
  • Oh also if your after a function that builds the URI for a GET request then this article may also help.. http://www.anddev.org/doing_http_post_with_android-t492.html – iTom Aug 03 '10 at 09:56
  • Hey Paresh please explain what's happening are you getting an error message? or is nothing being returned? – iTom Aug 03 '10 at 11:11
1

A few lines of code for HTTP Basic Auth, if you mean this.

String auth = Base64Converter.encode(String.format("%s:%s", user, pass));
URL u = new URL(url);
conn = (HttpsURLConnection) u.openConnection();
conn.addRequestProperty("Authorization", "Basic " + auth);

Where "Base64Converter" is a utility class converts a string to its Base64 compiled form. Do this before the openConnection() call in parsingxml.java, line 36.

Ryan
  • 870
  • 1
  • 7
  • 18
  • i think this helps me...let me try...give u reply if running..thanx for the support – Paresh Mayani Aug 04 '10 at 05:07
  • Base64Converter is not supported in Android..showing Red line under(error) the Base64Converter – Paresh Mayani Aug 04 '10 at 09:03
  • @Paresh yes Base64Converter is a public domain utility class that I found somewhere else. A Google search might get you the right place. Android meant to have provided an Base64 encoder / decoder class but is actually unavailable to app developers. – Ryan Aug 09 '10 at 12:23