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.

1
  • 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. Commented Aug 4, 2010 at 21:00

3 Answers 3

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());
Sign up to request clarification or add additional context in comments.

10 Comments

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.
just add this line to pass "Username" and "Password" : httpclient.getCredentialsProvider().setCredentials( new AuthScope(null, -1), new UsernamePasswordCredentials("username", "password"));
Should be? => if (status.getStatusCode() != 200) {
For future reference: that uri object can be constructed as follows: new HttpGet("http://example.com")
StatusLine.getStatusCode() returns int, shouldn't be compared to String.
|
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

4 Comments

@iTom...I already read this article....but then how do i pass "Username" and "Password" value.....btw thanx
With a GET request the form values are usually appended to the URI request instead of posted i.e. example.com/… Hope that helps, iTom
Oh also if your after a function that builds the URI for a GET request then this article may also help.. anddev.org/doing_http_post_with_android-t492.html
Hey Paresh please explain what's happening are you getting an error message? or is nothing being returned?
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.

3 Comments

i think this helps me...let me try...give u reply if running..thanx for the support
Base64Converter is not supported in Android..showing Red line under(error) the Base64Converter
@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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.