Android - Read an XML file with HTTP GET

2019-01-17 08:29发布

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.

标签: android xml get
3条回答
Viruses.
2楼-- · 2019-01-17 09:11

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

查看更多
混吃等死
3楼-- · 2019-01-17 09:21

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.

查看更多
不美不萌又怎样
4楼-- · 2019-01-17 09:26
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());
查看更多
登录 后发表回答