我想保存android中xml文件中某个链接的数据。我怎样才能做到这一点?任何人都可以帮助我,我不知道。
提前致谢。
来自链接的数据如下所示:
<elements>
<element>
<def>confirm</def>
<text>Ok</text>
</element>
<element>
<def>email</def>
<text>Email</text>
</element>
<element>
<def>firstname</def>
<text>Prénom</text>
</element>
</element>
答案 0 :(得分:3)
您没有问一个具体的问题。您不能指望其他人为您编写代码。对不起,不要试着做个意思。无论如何,为了让你开始,我假设你试图从http链接下载文件?你可能想看一下httpClient。这是使用它的一个很好的例子:
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
public class TestHttpGet {
public void executeHttpGet() throws Exception {
BufferedReader in = null;
try {
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet();
request.setURI(new URI("http://www.somewhere.com/somefile.xml"));
HttpResponse response = client.execute(request);
in = new BufferedReader
(new InputStreamReader(response.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String line = "";
String NL = System.getProperty("line.separator");
while ((line = in.readLine()) != null) {
sb.append(line + NL);
}
in.close();
String page = sb.toString();
System.out.println(page);
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}