Android - 使用 HTTP GET 读取 XML 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3395154/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 10:00:52  来源:igfitidea点击:

Android - Read an XML file with HTTP GET

androidxmlget

提问by Paresh Mayani

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.

我需要探索我的项目在 Android 上使用 Web 服务。我知道没有用于 XML - RPC Web 服务的官方库。

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

但是有 REST XML,我需要测试它。

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

我想从 Android 使用 HTTP GET 在我的网页(我必须传递用户名和密码的地方)上读取 XML。

OR

或者

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

假设,我按照这个链接,那么我在哪里传递用户名和密码?

Can anybody help me on this.

任何人都可以帮助我。

回答by Gianni

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());

回答by iTom

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

此链接帮助我开始了解如何使用 SAX 解析器进行 HTTP GET XML 和解析。

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

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

Hope this helps,

希望这可以帮助,

iTom

汤姆

回答by Ryan

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

HTTP Basic Auth 的几行代码,如果你是这个意思的话。

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.

其中“Base64Converter”是一个实用程序类,用于将字符串转换为其 Base64 编译形式。在 parsingxml.java 第 36 行中的 openConnection() 调用之前执行此操作。