Barnali Bhattacharjee
Barnali Bhattacharjee

Reputation: 497

Android not encoding the url as per my requirement

I have following

username [email protected]

how "+" is encoded as %2B

encoded url is :http://test.in/api/voi/login?password=anshu&username=amit24%[email protected]

secondly if username is this amit24*[email protected] then how we encode this url ?

I had tried like this :

 String url=ConstantNet.URL_LOGIN+ "?password=" + password.getText().toString() + "&username=" + email_mobile.getText().toString();
            String encodedurl = null;
            try {
               encodedurl = URLEncoder.encode(url,"UTF-8");
                Log.e("urlEncoded",""+encodedurl);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }

Result i am getting from encoded url is like this :

https%3A%2F%2Fgoturbo.in%2Fapi%2Fvo%2Flogin%3Fpassword%anshu%26username%3Damit%2B1%40gmail.com

but the actual result i want like this :

http://test.in/api/voi/login?password=anshu&username=amit24%[email protected]

Upvotes: 0

Views: 78

Answers (2)

w568w
w568w

Reputation: 173

Try this.

public static String encodeUrl(String url)
{
    String encoded="";
    for(String i:Uri.parse(url).getQuery().split("&"))
        encoded+=(i.split("=")[0]+"="+ URLEncoder.encode(i.split("=")[1])+"&");
    return Uri.parse(url).getScheme()+Uri.parse(url).getSchemeSpecificPart()+encoded;
}

Upvotes: 0

Hitesh Dhamshaniya
Hitesh Dhamshaniya

Reputation: 2182

Please check below code.

String userName = "amit24*[email protected]";
    String password = "anshu";
    try {

        String encodePassword = Uri.encode(password);
        String encodeUserName = Uri.encode(userName);
        android.util.Log.e(TAG, "onCreate: " + String.format("http://test.in/api/voi/login?password=%s&username=%s", encodePassword, encodeUserName));
    } catch (Exception e) {
        e.printStackTrace();
    }

Upvotes: 2

Related Questions