How to add authentication token in header in Picasso library

前端 未结 6 1089
梦如初夏
梦如初夏 2021-01-01 05:26

I am using the picasso library to download the bitmap so in the api I need to pass the token in the headers. I tried below code from this thread Android Picasso

6条回答
  •  太阳男子
    2021-01-01 06:15

    I used another library AQuery and was able to get not only authorized access to picassa rolling in a few minutes but also the library used the phones credentials so it was extremely easy.

    Even if you don't use this library take a look at how I get the experimental method of including only the fields needed working below. The smaller results makes for faster network io and a huge difference in CPU. Because the JSON is smaller it parses faster and or the DOM for the xml is smaller it is built extremely fast.

    Here I'm using the experimental method of returning only fields I want for public albums for the user in XML.

    GoogleHandle handle = new GoogleHandle(this.getActivity(),
                AQuery.AUTH_PICASA, AQuery.ACTIVE_ACCOUNT);
    
        // experimental fields method encoding the data part of the query string only.
        String url = "";
        try {
            url = "https://picasaweb.google.com/data/feed/api/user/default?kind=album&access=public&fields="
                    + URLEncoder
                            .encode("entry(title,id,gphoto:numphotosremaining,gphoto:numphotos,media:group/media:thumbnail)",
                                    "UTF-8");
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            //whatever I know this will work
            // I hard coded the string.
        }
    
    
            aq.auth(handle).progress(R.id.pbTrackerAlbumsProgress)
                    .ajax(url, XmlDom.class, this, "renderAlbums");
    
    
    
    public void renderAlbums(String url, XmlDom xml, AjaxStatus status) {
        List entries = convertAll(xml);
    
    
        if (entries.size() > 0) {
            isAuthError = false;
            // if the xml iis null we can't display the list
            // we can setup the adapter
            aa = new ArrayAdapter(this.getActivity(),
                    R.layout.listview_item_album, entries) {
    
                public View getView(int position, View convertView,
                        ViewGroup parent) {
    
                    if (convertView == null) {
    
                        // convertView =
                        // View.inflate(getActivity().getBaseContext(),
                        // R.layout.listview_item_album, parent);
                        convertView = getActivity().getLayoutInflater()
                                .inflate(R.layout.listview_item_album, parent,
                                        false);
                    }
    
                    PicasaAlbum picasaAlbum = getItem(position);
    
                    AQuery aqLocal = aq.recycle(convertView);
    
                    aqLocal.id(R.id.albumTitle).text(picasaAlbum.title);
                    // aq.id(R.id.meta).text(picasaAlbum.author);
    
                    String tbUrl = picasaAlbum.thumbNailUrl.toString();
    
                    Bitmap placeholder = aqLocal
                            .getCachedImage(R.drawable.ic_launcher2);
    
                    if (aqLocal.shouldDelay(position, convertView, parent,
                            tbUrl)) {
    
                        aqLocal.id(R.id.tb).image(placeholder);
                    } else {
                        aqLocal.id(R.id.tb).image(tbUrl, true, true, 0,
                                R.drawable.ic_launcher2x, placeholder,
                                AQuery.FADE_IN_NETWORK, 0);
                    }
    
                    return convertView;
    
                }
    
            };
            ((TextView) view.findViewById(R.id.tvTrackerExistingAlbum))
                    .setText("Select the album for route marker photos");
            ((ProgressBar) view.findViewById(R.id.pbTrackerAlbumsProgress))
                    .setVisibility(View.GONE);
            ListView lv = (ListView) view.findViewById(R.id.lvTrackerAlbums);
    
            lv.setAdapter(aa);
            aa.notifyDataSetChanged();
            lv.setVisibility(View.VISIBLE);
        }
    }
    

提交回复
热议问题