问题
I am creating a URL-String like this:
URLString = %@sale-ws/lots/sales?yardNumber=%@&saleFromDate=%@&range=%@&saleToDate=%@&pageSize=%@&pageNo=%@&sortBy=%@&sortType=%@&filterStartYear=%@&filterEndYear=%@&filterMake=%@&filterModel=%@&filterLocationId=%@&filterLane=%@&filterSellerId=%@&searchInSearchString=%@
So in Titanium i want to pass the values dynamically into a given string. I tried this one but did not get a proper response.
var urlParameterStringNew
=String.format(URLString,Alloy.Globals.SERVER_URL,"","","","","","","","","","","","","","","","");
回答1:
You can encode the url component and add this with your url.
var encoded = Ti.Network.encodeURIComponent('Pass'+' your'+' string'+' here');
var urlString = "https://www.google.co.in/search?&q="+encoded;
回答2:
You could define your own function returning the desired string value:
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;
}
This should work because replace("@%", "paramString") only replaces the first occurrence of the string. Please give it a try and give some feedback since I wasn't able to test the function!
回答3:
First change , i have to create my URL like this
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",
Then i can simply to replace the value of %s with any other value like this
var finalRequestString = String.format(URLString),Alloy.Globals.SERVER_URL,"","","","","","","","","","","","","","","","");
来源:https://stackoverflow.com/questions/29845861/how-to-create-a-request-string-with-passing-different-values-in-titanium