how to add a data to webservice with special characters? - android

i am trying to add a two strings to the web service its working fine when i send through browser and it returns "inserted successfully"
but when i run my application its displaying an error as E/Responce(17407): Illegal character in query at index 74: http://purple2.com/beerbro/addgroupon.php?userid=27&data=100001190851696,1|100001640732983,1|100002430763518,1|100002332633534,1|100000549596039,1|1375802933,1|1587585991,1|1569563632,1|100000132426450,1|100004296815391,1|1519195978,1
,
i think there is a problem with "|" this but i am not able to find the solution
here is the code
for(int i=0; i
data=data+friendid[i]+","+status[i]+"|";
data.replaceAll("","%20");
if(i==0){
}
else if(i==10)
{
String res=UrltoValue.getValuefromUrl(DataUrls.addfriendsurl+"?userid="+usid+"&data="+data.substring(0,data.length()-1));
Log.e("res",DataUrls.addfriendsurl+"?userid="+usid+"&data="+data.substring(0,data.length()-1));
Log.e("Responce",res);
data="";
}

if '|' is the problem then use URLEncoder and encode the String
String data = URLEncoder.encode("100001190851696,1|100001640732983,1|100002430763518,1|100002332633534,1|100000549596039,1|1375802933,1|1587585991,1|1569563632,1|100000132426450,1|100004296815391,1|1519195978");
String mainURL = "http://purple2.com/beerbro/addgroupon.php?userid=27&data="+data;
Hope this helps you.

Related

Android : Split string with { character

I have an string
String name = "\"edge_followed_by\":{\"count\":46199005},\"followed_by_viewer\":false,"
I want only this 46199005.
But { shows an error, when try to split the string
String[] separated = name.split("edge_followed_by\":{\"count\":");
Showing a suggestion , number expected and want me to replace with *.
Can anyone help me in this.
Just replace { with \{.
split is trying to use it as a part of regular expression.
Ideally, you should use JSON to parse this if you have proper structure. but if you want to get only the number you can split it using ":" and then split using "}". it should give you the exact number.
Why not to use:
String[] separated = name.split(":");
separated[2].split("}")[0];
Your string is not exact JSON object otherwise you can simply do json parsing and get the count value.
You can get count value using subtring operations like below:
String name = "\"edge_followed_by\":{\"count\":46199005},\"followed_by_viewer\":false,";
String substr = name.substring(name.indexOf("\"count\":") + 10);
String finalstr = substr.substring( 0, substr.indexOf("},"));
Log.d("Extracted_Value", finalstr); // output -> 46199005
There can be multiple ways. This is just one. Hope it will help you!

How do I parse a HTTP Response of following format

I am using the bulk sms api to trigger send an sms to mobile number in and android phone. The format of the bulk sms api is follows
http://xxx.xxx.xxx/bulksms/bulksms?username=fds-xxx&password=xxx&type=0&dlr=1&destination=9422522891,8275004333&source=xxx&message=18%20December
The following is the response I can get in android as string using code
bytesSent = httpclient.execute(httppost, res);
response below
1701|919422522891:224c1214-bb95-414d-ba76-77db95370545,1701|918275004333:5e93a439-2644-4455-9f01-f27e6cf0cde6
How do I parse this response like key value pairs ?
A little success with following code , but it fails when the regex char is '|'
public String[] split(String regex,String input)
{
input = "1701|919422522891:224c1214-bb95-414d-ba76-77db95370545,1701|918275004333:5e93a439-2644-4455-9f01-f27e6cf0cde6";
regex = "|"; // does not work
//regex = ":"; // works correct
String[] soso = Pattern.compile(regex).split(input, input.length());
for (String s : soso) {
Log.e("TAG",s.toString());
}
return null;
}
for regex char '|'
I get a Log output as single characters string array like {"1","7","0",........}
UPDATE
regex = "\\|" // works fine
Use the split() method of String to split the response into different entries.
Loop through the resulting array
use split() again to separate keys from values
store the result in a map, result[0] is the key,result[1] is the value
if you need to maintain order make sure you use a map that does that, e.g. LinkedHashMap

How to get Text written after "," in MultiAutoCompleteTextView?

I am developing an application that uses MultiAutoCompleteTextView for showing hints in the drop down list.In this application I retrieve the value written in the MultiAutoCompleteTextView by using
multitextview.getText();
and then query this value to server to recieve JSON response which is shown as suggestions in the drop down list.
If a user types Mu and then Selects music from the list and then types box for another suggestion the content in the MultiAutoCompleteTextView becomes Music,box and now the value for querying to the server is Music,box instead of this I want to select only box.
My question is how to retrieve text written after "," in MultiAutoCompleteTextView?
Can this be achieved using getText()?
I solved this issue
String intermediate_text=multitextview.getText().toString();
String final_string=intermediate_text.substring(intermediate_text.lastIndexOf(",")+1);
I'm sure there are several ways to get around this. One way to do it would be:
String textToQuerryServer = null;
String str = multitextview.getText().toString(); // i.e "music, box" or "any, thing, you , want";
Pattern p = Pattern.compile(".*,\\s*(.*)");
Matcher m = p.matcher(str);
if (m.find()) {
textToQuerryServer = m.group(1);
System.out.println("Pattern found: "+ textToQuerryServer);
}else {
textToQuerryServer = str;
System.out.println("No pattern: "+ textToQuerryServer);
}

Extract text from String

I have one String and into this string I have a url between two characters # such as "Hello world #http://thisurl# my name is Pippo" I want to take the url (http://thisurl) between two #.
How can I do ? Thanks
String data[] = str.split("#"); //spilliting string and taking into array
ArrayList<String> urlList = new ArrayList<String>();
for (int i = 0; i < data.length; i++) {
if(data[i].contains("http://"))
urlList.add(data[i]); //if string contains "http://" it means it is url save int list.
}
now you can get all uls from urlList.get(i) method.
this urlList will give you all the urls available in the string. I dint applied any null or other check. Apply it and try. If want something else try modifying content and checks.
Try String.split(). You really should be trying to google these things first.
here is an example - http://www.java-examples.com/java-string-split-example
The split method divides a string into several strings and store them into an array using a delimiter which can be defined by you.
the second element in the resulting array will be your URL

how to change the page number of the url in android

I have a url which have a response in Json
lon=-0.1275&pg=0
I parsed the data from it and displayed in ListView, after displaying the 20 fields the above url should be changed to
lon=-0.1275&pg=1
ie the "pg" must change from 0 to as many pages. How to do that?
please provide me some help
Thanks in advance
You can have a class level variable
int page = 0;
and when you make your url just append it like this:
url = "http://dentonsweb.com/app/html/android/get.php?what=Restaurants&lat=51.507222&lon=-0.1275&pg="+page;
and every time increment it with your pagination like this:
page+=10;
do the request again.
You may use string's replace method to modify url string:
String url = "http://dentonsweb.com/app/html/android/get.php?what=Restaurants&lat=51.507222&lon=-0.1275&pg=0";
url = url.replace("&pg=0", "&pg=1");
I think this way, NOT TESTED.
url = "http://dentonsweb.com/app/html/android/get.php?what=Restaurants&lat=51.507222&lon=-0.1275&pg=";
int i=0;
do{
tempURL = url+i;
// get tempURL response
if(response==null) break;
else
{
// parse response
i++;
}
}while(response!=null)

Categories

Resources