我有以下
username amit24+1@gmail.com
“+”如何编码为%2B
已编码的网址为:http://test.in/api/voi/login?password=anshu&username=amit24%2B1@gmail.com
其次如果用户名是amit24*1@gmail.com那么我们如何对这个网址进行编码?
我曾经这样试过:
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();
}
我从编码网址获得的结果是这样的:
https%3A%2F%2Fgoturbo.in%2Fapi%2Fvo%2Flogin%3Fpassword%anshu%26username%3Damit%2B1%40gmail.com
但实际结果我想这样:
http://test.in/api/voi/login?password=anshu&username=amit24%2B1@gmail.com
答案 0 :(得分:1)
请检查以下代码。
String userName = "amit24*1@gmail.com";
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();
}
答案 1 :(得分:0)
试试这个。
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;
}