怎么可能知道该链接将被重定向 [英] How is it possible to know that the link will be redirected
问题描述
我碰到这种类型的问题 - 当我尝试下载一些网站的图标,你遇到一个重定向(如google.com / favicon.ico的 - > www.google.com /的favicon.ico)。在这种情况下,我的code不保存图像。我不知道是否有可能知道页面是否被重定向,或如何改变我的code来解决这个障碍。希望对你有所帮助。
@覆盖
保护布尔doInBackground(无效...参数){
Log.d(URL中NetworkTask,URL);
HTTPGET HTTPGET =新HTTPGET(URL);
AndroidHttpClient的HttpClient = AndroidHttpClient
.newInstance(机器人);
HTT presponse HTT presponse = NULL;
尝试{
HTT presponse = httpClient.execute(HTTPGET);
}赶上(IOException异常五){
e.printStackTrace();
}
如果(HTT presponse!= NULL)
主要codeStatus = HTT presponse.getStatusLine()
.getStatus code();
别的Log.d(HTT presponse,空);
如果(主codeStatus!= 0){
尝试{
urlFavIcon =新的URL(网址).getProtocol()
+://
+新网址(URL).getHost()
+/ favicon.ico的;
文件名=新的URL(网址).getHost()+(图标).JPG;
的System.out.println(urlFavIcon);
}赶上(MalformedURLException的E){
e.printStackTrace();
}
如果(!(urlFavIcon.equals())){
= HTTPGET HTTPGET新(urlFavIcon);
尝试{
HTT presponse = httpClient.execute(HTTPGET);
}赶上(IOException异常五){
e.printStackTrace();
}
尝试{
为=(java.io.InputStream中)
HTT presponse
.getEntity()
.getContent();
}赶上(IllegalStateException异常五){
e.printStackTrace();
}赶上(IOException异常五){
e.printStackTrace();
}
}
httpClient.close();
Log.d(启动服务,监视URL:+网址);
返回true;
}
否则返回false;
} @覆盖
公共无效onPostExecute(布尔参数){
如果(参数){
位图siteIcon = BitmapFactory.de codeStream(是);
字符串路径= Environment.getExternalStorageDirectory()的getPath()+/。
的System.out.println(Environment.getExternalStorageDirectory()canWrite());
文件fileFavIcon = NULL;
fileFavIcon =新的文件(路径,文件名);
如果(fileFavIcon = NULL&放大器;!&安培;!siteIcon = NULL){
尝试{
FOS的FileOutputStream =新的FileOutputStream(fileFavIcon);
siteIcon.com preSS(Bitmap.Com pressFormat.PNG,100,FOS);
fOS.flush();
fOS.close();
}赶上(FileNotFoundException异常五){
e.printStackTrace();
}赶上(IOException异常五){
e.printStackTrace();
}
} getActivity()
.startService(新意图(getActivity(),ExService.class)
.putExtra(UrlBox,新UrlBoxHelper(新UrlBox(URL,
60000))));
FragmentDialogAddNewUrlConectActivity conectActivity =
(FragmentDialogAddNewUrlConectActivity)getActivity();
conectActivity
.fragmentDialogClickButtonListener(URL,
drawableFavIconUrl);
}
其他{
吐司toastInfo =吐司
.makeText(getActivity()。getApplicationContext(),
这个链接不存在页
Toast.LENGTH_LONG);
toastInfo.setGravity(Gravity.TOP,0,0);
toastInfo.show();
} }
修改
在这里,他写道,解决我的问题:
= HTTPGET HTTPGET新(urlFavIcon);
的HttpParams PARAMS =新BasicHttpParams();
params.setParameter(http.protocol.handle-重定向,FALSE);
httpGet.setParams(PARAMS);
HTT presponse = httpClient.execute(HTTPGET);
头locationHeader = HTT presponse.getFirstHeader(位置);
如果(locationHeader!= NULL){
的System.out.println(locationHeader.getValue()); // locationHeader.getValue()获得新的链接(重定向)
urlFavIcon = locationHeader.getValue();
}
您应该检查的你的HTTP请求的状态code 。在3XX codeS重新presents重定向。所以,你可以给他们适当的反应。
I ran into a problem of this type - when I try to download the favicon of some sites, you come across a redirect (like google.com / favicon.ico -> www.google.com / favicon.ico). In this case, my code does not save the image. I wonder whether it is possible to know whether the page is redirected, or how to change my code to work around this obstacle. I hope for your help.
@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();
}
}
EDIT
Here he wrote that solved my problem:
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();
}
You should check the status code of your http request. The 3xx codes represents redirections. So you can react to them properly.
这篇关于怎么可能知道该链接将被重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!