使用异步任务将xml下载到String []

时间:2014-08-22 06:00:45

标签: java android android-asynctask

所以我试图让AsyncTask出去并获取三个不同的XML文件。问题是,我不知道如何将它们从AsyncTask中取出来

有GetXML类:

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

import android.os.AsyncTask;

public class GetXML extends AsyncTask<String,Integer,String[]> {
@Override
protected String[] doInBackground(String... params) {
    // TODO Auto-generated method stub

    String[] retreiver = new String[3];

    try {
        retreiver = getXMLs();
        return retreiver;
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    for(int i=0;i<retreiver.length;i++){
        retreiver[i]="<object_list><object><title>ERROR</title></object></object_list>";
    }
    return retreiver;
}


 private static String[] getXMLs() throws Exception {
        String[] urls = new String[3];

        urls[0] = "http://www.parkland.hostoi.com/XML_Files/indoor.xml";
        urls[1] = "http://www.parkland.hostoi.com/XML_Files/outdoor.xml";
        urls[2] = "http://www.parkland.hostoi.com/XML_Files/events.xml";

        BufferedReader reader = null;
        URLConnection uc = null;

        int i = 0;

        for(i=0;i<urls.length;i++){

            try {
                URL url = new URL(urls[i]);
                uc = url.openConnection();
                uc.connect();
                reader = new BufferedReader(new InputStreamReader(uc.getInputStream()));
                StringBuffer buffer = new StringBuffer();
                int read;
                char[] chars = new char[1024];
                while ((read = reader.read(chars)) != -1)
                    buffer.append(chars, 0, read);

                urls[i]= buffer.toString();
                } 
                finally {
                    if (reader != null)
                        reader.close();
                }
        }
        return urls;
 }

然后......下一步是什么?目前我在主要活动上有这一点,我知道这是导致问题的部分:

String[] xmls = new String[3];

    GetXML taskA = new GetXML();
    try {
        xmls = taskA.get();
    } catch (InterruptedException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (ExecutionException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

但所有这一切都会导致崩溃或超时等等......在LogCat中没有出现任何错误。

我只是不知道。这是我的第一个android项目,它正在杀了我。

3 个答案:

答案 0 :(得分:1)

您的任务未执行,因为您从未执行过,

需要致电

 taskA .execute();

参考下文:

主要活动:

GetXML taskA = new GetXML();
taskA.execute();

您的AsyncTask是:

public class GetXML extends AsyncTask<String,Integer,String[]> {
        @Override
        protected String[] doInBackground(String... params) {
            // TODO Auto-generated method stub

            String[] retreiver = new String[3];

            try {
                retreiver = getXMLs();
                return retreiver;
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            for(int i=0;i<retreiver.length;i++){
                retreiver[i]="<object_list><object><title>ERROR</title></object></object_list>";
            }
            return retreiver;
        }


        private String[] getXMLs() throws Exception {
            String[] urls = new String[3];

            urls[0] = "http://www.parkland.hostoi.com/XML_Files/indoor.xml";
            urls[1] = "http://www.parkland.hostoi.com/XML_Files/outdoor.xml";
            urls[2] = "http://www.parkland.hostoi.com/XML_Files/events.xml";

            BufferedReader reader = null;
            URLConnection uc = null;

            int i = 0;

            for(i=0;i<urls.length;i++){

                try {
                    URL url = new URL(urls[i]);
                    uc = url.openConnection();
                    uc.connect();
                    reader = new BufferedReader(new InputStreamReader(uc.getInputStream()));
                    StringBuffer buffer = new StringBuffer();
                    int read;
                    char[] chars = new char[1024];
                    while ((read = reader.read(chars)) != -1)
                        buffer.append(chars, 0, read);

                    urls[i]= buffer.toString();
                } 
                finally {
                    if (reader != null)
                        reader.close();
                }
            }
            return urls;
        }

        @Override
        protected void onPostExecute(String[] result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
            String[] xmls = result;
        }
    }

结果填充在 onPostExecute

要求清单中的权限:

 <uses-permission android:name="android.permission.INTERNET"/>

答案 1 :(得分:1)

你可以试试这个:

String[] xmls = new String[3];

    GetXML taskA = new GetXML();
    try {
        xmls = taskA.execute().get();
    } catch (InterruptedException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (ExecutionException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

答案 2 :(得分:0)

可以在docs中找到一个很好的例子。

您需要在任务上调用execute()方法,而不是get()

我还建议您至少在onPostExecute()中实施AsyncTask方法。