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
You should use
and
Classic mistake. When comparing Strings, use
.equals()
: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)Compare strings with
.equals()
==
In Java compares by object reference since strings are objects in Java.Try
appidGet.equals("one")
Classic java mistake.
In java, “==” compares the reference of objects, so, use equals instead of ==.