Results 1 to 3 of 3
  1. #1
    guerlando is offline Member
    Join Date
    Mar 2016
    Posts
    11
    Rep Power
    0

    Default function that returns a map <String,Object>, and using that Object as an String

    I'm writing a function called simpleRequest that requests a page and returns its response, http code and cookies via a HashMap:

    Java Code:
    public static Map<String, Object> simpleRequest(String url, String postQuery, List<String> cookies) throws Exception {
    Inside it, there's a HTTPs connection declared like this:

    Java Code:
    HttpsURLConnection con = (HttpsURLConnection)myurl.openConnection();
    Then I do this to return the values:

    Java Code:
    List<String> rCookies = con.getHeaderFields().get("Set-Cookie");
        String responseCode = Integer.toString(con.getResponseCode());
        String responseMessage = con.getResponseMessage();
        
        Map<String,Object> r = new HashMap<String,Object>();
        r.put("response",response);
        r.put("responseCode",responseCode);
        r.put("message",responseMessage);
        r.put("cookies",rCookies);
        if (responseCode.equals("302")){
          r.put("location", con.getHeaderFields().get("Location").get(0));
        }
        return(r);
    Here, return(r) is the return of the function simpleRequest. I need to input some of these informations into a new simpleRequest() call, like this:

    Java Code:
    Map<String,Object> do_login = simpleRequest(page_url, query, null);
    Map<String,Object> first_screen = simpleRequest(do_login.get("location"), new String(), do_login.get("cookies"));
    The first call, called do_login, will return a Map with a new location and cookies, which I'm trying to insert into simpleRequest again, as you can see in first_screen. Here's the error:

    Java Code:
    Main.java:94: error: method simpleRequest in class Main cannot be applied to given types;                   
        Map<String,Object> first_screen = simpleRequest(do_login.get("location"), new String(), do_login.get("cookies"
    ));                                                                                                               
                                          ^                                                                           
      required: String,String,List<String>                                                                            
      found: Object,String,Object                                                                                     
      reason: actual argument Object cannot be converted to String by method invocation conversion
    I can't understand what's happening, since I thought it was returning the right values...

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,807
    Rep Power
    13

    Default Re: function that returns a map <String,Object>, and using that Object as an String

    Quote Originally Posted by guerlando View Post
    required: String,String,List<String>
    found: Object,String,Object
    Check out the error message. The call to Map.get will return an Object and yet your method expects a String for the first parameter and a List<String> as the third parameter.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: function that returns a map <String,Object>, and using that Object as an String

    Instead of using a Map I would suggest creating a simple class that contains the three things you need, 2 Strings for the response and responseCode and a List<String> for the cookies.
    This will save you having to cast anything and it will ensure the correct types are passed around.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 7
    Last Post: 07-11-2015, 03:29 AM
  2. Replies: 2
    Last Post: 08-25-2013, 11:32 PM
  3. Replies: 1
    Last Post: 01-09-2012, 04:46 PM
  4. Casting a String object to a Form object
    By kakinyim in forum CLDC and MIDP
    Replies: 0
    Last Post: 04-23-2011, 11:41 AM
  5. String vs Object
    By Gilgamesh in forum New To Java
    Replies: 1
    Last Post: 11-28-2007, 10:26 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •