0

我遇到了这种类型的问题 - 当我尝试下载某些网站的 favicon 时,您会遇到重定向(如 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();
}
4

1 回答 1

0

您应该检查您的 http 请求的状态码。3xx 代码代表重定向。所以你可以对它们做出适当的反应。

于 2012-11-20T09:37:29.793 回答