gpt4 book ai didi

安卓 java.net.UnknownHostException

转载 作者:行者123 更新时间:2023-11-30 02:50:58 24 4
gpt4 key购买 nike

我有下面的代码从 URL 下载图像。但是得到 UnknowHostException:

  class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;

public DownloadImageTask(ImageView bmImage) {
this.bmImage = bmImage;
}

protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];

Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return mIcon11;
}

protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
}
}

我这样调用这段代码

new DownloadImageTask((ImageView) findViewById(R.id.imageView2))
.execute(ocrResult.getImgUrl());

ocrResult.getImgUrl 的值如下:

http://thumbs1.ebaystatic.com/m/mwXFFYQ1tmdoRBaDVQG2tsA/140.jpg

我在 Android Manifest 中有以下权限:

  <uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>

下面是整个堆栈。在我看来,URL 编码不正确。我看到 %5c,然后是“/”。

11 17:34:19.109: E/Error(2815): http:%5C/%5C/thumbs4.ebaystatic.com%5C/m%5C/mdfwIytXczq_z11DEJ42J9g%5C/140.jpg
06-11 17:34:19.117: W/System.err(2815): java.net.UnknownHostException: http:%5C/%5C/thumbs4.ebaystatic.com%5C/m%5C/mdfwIytXczq_z11DEJ42J9g%5C/140.jpg
06-11 17:34:19.125: W/System.err(2815): at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:279)
06-11 17:34:19.132: W/System.err(2815): at com.android.okhttp.internal.http.HttpEngine.sendSocketRequest(HttpEngine.java:255)
06-11 17:34:19.132: W/System.err(2815): at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:206)
06-11 17:34:19.132: W/System.err(2815): at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:345)
06-11 17:34:19.140: W/System.err(2815): at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:296)
06-11 17:34:19.140: W/System.err(2815): at com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:179)
06-11 17:34:19.187: D/dalvikvm(2815): GC_FOR_ALLOC freed 1370K, 29% free 6738K/9360K, paused 39ms, total 39ms
06-11 17:34:19.195: W/System.err(2815): at java.net.URL.openStream(URL.java:470)
06-11 17:34:19.203: W/System.err(2815): at edu.sfsu.cs.orange.ocr.CaptureActivity$DownloadImageTask.doInBackground(CaptureActivity.java:854)
06-11 17:34:19.203: W/System.err(2815): at edu.sfsu.cs.orange.ocr.CaptureActivity$DownloadImageTask.doInBackground(CaptureActivity.java:1)
06-11 17:34:19.210: W/System.err(2815): at android.os.AsyncTask$2.call(AsyncTask.java:300)
06-11 17:34:19.218: W/System.err(2815): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
06-11 17:34:19.218: W/System.err(2815): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:240)
06-11 17:34:19.226: W/System.err(2815): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
06-11 17:34:19.226: W/System.err(2815): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
06-11 17:34:19.226: W/System.err(2815): at java.lang.Thread.run(Thread.java:841)

最佳答案

%5c 是 url 编码的反斜杠。看起来您的字符串的正斜杠正在使用反斜杠进行转义,如下所示:

http:\/\/thumbs1.ebaystatic.com\/m\/mwXFFYQ1tmdoRBaDVQG2tsA\/140.jpg

稍后当您传入此 URL 时,所有反斜杠都将转换为 %5c

确保您的字符串在传递到 DownloadImageTask 之前没有转义正斜杠。正确的做法是

url = url.replaceAll("\\", "");

关于安卓 java.net.UnknownHostException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24174849/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com