java - 无法使用 Android 从 Google Cloud Storage url 下载图像

标签 java android google-cloud-storage httpurlconnection apache-httpclient-4.x

我在从 Android 应用程序的 Google Cloud Storage 公共(public)网址下载图像时遇到问题。我的代码非常适合其他来源的图像。谷歌云存储中的图像也可以通过网络浏览器访问,没有任何问题。以此图为例:http://tin_images.storage.googleapis.com/1420678062-zmj1qJQe126843HbyJvbUI.jpg

这是一个说明问题的 Intent 服务:

public class TestService1 extends IntentService {

public TestService1(){
    super("TestService1");
}

@Override
protected void onHandleIntent(Intent intent) {
    String urlString="http://tin_images.storage.googleapis.com/1420678062-zmj1qJQe126843HbyJvbUI.jpg";
    try {
        HttpClient httpclient = new DefaultHttpClient();
        HttpGet httpget = new HttpGet(urlString);
        HttpResponse response = httpclient.execute(httpget);
    }
    catch(IOException e){
        e.printStackTrace();
    }

}
}

这给了我以下错误消息:
java.lang.IllegalArgumentException:主机名不能为空

我也尝试过使用 httpurlconnection:

public class TestService2 extends IntentService {

public TestService2() {
    super("TestService2");
}

@Override
protected void onHandleIntent(Intent intent) {
    String urlString="http://tin_images.storage.googleapis.com/1420678062-zmj1qJQe126843HbyJvbUI.jpg";
    try {
        URL url = new URL(urlString);
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.setRequestMethod("GET");
        urlConnection.setDoOutput(true);
        urlConnection.connect();
    }
    catch(IOException e){
        e.printStackTrace();
    }
}
}

此处的错误(在 catch block 中捕获)是
java.net.UnknownHostException: http://tin_images.storage.googleapis.com/1420678062-zmj1qJQe126843HbyJvbUI.jpg

在这两种情况下,如果我使用另一个网址,它就可以正常工作。我更喜欢使用 HttpClient,因为我在应用程序的其余部分中使用该库。

有什么办法可以解决这个问题吗?

最佳答案

如上所述here ,HttpClient好像不是support underscores for hostname .

此外,还有一个很长的discussion关于在域名和主机名上使用下划线。

关于java - 无法使用 Android 从 Google Cloud Storage url 下载图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27905356/

相关文章:

java - 如何让 TestNG-@Factory 以特定顺序实例化测试类?

android - Webview 链接在外部浏览器中打开

android - react native : Not receiving intent from other Android app

pyspark - 使用 pySpark 和 Cloud Storage 过滤数百万个文件

azure - 有没有办法使用 GCP 资源将数据从 Google Cloud Storage 传输到 Azure Blob Storage?

java - 如何仅为特定类配置 log4j2.xml 日志级别?

Java Spring : How to handle HTTP Status 400?

java - Spring MVC 中的重定向

Android:标签栏上的回收者 View

javascript - 如何从视频元素中删除“下载”按钮?