简体   繁体   English

Android XML阅读器出错

[英]Error with Android XML reader

I have an XML parser that works correctly, but if the User is connected to a network that are not browsing the app breaks. 我有一个XML解析器,可以正常工作,但是如果用户连接到未浏览应用程序的网络,则会中断。 To put a function to detect whether or not the User is connected, but the problem mentioned above, the User would be connected but no browsing. 为了放置检测用户是否已连接但上述问题的功能,用户将被连接但没有浏览。

My code follows in full: 我的代码完全如下:

public void chamaParser(){
    Boolean conected = Conectado(this);
    if(conected == true){
        new DoInBackground().execute();
    } else {
        mPullRefreshListViewRSS.onRefreshComplete();
        new AlertDialog.Builder(PullToRefreshListActivityRSS.this)
                .setTitle("Alerta!")
                .setMessage("Não foi possível estabelecer uma conexão com a Internet, verifique sua rede ou tente novamente mais tarde.")
                .setNeutralButton("OK", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {}
        }).show();
    }
}

public void do_update() {
    RssParser.parse();
}

public static boolean Conectado(Context context) {
    try {
        ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
        String LogSync = null;
        String LogToUserTitle = null;
        if (cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnected()) {
            LogSync += "\nConectado a Internet 3G ";
            LogToUserTitle += "Conectado a Internet 3G ";
            return true;
        } else if(cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnected()) {
            LogSync += "\nConectado a Internet WIFI ";
            LogToUserTitle += "Conectado a Internet WIFI ";
            return true;
        } else {
            LogSync += "\nNão possui conexão com a internet ";
            LogToUserTitle += "Não possui conexão com a internet ";
            return false;
        }
    } catch (Exception e) {
        return false;
    }
}

private class DoInBackground extends AsyncTask<Void, Void, Void> implements DialogInterface.OnCancelListener {
    private ProgressDialog dialog;

    protected void onPreExecute() {
        //dialog = ProgressDialog.show(PullToRefreshListActivityRSS.this, "", "Carregando...", true);
        dialog = ProgressDialog.show(PullToRefreshListActivityRSS.this, "", "Carregando...", true, true, new DialogInterface.OnCancelListener(){
                @Override
                public void onCancel(DialogInterface dialog) {
                    mPullRefreshListViewRSS.onRefreshComplete();
                    DoInBackground.this.cancel(true);
                    // finish();
                }
        });
    }

    protected Void doInBackground(Void... unused) { 
        PodcastAdapterRSS.topBarRSS=true;

        do_update(); 
        return null; 
    }

Parser XML: 解析器XML:

public class RssParser {

