Reputation: 30855
I need to post string containing url to the server I have able to encoding the url but this will encoding whole string and I need to require only url link to be encoded and decode when get this data back to display in android.
mydomain/post.php?mesg=Hello%2Bhow%2Br%2Bu%250A%2BHttp%253A%252F%252Fwww.google.com%250A%250ATesting%2Burl
Edited
My code
String postStr = "Hello how r u?
http://www.google.com"; // suppose user entered string like this way
Uri.Builder builder = new Uri.Builder()
.scheme("http")
.authority(domain)
.path(pageName)
.appendQueryParameter("mesg", URLEncoder.encode(postStr, "utf-8"))
String urlStr = builder.build().toString();
After executing this getting this result
Hello+how+r+u%0A+Http%3A%2F%2Fwww.google.com%0A%0ATesting+url
And actual result want like this
Hello how r u?
http://www.google.com
Testing url
Thanks in advance
Upvotes: 0
Views: 4946
Reputation: 3617
Build the dynamic string separating url and the text. For instance, you could do like
String postStr = Hello how r u? %& http://www.google.com
As you can see, I used a combination of symbols as separator.
Now use split the string,
String[] values = postStr.split("%&");
You could also use StringTokenizer class!
Hope this helps!
Upvotes: 0
Reputation: 25830
Try using Beloq Way
you should not encode whole String containing URL.just encode the Part you want to encode and decode.i think below is the Way to go. try it out. though i am not Sure this is the thing you are looking for.
String name=URLEncoder.encode("Hello hw are you?")+"http://www.google.com"+URLEncoder.encode("Testing url");
Hope it will Help somehow.
Upvotes: 1
Reputation: 2503
Try this:
import android.net.Uri;
import android.net.Uri.Builder;
String reqUrl = "http://google.com"
Builder builder = Uri.parse(reqUrl).buildUpon();
builder.appendQueryParameter("mesg", "Value for mesg");
String finalUri = builder.build().toString();
Upvotes: 1