当我尝试在 url android 中运行一些空格时出现错误, 这是代码:
String strUrlLoginFullpath = "";
strUrlLoginFullpath = strUrlMain+"exl?u="+strUser+"&p="+strPass+"&t=1";
URL url = new URL(strUrlLoginFullpath);
publishProgress(40);
//membuka koneksi
URLConnection urlConn = url.openConnection();
urlConn.setConnectTimeout(50000);
urlConn.setReadTimeout(50000);
//digunakan untuk menangkap nilai dari server
BufferedReader in = new BufferedReader(
new InputStreamReader(
urlConn.getInputStream()));
publishProgress(60);
String inputLine;
int i=0;
while ((inputLine = in.readLine()) != null)
{
Log.v("Login",i+", return:" +inputLine+"; url: "+strUrlLoginFullpath);
if(i == 5)
{
Log.v("Login","RESULT >"+i+", return:" +inputLine+"; url: "+strUrlLoginFullpath);
str2 = inputLine;
}
i++;
}
in.close();
publishProgress(80);
publishProgress(100);
}
catch (MalformedURLException e)
{
Log.v("KxL", "MalformedURLException=" + e.toString());
}
catch (IOException e)
{
Log.v("KxL", "IOException=" + e.toString());
}
return null;
}
如您所见..我有在 strUrlLoginFullpath = strUrlMain+"exl?u="+strUser+"&p="+strPass+"&t=1";
中进行登录验证的命令,但有条件strUser 有时包含空格,这会使我的程序无法运行。那么如何解决这个问题呢?
最佳答案
URLEncoder.encode()
您的 URL 参数值,以便正确编码空格和其他特殊字符。
示例:
strUrlLoginFullpath = strUrlMain + "exl?u=" + URLEncoder.encode(strUser, "UTF-8") +
"&p=" + URLEncoder.encode(strPass, "UTF-8") + "&t=1";
关于java - android 如何处理url中的空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15896070/