    public static void parse() {
        URL url;
        try {
            url = new URL("http://www.xxxx.com.br/feed/");
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();

            if(conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
                DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                DocumentBuilder db = dbf.newDocumentBuilder();
                Document doc;
                doc = db.parse(url.openStream());
                doc.getDocumentElement().normalize();
                NodeList itemLst = doc.getElementsByTagName("item");
                arrays.Title = new String[itemLst.getLength()];
                arrays.Link = new String[itemLst.getLength()];
                arrays.Description = new String[itemLst.getLength()];
                // arrays.PodcastMedia = new String[itemLst.getLength()];

                for(int i=0; i < itemLst.getLength(); i++){

                    Node item = itemLst.item(i);
                    if(item.getNodeType() == Node.ELEMENT_NODE){
                        Element ielem = (Element)item;
                        NodeList title = ielem.getElementsByTagName("title");
                        NodeList link = ielem.getElementsByTagName("link");
                        //NodeList description = ielem.getElementsByTagName("description"); 
                        NodeList content = ielem.getElementsByTagName("description");
                        //   NodeList media = ielem.getElementsByTagName("media:content");

                        //    String mediaurl = media.item(0).getAttributes().getNamedItem("url").getNodeValue();

                          arrays.Title[i] = title.item(0).getChildNodes().item(0).getNodeValue();
                          arrays.Link[i] = link.item(0).getChildNodes().item(0).getNodeValue();
                          arrays.Description[i] = content.item(0).getChildNodes().item(0).getNodeValue();

                    }
                }
            }

        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (DOMException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ParserConfigurationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SAXException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

Log Cat: 日志猫:

07-30 15:20:48.319: W/System.err(10997): java.net.UnknownHostException: www.xxxx.com.br 07-30 15:20:48.319: W/System.err(10997): at java.net.InetAddress.lookupHostByName(InetAddress.java:508) 07-30 15:20:48.319: W/System.err(10997): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:296) 07-30 15:20:48.319: W/System.err(10997): at java.net.InetAddress.getAllByName(InetAddress.java:258) 07-30 15:20:48.319: W/System.err(10997): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.(HttpConnection.java:69) 07-30 15:20:48.319: W/System.err(10997): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.(HttpConnection.java:48) 07-30 15:20:48.319: W/System.err(10997): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection$Address.connect(HttpConnection.java:322) 07-30 15:20:48.319: W/System.err(10997): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnectionPool.get(HttpConnectionPool.java:89) 07-30 15:20:48.319: W 07-30 15:20:48.319:W / System.err(10997):java.net.UnknownHostException:www.xxxx.com.br 07-30 15:20:48.319:W / System.err(10997):在java.net.InetAddress.lookupHostByName(InetAddress.java:508)07-30 15:20:48.319:W / System.err(10997):位于java.net.InetAddress.getAllByNameImpl(InetAddress.java:296)07-30 15:20:48.319:W / System.err(10997):在java.net.InetAddress.getAllByName(InetAddress.java:258)07-30 15:20:48.319:W / System.err(10997):在组织.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection。(HttpConnection.java:69)07-30 15:20:48.319:W / System.err(10997):位于org.apache.harmony .luni.internal.net.www.protocol.http.HttpConnection。(HttpConnection.java:48)07-30 15:20:48.319:W / System.err(10997):位于org.apache.harmony.luni.internal .net.www.protocol.http.HttpConnection $ Address.connect(HttpConnection.java:322)07-30 15:20:48.319:W / System.err(10997):位于org.apache.harmony.luni.internal。 net.www.protocol.http.HttpConnectionPool.get(HttpConnectionPool.java:89)07-30 15:20:48.319:W /System.err(10997): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getHttpConnection(HttpURLConnectionImpl.java:285) 07-30 15:20:48.319: W/System.err(10997): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.makeConnection(HttpURLConnectionImpl.java:267) 07-30 15:20:48.319: W/System.err(10997): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.retrieveResponse(HttpURLConnectionImpl.java:1018) 07-30 15:20:48.319: W/System.err(10997): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:726) 07-30 15:20:48.329: W/System.err(10997): at tabbar.rss.RssParser.parse(RssParser.java:30) 07-30 15:20:48.329: W/System.err(10997): at tabbar.rss.PullToRefreshListActivityRSS.do_update(PullToRefreshListActivityRSS.java:91) 07-30 15:20:48.329: W/System.err(10997): at tabbar.rss.PullToRefreshListActivityRSS$DoInBackground.doInBackground(PullTo /System.err(10997):位于org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getHttpConnection(HttpURLConnectionImpl.java:285)07-30 15:20:48.319:W / System。 err(10997):位于org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.makeConnection(HttpURLConnectionImpl.java:267)07-30 15:20:48.319:W / System.err(10997 ):在org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.retrieveResponse(HttpURLConnectionImpl.java:1018)07-30 15:20:48.319:W / System.err(10997):at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:726)07-30 15:20:48.329:W / System.err(10997):位于tabbar.rss .RssParser.parse(RssParser.java:30)07-30 15:20:48.329:W / System.err(10997):位于tabbar.rss.PullToRefreshListActivityRSS.do_update(PullToRefreshListActivityRSS.java:91)07-30 15:20 :48.329:W / System.err(10997):位于tabbar.rss.PullToRefreshListActivityRSS $ DoInBackground.doInBackground(PullTo RefreshListActivityRSS.java:205) 07-30 15:20:48.329: W/System.err(10997): at tabbar.rss.PullToRefreshListActivityRSS$DoInBackground.doInBackground(PullToRefreshListActivityRSS.java:1) 07-30 15:20:48.329: W/System.err(10997): at android.os.AsyncTask$2.call(AsyncTask.java:185) 07-30 15:20:48.329: W/System.err(10997): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306) 07-30 15:20:48.329: W/System.err(10997): at java.util.concurrent.FutureTask.run(FutureTask.java:138) 07-30 15:20:48.329: W/System.err(10997): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088) 07-30 15:20:48.329: W/System.err(10997): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581) 07-30 15:20:48.329: W/System.err(10997): at java.lang.Thread.run(Thread.java:1019) 07-30 15:20:48.329: W/dalvikvm(10997): threadid=1: thread exiting with uncaught exception (group=0x401fc560) 07-30 15:20:48.339: E/AndroidRuntime(10997): FATAL EXCEPTION: main 07-30 RefreshListActivityRSS.java:205)07-30 15:20:48.329:W / System.err(10997):位于tabbar.rss.PullToRefreshListActivityRSS $ DoInBackground.doInBackground(PullToRefreshListActivityRSS.java:1)07-30 15:20:48.329: W / System.err(10997):在android.os.AsyncTask $ 2.call(AsyncTask.java:185)07-30 15:20:48.329:W / System.err(10997):在java.util.concurrent。 FutureTask $ Sync.innerRun(FutureTask.java:306)07-30 15:20:48.329:W / System.err(10997):at java.util.concurrent.FutureTask.run(FutureTask.java:138)07-30 15:20:48.329:W / System.err(10997):在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)07-30 15:20:48.329:W / System.err(10997):在java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:581)07-30 15:20:48.329:W / System.err(10997):在java.lang.Thread.run(Thread.java: 1019)07-30 15:20:48.329:W / dalvikvm(10997):threadid = 1:线程以未捕获的异常退出(group = 0x401fc560)07-30 15:20:48.339:E / AndroidRuntime(10997):致命异常:主要07-30 15:20:48.339: E/AndroidRuntime(10997): java.lang.NullPointerException 07-30 15:20:48.339: E/AndroidRuntime(10997): at tabbar.rss.PullToRefreshListActivityRSS.populate_listview(PullToRefreshListActivityRSS.java:101) 07-30 15:20:48.339: E/AndroidRuntime(10997): at tabbar.rss.PullToRefreshListActivityRSS$DoInBackground.onPostExecute(PullToRefreshListActivityRSS.java:214) 07-30 15:20:48.339: E/AndroidRuntime(10997): at tabbar.rss.PullToRefreshListActivityRSS$DoInBackground.onPostExecute(PullToRefreshListActivityRSS.java:1) 07-30 15:20:48.339: E/AndroidRuntime(10997): at android.os.AsyncTask.finish(AsyncTask.java:417) 07-30 15:20:48.339: E/AndroidRuntime(10997): at android.os.AsyncTask.access$300(AsyncTask.java:127) 07-30 15:20:48.339: E/AndroidRuntime(10997): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429) 07-30 15:20:48.339: E/AndroidRuntime(10997): at android.os.Handler.dispatchMessage(Handler.java:99) 07-30 15:20:48.339: E/AndroidRuntime(10997): at android.os. 15:20:48.339:E / AndroidRuntime(10997):java.lang.NullPointerException 07-30 15:20:48.339:E / AndroidRuntime(10997):位于tabbar.rss.PullToRefreshListActivityRSS.populate_listview(PullToRefreshListActivityRSS.java:101)07 -30 15:20:48.339:E / AndroidRuntime(10997):位于tabbar.rss.PullToRefreshListActivityRSS $ DoInBackground.onPostExecute(PullToRefreshListActivityRSS.java:214)07-30 15:20:48.339:E / AndroidRuntime(10997):位于tabbar .rss.PullToRefreshListActivityRSS $ DoInBackground.onPostExecute(PullToRefreshListActivityRSS.java:1)07-30 15:20:48.339:E / AndroidRuntime(10997):at android.os.AsyncTask.finish(AsyncTask.java:417)07-30 15 :20:48.339:E / AndroidRuntime(10997):在android.os.AsyncTask.access $ 300(AsyncTask.java:127)07-30 15:20:48.339:E / AndroidRuntime(10997):在android.os.AsyncTask $ InternalHandler.handleMessage(AsyncTask.java:429)07-30 15:20:48.339:E / AndroidRuntime(10997):at android.os.Handler.dispatchMessage(Handler.java:99)07-30 15:20:48.339 :E / AndroidRuntime(10997):位于android.os。 Looper.loop(Looper.java:130) 07-30 15:20:48.339: E/AndroidRuntime(10997): at android.app.ActivityThread.main(ActivityThread.java:3689) 07-30 15:20:48.339: E/AndroidRuntime(10997): at java.lang.reflect.Method.invokeNative(Native Method) 07-30 15:20:48.339: E/AndroidRuntime(10997): at java.lang.reflect.Method.invoke(Method.java:507) 07-30 15:20:48.339: E/AndroidRuntime(10997): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:875) 07-30 15:20:48.339: E/AndroidRuntime(10997): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:633) 07-30 15:20:48.339: E/AndroidRuntime(10997): at dalvik.system.NativeStart.main(Native Method) Looper.loop(Looper.java:130)07-30 15:20:48.339:E / AndroidRuntime(10997):位于android.app.ActivityThread.main(ActivityThread.java:3689)07-30 15:20:48.339: E / AndroidRuntime(10997):at java.lang.reflect.Method.invokeNative(本机方法)07-30 15:20:48.339:E / AndroidRuntime(10997):at java.lang.reflect.Method.invoke(Method。 java:507)07-30 15:20:48.339:E / AndroidRuntime(10997):位于com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:875)07-30 15:20:48.339: E / AndroidRuntime(10997):位于com.android.internal.os.ZygoteInit.main(ZygoteInit.java:633)07-30 15:20:48.339:E / AndroidRuntime(10997):位于dalvik.system.NativeStart.main (本机方法)

Try adding this to your manifest: 尝试将其添加到清单中:

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

If that doesn't work, consult the answers to these two questions: 如果这不起作用,请参考以下两个问题的答案:

Android java.net.UnknownHostException: Host is unresolved Android java.net.UnknownHostException:无法解析主机

Android java.net.UnknownHostException: Host is unresolved (strategy question) Android java.net.UnknownHostException:无法解析主机(策略问题)

You will need to give your asynctask a timeout handler, since you are connected but not downloading anything. 您将需要为asynctask提供一个超时处理程序,因为您已连接但未下载任何内容。 Put a timeout for around 3000 - 5000ms 设置大约3000-5000ms的超时时间

How to create a timeout handler: 如何创建超时处理程序:

Android - Setting a Timeout for an AsyncTask? Android-为AsyncTask设置超时?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM