0

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

3 Answers 3

1

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();
Sign up to request clarification or add additional context in comments.

3 Comments

Then what you need is: URLEncoder.encode is from package: java.net.URLEncoder;
I dont get what is your problem, you need to send your values in the url encoded. The decoding may be in the server side. Why you want to send a decoded url?
I have dynamic string like in my question and want to post this string to server now if I encoding whole string then getting result hello+how+r like this but I want Hello how r u? etc like this way
1

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.

1 Comment

u r encoding the string not the url I want only encoding the string url not whole string and string was dynamic which entered by the user
0

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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.