Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 从HTTP读取XML_Android_Xml - Fatal编程技术网

Android 从HTTP读取XML

Android 从HTTP读取XML,android,xml,Android,Xml,我正在尝试从internet读取xml文件 这是我正在使用的代码: HttpClient client = new DefaultHttpClient(); HttpPost httppost = new HttpPost("http://www.mypage.com/version.xml"); HttpResponse resp = client.execute(httppost); 我得到了这个前男友: android.os.NetworkOnMainThreadException 我正

我正在尝试从internet读取xml文件

这是我正在使用的代码:

HttpClient client = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.mypage.com/version.xml");
HttpResponse resp = client.execute(httppost);
我得到了这个前男友:

android.os.NetworkOnMainThreadException

我正试图用一个异步方法来实现这一点,方法如下所示。 这是我尝试的代码:

public class Download extends AsyncTask<String, Void, Document> {

@SuppressWarnings("unused")
private Exception exception;
public Document document;

public Download(String url)
{
    this.execute(url);
}

@Override
protected Document doInBackground(String... url) {
    try {
        HttpUriRequest request = new HttpGet(url.toString());
        HttpClient httpClient = new DefaultHttpClient();
        HttpResponse response = httpClient.execute(request);

        StatusLine statusLine = response.getStatusLine();
        int statusCode = statusLine.getStatusCode();
        if (statusCode == 200) {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            document = builder.parse(response.getEntity().getContent());
            return document;
        } else {
            throw new IOException("Download failed, HTTP response code "
                    + statusCode + " - " + statusLine.getReasonPhrase());
        }            

    } catch (Exception e) {
        exception = e;
        return null;
    }
}

protected void onPostExecute(Document doc)
{
    // TODO: check this.exception 
    // TODO: do something with the feed
    document = doc;
}

}
公共类下载扩展异步任务{
@抑制警告(“未使用”)
私人例外;
公共文件;
公开下载(字符串url)
{
执行(url);
}
@凌驾
受保护文档doInBackground(字符串…url){
试一试{
HttpUriRequest request=newhttpget(url.toString());
HttpClient HttpClient=新的DefaultHttpClient();
HttpResponse response=httpClient.execute(请求);
StatusLine StatusLine=response.getStatusLine();
int statusCode=statusLine.getStatusCode();
如果(状态代码==200){
DocumentBuilderFactory工厂=DocumentBuilderFactory.newInstance();
DocumentBuilder=factory.newDocumentBuilder();
document=builder.parse(response.getEntity().getContent());
归还文件;
}否则{
抛出新IOException(“下载失败,HTTP响应代码”
+statusCode+“-”+statusLine.GetReasonPhase());
}            
}捕获(例外e){
例外=e;
返回null;
}
}
后期执行时受保护的无效(文档文档)
{
//TODO:检查此项。异常
//TODO:对提要做些什么
文件=文件;
}
}

但是文档为空。

尝试在
异步任务
中包含Web调用逻辑


仅供参考,您可以在AsyncTask的
doInBackground()方法中执行后台处理。

您是否阅读了异常?使用后台线程。最好避免在主线程上进行网络操作。检查此线程:或使用
AsyncTaskLoader