0

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

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;       
    }
4

1 回答 1

1

说挂了,是不是半天没反应还是出现异常?

更多代码:

  1. 您打开流但从不关闭它们。或者你会对应用程序的另一部分进行控制吗?
  2. 打开流而不使用某种连接超时是一个坏主意。
  3. 该异常不会打印任何内容,因为您应该使用 e.printStackTrace() 打印它或将消息发送到某个日志/输出。
于 2013-03-27T13:11:36.613 回答