0

我有一个从 Flickr 网站爬取的图像 ID 列表

我需要得到它的位置(纬度和经度)

这是我的代码

File source = //images id 
File target = // images location

Scanner scanner = null;
PrintStream pStream =null;
String apiKey = //;
Flickr flick = new Flickr(apiKey);

PhotosInterface photosInterface=    flick.getPhotosInterface();
try {
    scanner= new Scanner(source);
    pStream= new PrintStream(target);       
    while (scanner.hasNext())
    {       
        String line = scanner.nextLine();       
        if (photosInterface.getPhoto(line).getGeoData()!=null)
        {
            pStream.println(Float.toString(photosInterface.getPhoto(line).getGeoData().getLatitude())+"\t"+Float.toString(photosInterface.getPhoto(line).getGeoData().getLongitude()));
        }
        else pStream.println("null");
    }           
    System.out.println(lineNumber);
}
catch (Exception e) {
}

}

在这种情况下,我想执行这条指令

pStream.println("null");

的情况下:

图片不可用(FlickrException)

或图像未与 GeoData 关联

这个怎么做?

4

1 回答 1

0

我猜你需要包围FlickrException在一个try {..} catch块中抛出 a 的调用。

例如,如果在没有可用图像时photosInterface.getPhoto(line)抛出 a ,则:FlickrException

while (scanner.hasNext())
{       
    String line = scanner.nextLine();       
    try {
        if (photosInterface.getPhoto(line).getGeoData()!=null)
        {
            pStream.println(Float.toString(photosInterface.getPhoto(line).getGeoData().getLatitude())+"\t"+Float.toString(photosInterface.getPhoto(line).getGeoData().getLongitude()));
        }
        else pStream.println("null");
    } catch (FlickrException fe) {
        // You may need to check something about the exception
        // here in case it can be thrown for a different reason
        // than images are not available 
        pStream.println("null");
    }
}           
System.out.println(lineNumber);
于 2013-04-23T21:30:16.447 回答