简体   繁体   中英

Escape string from EditText

I was just wondering, if I have an EditText that I'm turning the content of into a string, say

String queryStr = new String(searchText.getText().toString());

How would I go about escaping special characters from this string so that I could put it into an HttpGet method? Thanks!

使用java.net.URLEncoder

java.net.URLEncoder.encode(queryStr, "UTF-8");

Try:

TextUtils.htmlEncode(queryStr);

I think that should do what you want.

Try smth like this:

import android.net.Uri;

...

private String getParams(String queryStr) {
    Uri.Builder builder = new Uri.Builder();
    builder.appendQueryParameter("your_parameter_key", queryStr);
    return builder.toString();
}

It will give you the properly encoded params string to append to your base url.

On a side note, there's no reason to use new String() here.

String queryStr = new String(searchText.getText().toString());

should just be

String queryStr = searchText.getText().toString();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM