0

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();
}
Siruk Viktor
  • 504
  • 1
  • 8
  • 30

1 Answers1

0

You should check the status code of your http request. The 3xx codes represents redirections. So you can react to them properly.

Thomas
  • 689
  • 3
  • 8
  • 14
  • in android HttpStatus this (int SC_TEMPORARY_REDIRECT) 307 Temporary Redirect (HTTP/1.1 - RFC 2616), I understood you correctly? Is it possible to find a link where I redirects? – Siruk Viktor Nov 20 '12 at 09:44
  • have not tried it by myself but you could try to implement an opposit of [how-to-provent-apache-http-client-from-following-a-redirect](http://stackoverflow.com/questions/1519392/how-to-prevent-apache-http-client-from-following-a-redirect) – Thomas Nov 20 '12 at 10:07