skip to Main Content

I have this function here that assembles an API call to eBay. It used to only work with one EbayKeys.appid now there arose a need to include a second one.

The parameter String appidGet is passed from another function that uses request.getParameter(“token”); I’ve output the value into catalina.out and the value gets passed, however the if/else statement does not get executed and it does not append the values onto the string.

I have tried it with several different values and none of them work. I’m completely new to Java and have no idea what I’m doing wrong here.

public String getSearchUrl(String qurl, List<String> excludes, String appidGet) throws UnsupportedEncodingException {
    String query = buildQuery(qurl, excludes);
    String safequery;
    try {
        safequery = URLEncoder.encode(getQuery(),charset);
        StringBuffer apicall = new StringBuffer();
        apicall.append(EbayKeys.endpoint).append("?").append("OPERATION-NAME=findItemsByKeywords")
        .append("&SERVICE-VERSION=").append(EbayKeys.version);

        //Does not execute
        if (appidGet == "one" ) {
            apicall.append("&SECURITY-APPNAME=").append(EbayKeys.appid1);
        }           
        else if (appidGet == "two" ) {
            apicall.append("&SECURITY-APPNAME=").append(EbayKeys.appid2);
        }

        System.out.println(getClass().getName() + " "
                + Utils.getTime()
                + " APP ID = " + appidGet);
        apicall.append("&GLOBAL-ID=").append(EbayKeys.globalid)
        .append("&keywords=").append(safequery)
        .append("&sortOrder=StartTimeNewest&paginationInput.entriesPerPage=2")
        .append(query);
        return apicall.toString();
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return "";
}

Thanks for any help in advance.

4

Answers


  1. You should use

    appidGet.equals("one") 
    

    and

    appidGet.equals("two") 
    
    Login or Signup to reply.
  2. Classic mistake. When comparing Strings, use .equals():

        if (appidGet.equals("one") ) {
            apicall.append("&SECURITY-APPNAME=").append(EbayKeys.appid1);
        }           
        else if (appidGet.equals("two") ) {
            apicall.append("&SECURITY-APPNAME=").append(EbayKeys.appid2);
        }
    

    For non-primitives, == tests if the two objects are the same object (same memory address), whereas .equals() tests if they “seem the same” (class-specific implementation)

    Login or Signup to reply.
  3. Compare strings with .equals()

    == In Java compares by object reference since strings are objects in Java.

    Try appidGet.equals("one")

    Login or Signup to reply.
  4. Classic java mistake.

    In java, “==” compares the reference of objects, so, use equals instead of ==.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search