如何在Titanium中传递不同的值来创建请求字符串?

时间:2015-04-24 11:05:25

标签: titanium titanium-mobile titanium-alloy

我正在创建一个像这样的URL-String:

URLString = %@sale-ws/lots/sales?yardNumber=%@&saleFromDate=%@&range=%@&saleToDate=%@&pageSize=%@&pageNo=%@&sortBy=%@&sortType=%@&filterStartYear=%@&filterEndYear=%@&filterMake=%@&filterModel=%@&filterLocationId=%@&filterLane=%@&filterSellerId=%@&searchInSearchString=%@

所以在Titanium中我想将值动态传递给给定的字符串。 我试过这个,但没有得到适当的回应。

var urlParameterStringNew
=String.format(URLString,Alloy.Globals.SERVER_URL,"","","","","","","","","","","","","","","","");

3 个答案:

答案 0 :(得分:0)

您可以对网址组件进行编码,并将其与您的网址一起添加。

var encoded = Ti.Network.encodeURIComponent('Pass'+' your'+' string'+' here');
var urlString = "https://www.google.co.in/search?&q="+encoded;

答案 1 :(得分:0)

您可以定义自己的函数,返回所需的字符串值:

var params = [] //Add all your params to the array, order is important!

function myFunction("yourBaseString", params) { //Add all your param values
    yourString = yourBaseString;
    for (index = 0; index < params.length; ++index) {
        yourString = yourString.replace("%@", params[index]);
    }
    return yourString;
}

这应该有效,因为replace(“@%”,“paramString”)仅替换第一次出现的字符串。请试一试并提供一些反馈,因为我无法测试该功能!

答案 2 :(得分:0)

首先,我必须像这样创建我的网址

URLString = "%ssale-ws/lots/sales?yardNumber=%s&saleFromDate=%s&range=%s&saleToDate=%s&pageSize=%s&pageNo=%s&sortBy=%s&sortType=%s&filterStartYear=%s&filterEndYear=%s&filterMake=%s&filterModel=%s&filterLocationId=%s&filterLane=%s&filterSellerId=%s&searchInSearchString=%s",

然后我可以简单地将%s的值替换为像这样的任何其他值

var finalRequestString = String.format(URLString),Alloy.Globals.SERVER_URL,"","","","","","","","","","","","","","","","");