tags:

views:

358

answers:

3

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  A: 

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
@iTom...I already read this article....but then how do i pass "Username" and "Password" value.....btw thanx
PM - Paresh Mayani
iTom
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
@iTom still not getting
PM - Paresh Mayani
Hey Paresh please explain what's happening are you getting an error message? or is nothing being returned?
iTom
+1  A: 

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
@Ryan i think this helps me...let me try...give u reply if running..thanx for the support
PM - Paresh Mayani
@Ryan Base64Converter is not supported in Android..showing Red line under(error) the Base64Converter
PM - Paresh Mayani
@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
+3  A: 
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());
Gianni
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
@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
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
just add this line to pass "Username" and "Password" : httpclient.getCredentialsProvider().setCredentials( new AuthScope(null, -1), new UsernamePasswordCredentials("username", "password"));
PM - Paresh Mayani
After adding the above line...this is working
PM - Paresh Mayani
@Paresh Mayani: Please tell me the `Uri` value?
Praveen Chandrasekaran