Search code examples
androidurl-encoding

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]

Solution

  • 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();
        }