我在从 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/