このタイプの問題に遭遇しました。一部のサイトのファビコンをダウンロードしようとすると、リダイレクトに遭遇します (google.com / favicon.ico -> www.google.com / favicon.ico など)。この場合、私のコードは画像を保存しません。ページがリダイレクトされているかどうか、またはこの障害を回避するためにコードを変更する方法を知ることができるかどうか疑問に思っています。あなたの助けを願っています。
@Override
protected Boolean doInBackground(Void...param) {
Log.d("url in NetworkTask", url);
HttpGet httpGet = new HttpGet(url);
AndroidHttpClient httpClient = AndroidHttpClient
.newInstance("Android");
HttpResponse httpResponse = null;
try {
httpResponse = httpClient.execute(httpGet);
} catch (IOException e) {
e.printStackTrace();
}
if(httpResponse!=null)
primaryCodeStatus = httpResponse.getStatusLine()
.getStatusCode();
else Log.d("httpResponse", "NULL");
if(primaryCodeStatus != 0){
try {
urlFavIcon = new URL(url).getProtocol()
+"://"
+new URL(url).getHost()
+"/favicon.ico";
fileName = new URL(url).getHost()+"(FAVICON).jpg";
System.out.println(urlFavIcon);
} catch (MalformedURLException e) {
e.printStackTrace();
}
if(!(urlFavIcon.equals(""))){
httpGet = new HttpGet(urlFavIcon);
try {
httpResponse = httpClient.execute(httpGet);
} catch (IOException e) {
e.printStackTrace();
}
try {
is = (java.io.InputStream)
httpResponse
.getEntity()
.getContent();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
httpClient.close();
Log.d("START SERVICE", "monitor url: "+url);
return true;
}
else return false;
}
@Override
public void onPostExecute(Boolean param){
if(param){
Bitmap siteIcon = BitmapFactory.decodeStream(is);
String path = Environment.getExternalStorageDirectory().getPath()+"/";
System.out.println(Environment.getExternalStorageDirectory().canWrite());
File fileFavIcon = null;
fileFavIcon = new File(path, fileName);
if(fileFavIcon != null && siteIcon != null){
try {
FileOutputStream fOS = new FileOutputStream(fileFavIcon);
siteIcon.compress(Bitmap.CompressFormat.PNG, 100, fOS);
fOS.flush();
fOS.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
getActivity()
.startService(new Intent(getActivity(), ExService.class)
.putExtra("UrlBox", new UrlBoxHelper(new UrlBox(url,
60000))));
FragmentDialogAddNewUrlConectActivity conectActivity =
(FragmentDialogAddNewUrlConectActivity) getActivity();
conectActivity
.fragmentDialogClickButtonListener(url,
drawableFavIconUrl);
}
else {
Toast toastInfo = Toast
.makeText(getActivity().getApplicationContext(),
"This link does not exist page",
Toast.LENGTH_LONG);
toastInfo.setGravity(Gravity.TOP, 0, 0);
toastInfo.show();
}
}
編集
ここで彼は私の問題を解決したと書いています:
httpGet = new HttpGet(urlFavIcon);
HttpParams params = new BasicHttpParams();
params.setParameter("http.protocol.handle-redirects",false);
httpGet.setParams(params);
httpResponse = httpClient.execute(httpGet);
Header locationHeader = httpResponse.getFirstHeader("location");
if(locationHeader != null){
System.out.println(locationHeader.getValue());// locationHeader.getValue() get new link(redirect)
urlFavIcon = locationHeader.getValue();
}