Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to solve this incompatible types in java?

Tags:

java

generics

I get error in following lines.

error: incompatible types
required : java.util.Map.entry<java.lang.String,java.lang.String[]>
found :java.lang.Object         

full code is below

package com.auth.actions;

public class SocialAuthSuccessAction extends Action {

    final Log LOG = LogFactory.getLog(SocialAuthSuccessAction.class);

    @Override
    public ActionForward execute(final ActionMapping mapping,
            final ActionForm form, final HttpServletRequest request,
            final HttpServletResponse response) throws Exception {

        AuthForm authForm = (AuthForm) form;
        SocialAuthManager manager = null;
        if (authForm.getSocialAuthManager() != null) {
            manager = authForm.getSocialAuthManager();
        }
        if (manager != null) {
            List<Contact> contactsList = new ArrayList<Contact>();
            Profile profile = null;
            try {
                Map<String, String> paramsMap = new HashMap<String, String>();
                for (Map.Entry<String, String[]> entry :request.getParameterMap().entrySet() ) { // error in this line!
                    String key = entry.getKey();
                    String values[] = entry.getValue();
                    paramsMap.put(key, values[0].toString()); // Only 1 value is
                }
                AuthProvider provider = manager.connect(paramsMap);

                profile = provider.getUserProfile();
                contactsList = provider.getContactList();
                if (contactsList != null && contactsList.size() > 0) {
                    for (Contact p : contactsList) {
                        if (StringUtils.isEmpty(p.getFirstName())
                                && StringUtils.isEmpty(p.getLastName())) {
                            p.setFirstName(p.getDisplayName());
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            request.setAttribute("profile", profile);
            request.setAttribute("contacts", contactsList);

            return mapping.findForward("success");
        }
        // if provider null
        return mapping.findForward("failure");
    }
}

Please help

like image 541
Joe Avatar asked Feb 28 '12 08:02

Joe


2 Answers

You need to cast request.getParameterMap()to Map<String, String[]>

for (Map.Entry<String, String[]> entry :
     ((Map<String, String[]>)request.getParameterMap()).entrySet())
like image 94
Guillaume Polet Avatar answered Sep 19 '22 17:09

Guillaume Polet


Try the following:

for (Object obj :request.getParameterMap().entrySet() ) {
                Map.Entry<String, String[]> entry = (Map.Entry<String, String[]>) obj;
                String key = entry.getKey();
                String values[] = entry.getValue();
                paramsMap.put(key, values[0].toString()); // Only 1 value is
            }

Am not really sure this will work, anyway, you got the approach. Hope this helps.

like image 41
Egor Avatar answered Sep 19 '22 17:09

Egor