How to create a request string with passing different values in Titanium?

痴心易碎 提交于 2019-12-08 14:04:50

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!