症状:
无法完成getInputStream
。它失败了代码(HttpURLConnectionImpl.java:238
)
原始错误日志:
05-25 17:57:06.473 2675-2722 / com.manantial.raul.photogallery E / TAG:FlickrFetchr无法获取商品 java.io.FileNotFoundException:http://api.flickr.com/services/rest/?method=flickr.photos.getRecent&api_key=6f722a706254ed716d5abb9fb1f012c7&extras=url_s 在com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:238) 在com.manantial.raul.photogallery.FlickrFetchr.getUrlBytes(FlickrFetchr.java:37) 在com.manantial.raul.photogallery.FlickrFetchr.getUrl(FlickrFetchr.java:69) 在com.manantial.raul.photogallery.FlickrFetchr.fetchItems(FlickrFetchr.java:79) at com.manantial.raul.photogallery.PhotoGalleryFragment $ FetchItemsTask.doInBackground(PhotoGalleryFragment.java:45) at com.manantial.raul.photogallery.PhotoGalleryFragment $ FetchItemsTask.doInBackground(PhotoGalleryFragment.java:36) 在android.os.AsyncTask $ 2.call(AsyncTask.java:295) 在java.util.concurrent.FutureTask.run(FutureTask.java:237) 在android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:234) 在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:588) 在java.lang.Thread.run(Thread.java:818)
HttpURLConnection getResponseCode
= 403 / HTPP_FORBIDDEN
通过网络浏览器使用相同的网址时,没有错误,getResponseCode
可以/ 200。
事实:
使用的代码:
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
InputStream in = connection.getInputStream(); // fails, while HttpURLConnection = HTTP_FORBIDDEN 403
if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) { // connection resp code 403 FORBIDDEN
return null;
}
int bytesRead = 0;
byte[] buffer = new byte[1024];
while ((bytesRead = in.read(buffer)) > 0) {
out.write(buffer, 0, bytesRead);
}
out.close();
String temp = out.toByteArray().toString();
Log.i("TAG", TAG + "out.toByteArray is the data response: " + temp);
return out.toByteArray();
} finally {
Log.i("TAG", TAG + " connection response code: " + connection.getResponseCode());
connection.disconnect();
}
答案 0 :(得分:-1)
代码中使用的URL是:
当通过浏览器使用它时,它被转换为https,但在通过我的测试Android应用程序使用时却没有。解决方案只是在代码中将URL调整为https: