xmlpullparser(错误的文件描述符)

时间:2018-07-01 19:29:25

标签: android xmlpullparser

我对android还是很陌生,我正尝试使用XmlPullParser解析一些xml,但我一直收到此错误(它发生的时间是相当不一致的,有时它在错误发生之前会变得更远)

E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
              Process: com.example.android.unescotracker, PID: 21828
              java.lang.RuntimeException: java.net.SocketException: recvfrom failed: EBADF (Bad file descriptor)

我在按下按钮时调用此代码,然后以AsyncTask的身份运行我的代码:

public class DatabaseManager{
private static String LOG_TAG = DatabaseManager.class.getName();

private static String XMLSTREAM = "http://whc.unesco.org/en/list/xml/";

public static void runDatabaseManager(Boolean isConnected){
    if (isConnected) {
        AsyncTask.execute(new Runnable() {
            @Override
            public void run() {
                URL url = XmlUtils.createUrl(XMLSTREAM);
                InputStream stream = null;

                try {
                    stream = XmlUtils.makeHttpRequest(url);
                } catch (IOException e){
                    Log.e(LOG_TAG, "IOException Error: Trying to get URL stream", e);
                }

                updateDatabase(stream);
            }
        });
    }

}

private static void updateDatabase(InputStream stream){
    try {
        XmlUtils.getSitesFromXml(stream);
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        if (stream != null) {
            try {
                stream.close();
            } catch (IOException e) {
                Log.e(LOG_TAG, "IOException Error: Trying to close xml stream", e);
            }
        }
    }
}
}

实际xml解析器类

public class XmlUtils {
private static String LOG_TAG = XmlUtils.class.getName();

public static void getSitesFromXml (InputStream stream) throws XmlPullParserException, IOException{
    XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
    XmlPullParser parser = factory.newPullParser();

    parser.setInput(stream, null);

    int event = parser.getEventType();
    Log.d("XML", "Starting to read doc");
    while (event != XmlPullParser.END_DOCUMENT) {
        if (parser.getEventType() == XmlPullParser.START_TAG) {
            String name = parser.getName();

            if (name.equals("row")) {
                readSite(parser);
            }
        }
        event = parser.next();
    }
    Log.d("XML", "Finished Reading");
}

private static void readSite(XmlPullParser parser) throws XmlPullParserException, IOException {
    String siteName = "";
    String tag = "";
    int event;
    Log.d("XML", "reading new row");

    parser.require(XmlPullParser.START_TAG, null, "row");
    while (parser.next() != XmlPullParser.END_DOCUMENT) {
        event = parser.getEventType();
        if (event == XmlPullParser.END_TAG) {
            if (parser.getName().equals("row")) {
                break;
            }
            continue;
        } else if (event != XmlPullParser.START_TAG) {
            continue;
        }

        tag = parser.getName();
        Log.d("XML", tag);

        switch (tag) {
            case "site":
                siteName = readText(parser);
                Log.d("XML", siteName);
                break;
        }
        Log.d("XML", Integer.toString(parser.next()));
    }
}

// For the tags title and summary, extracts their text values.
private static String readText(XmlPullParser parser) throws IOException, XmlPullParserException {
    String result = "";
    if (parser.next() == XmlPullParser.TEXT) {
        result = parser.getText();
        parser.nextTag();
    }
    return result;
}

// Make HTTP GET request for JSON data
public static InputStream makeHttpRequest(URL url) throws IOException {
    HttpURLConnection urlConnection = null;
    InputStream inputStream = null;

    // Catch null url and return
    if (url==null){
        return inputStream;
    }

    try{
        urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.setReadTimeout(20000); // Milliseconds
        urlConnection.setConnectTimeout(25000); // Milliseconds
        urlConnection.setRequestMethod("GET");
        urlConnection.connect();
        int response = urlConnection.getResponseCode();

        if (response == HttpURLConnection.HTTP_OK){
            inputStream = urlConnection.getInputStream();
        }else {
            Log.e(LOG_TAG, "HTTP Request Failed, Response Code: " + urlConnection.getResponseCode());
        }
    } catch (IOException e){
        Log.e(LOG_TAG, "IOException Error: Problem getting JSON data", e);
    } finally {
        if (urlConnection != null){
            urlConnection.disconnect();
        }
    }

    return inputStream;
}

// Return URL object from String URL
public static URL createUrl(String stringUrl){
    URL url = null;

    try {
        url = new URL(stringUrl);
    } catch (MalformedURLException e){
        Log.e(LOG_TAG, "Malformed URL Error: Trying to create URL", e);
    }

    return url;
}

}

使我感到困惑的最大事情是,它在解析xml时失败之处不一致。只是移动日志会使其在不同点出错,这是否与asynctask或内存问题有关?

0 个答案:

没有答案