【问题标题】:Android app hangs while trying to open two DataInputStreamsAndroid 应用程序在尝试打开两个 DataInputStreams 时挂起
【发布时间】:2013-03-17 13:20:25
【问题描述】:

我正在编写这个应用程序,我需要同时打开两个 InputStream 并能够切换到任一流以传输图像。我可以打开第一个流,但是当我尝试打开第二个流时它挂起。以下是代码,我在它挂起的地方做了评论,你能解释一下我是否做错了吗?

public boolean Start()
    {
        numberOfServicesUsingThisInstanceLock.lock();

        if (numberOfServicesUsingThisInstance > 0)
        {
            numberOfServicesUsingThisInstance++;
            return true; 
        }

        // else 
        numberOfServicesUsingThisInstance = 1;

        bisList.clear();
        disList.clear();        
        FrameTimeStampList.clear();

        try
        {
            for (int i = 0; i < this.objConfig.lstCameraInfo.size(); i++)
            {
                FrameTimeStampList.add(Long.valueOf("-1"));
                final CameraInfo ci = this.objConfig.lstCameraInfo.get(i);
                String sourceUrl = GetMjpegUrlForCam(this.Type, ci.brand, ci.ipAddress);

                Log.d("DUMPMJPEG_START", "URL: " + sourceUrl);

                if (sourceUrl == "NONE") continue;

                URL url = new URL(sourceUrl);               

                Authenticator.setDefault(new Authenticator() {
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication (ci.userName, ci.password.toCharArray());
                    }
                });

                Log.d("DUMPMJPEG_START", "OpenStream");
                InputStream in = url.openStream(); // CODE HANGS HERE
                Log.d("DUMPMJPEG_START", "Creating DataInputStream");
                DataInputStream dis = new DataInputStream(in);
                Log.d("DUMPMJPEG_START", "DataInputStream added to the DataInputStream List");

                Log.d("DUMPMJPEG_START", "adding BufferedInputStreams to the list");
                BufferedInputStream bis = new BufferedInputStream(dis);
                Log.d("DUMPMJPEG_START", "BufferendInputStreams added to the list");
                disList.add(dis);
                bisList.add(bis);
            }
        }
        catch(Exception ex)
        {
            ex.getMessage();
        }
        return false;       
    }

【问题讨论】:

    标签: java android url inputstream datainputstream


    【解决方案1】:

    当你说它挂起时,你是不是很长时间没有得到任何响应或者你得到一个异常?

    更多代码:

    1. 您打开流但从不关闭它们。或者您会控制应用的其他部分吗?
    2. 打开流而不使用某种连接超时是个坏主意。
    3. 该异常不会打印任何内容,因为您应该使用 e.printStackTrace() 打印它或将消息发送到某个日志/输出。

    【讨论】:

    • 我很长一段时间都没有得到任何回应,没有 II 没有得到任何例外 1)我有一个控制它们的应用程序的不同部分 2)同意 3)同意,但是当我调试它时这里没有异常..
    • 好吧,您似乎遇到了与服务器的连接问题。当您从代码外部连接时,它有效吗?你是在安卓模拟器上测试吗?对此功能进行单元测试怎么样?
    • 是的,我猜是这样,但我确定这不是连接问题,因为我可以打开第一个流,但不能打开第二个!。我没有使用设备
    • 如果你切换IP的顺序,让第一个变成第二个,第二个变成第一个,它应该会以某种方式失败,看起来好像它真的无法连接。另一个想法,Android有很多限制,你有没有检查过你没有设置一些Android Permission?至少你应该在你的 Manifest 中有 INTERNET 权限......
    猜你喜欢
    • 2019-12-21
    • 1970-01-01
    • 2018-02-04
    • 2023-04-04
    • 1970-01-01
    • 2021-01-25
    • 1970-01-01
    • 1970-01-01
    • 2021-09-29
    相关资源
    最近更新 更多