如果多次使用,InputStream 将不起作用

发布于 2024-12-03 11:05:42 字数 3427 浏览 0 评论 0原文

我在使用输入流时遇到一些问题。我正在编写一个小型 Android 应用程序,其中一部分必须从网站获取 HTML 代码。一般来说,它工作得很好,但有时(通常是第二次调用它,但也可能需要几次尝试才能重现这一点)它只会跳过InputStream(我注意到这一点,因为调试时需要几秒钟,但每次如果失败,它会立即跳到下一行)。有什么想法可能导致此问题以及如何解决它吗?

    private class fetchdata extends AsyncTask<Void, Void, Void> {
            public Activity activity;      
            public fetchdata(Activity a)
            {
                activity = a;
            }  
            protected Void doInBackground(Void...voids)
            {
                String[] page = new String[16384]; //Number is just for testing, don't worry
                try {
                                page = executeHttpGet();
                        } catch (Exception e) {
                                page[0] = "Error";
                        }
                displayFetchedData(page);
                return null;
            }
    public String[] executeHttpGet() throws Exception {
         URL u;
        InputStream is = null;
        DataInputStream dis = null;
        String s;
        int i = 0;
        int hostselection;
        boolean skip;
        String[] page = new String[16384];
        String[] serverurls = new String[2];
        addSecurityException();
        SharedPreferences dataprefs = getSharedPreferences("serverdata", Context.MODE_PRIVATE);        
        hostselection = dataprefs.getInt("selectedhost", 0);
        SharedPreferences preferences;
        preferences = PreferenceManager.getDefaultSharedPreferences(activity);
        serverurls[0] = preferences.getString("server01", "");
        serverurls[1] = preferences.getString("server02", "");
        for (int j = 0; j < 2; j++)
        {
         skip = false;
         if (j == 0)
         {
                         if (hostselection == 0 || hostselection == 1)
                             {
                                 Authenticator.setDefault(new MyAuthenticator(activity, false));
                             }
                         else
                         {
                                 skip = true;
                         }
         }
         if (j == 1)
         {
                         if (hostselection == 0 || hostselection == 2)
                             {
                                 Authenticator.setDefault(new MyAuthenticator(activity, true));
                             }
                         else
                         {
                                 skip = true;
                         }
         }
         if (skip == false)
         {
                         try {
                            u = new URL(serverurls[j]);
                            is = u.openStream();        //LINE IN QUESTION
                            dis = new DataInputStream(new BufferedInputStream(is));
                            while ((s = dis.readLine()) != null)
                            {
                                if (s.length() > 18)
                                {
                                        page[i] = s;
                                        i++;
                                }
                            }
                         }
                         catch (IOException ioe)
                         {
                                 ioe.printStackTrace();
                         }
                         is.close();
                      }
        }
        return page;
   }

I'm having some problems with an InputStream. I'm writing a little Android application and part of it has to fetch HTML code from a website. Generally, it works fine, but sometimes (usually the second time it's called, but it may also take a few tries to reproduce this) it will just skip the InputStream (I noticed this since it takes a few seconds while debugging, but every time it fails it will just immediately skip to the next line). Any ideas what could be causing this and how to fix it?

    private class fetchdata extends AsyncTask<Void, Void, Void> {
            public Activity activity;      
            public fetchdata(Activity a)
            {
                activity = a;
            }  
            protected Void doInBackground(Void...voids)
            {
                String[] page = new String[16384]; //Number is just for testing, don't worry
                try {
                                page = executeHttpGet();
                        } catch (Exception e) {
                                page[0] = "Error";
                        }
                displayFetchedData(page);
                return null;
            }
    public String[] executeHttpGet() throws Exception {
         URL u;
        InputStream is = null;
        DataInputStream dis = null;
        String s;
        int i = 0;
        int hostselection;
        boolean skip;
        String[] page = new String[16384];
        String[] serverurls = new String[2];
        addSecurityException();
        SharedPreferences dataprefs = getSharedPreferences("serverdata", Context.MODE_PRIVATE);        
        hostselection = dataprefs.getInt("selectedhost", 0);
        SharedPreferences preferences;
        preferences = PreferenceManager.getDefaultSharedPreferences(activity);
        serverurls[0] = preferences.getString("server01", "");
        serverurls[1] = preferences.getString("server02", "");
        for (int j = 0; j < 2; j++)
        {
         skip = false;
         if (j == 0)
         {
                         if (hostselection == 0 || hostselection == 1)
                             {
                                 Authenticator.setDefault(new MyAuthenticator(activity, false));
                             }
                         else
                         {
                                 skip = true;
                         }
         }
         if (j == 1)
         {
                         if (hostselection == 0 || hostselection == 2)
                             {
                                 Authenticator.setDefault(new MyAuthenticator(activity, true));
                             }
                         else
                         {
                                 skip = true;
                         }
         }
         if (skip == false)
         {
                         try {
                            u = new URL(serverurls[j]);
                            is = u.openStream();        //LINE IN QUESTION
                            dis = new DataInputStream(new BufferedInputStream(is));
                            while ((s = dis.readLine()) != null)
                            {
                                if (s.length() > 18)
                                {
                                        page[i] = s;
                                        i++;
                                }
                            }
                         }
                         catch (IOException ioe)
                         {
                                 ioe.printStackTrace();
                         }
                         is.close();
                      }
        }
        return page;
   }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

秋千易 2024-12-10 11:05:42

从您获得的输入流中创建一个 BufferedInputStream ,然后调用 mark() 方法,以输入流长度作为参数。当您下次需要重用该流时,请调用reset()

Create a BufferedInputStream out of the input stream you get, then Call mark() method with the input stream length as parameter. Call reset() when you need to reuse the stream next time.

哥,最终变帅啦 2024-12-10 11:05:42

不相关,但您没有关闭 DataInputStream。

告诉我们更多有关跳过的信息。是否引发异常?当您在调试模式之外运行它时,它是否有可能以某种方式引用过时的类文件?我唯一能想象的是,您的调试类和普通类在某种程度上有所不同。

Unrelated, but you aren't closing the DataInputStream.

Tell us more about the skipping. Is an exception raised? Is it possible that when you are running it outside of debug mode it is somehow referencing stale class files? The only thing I can imagine is that somehow your debug and normal classes are somehow different.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文