我这里有这个功能,它组装了一个对eBay的API调用.它过去只用于一个EbayKeys.appid,现在需要包含第二个.
参数String appidGet从另一个使用request.getParameter("token")的函数传递; 我将值输出到catalina.out并且值被传递,但是if/else语句没有被执行,并且它没有将值附加到字符串上.
我尝试了几个不同的值,但没有一个工作.我是Java的新手,不知道我在这里做错了什么.
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 "";
}
Run Code Online (Sandbox Code Playgroud)
在此先感谢您的帮助.
经典错误.比较字符串时,使用.equals():
if (appidGet.equals("one") ) {
apicall.append("&SECURITY-APPNAME=").append(EbayKeys.appid1);
}
else if (appidGet.equals("two") ) {
apicall.append("&SECURITY-APPNAME=").append(EbayKeys.appid2);
}
Run Code Online (Sandbox Code Playgroud)
对于非原语,==测试两个对象是否是同一个对象(相同的内存地址),而.equals()测试它们是否"看起来相同"(特定于类的实现)
你应该用
appidGet.equals("one")
Run Code Online (Sandbox Code Playgroud)
和
appidGet.equals("two")
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
686 次 |
| 最近记录: |