我正在编写这个应用程序,我需要同时打开两个 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;
}