0

I construct a URL with the following bit of code:

String login = rootActivity.getString(R.string.url_authentication);
login = login + "user=" + mySharedPreferences.getString("username", "invalid") + "&" + "key=" + mySharedPreferences.getString("key", "invalid");
login = login.toLowerCase(Locale.US);
System.out.println("Logging in at " + login);
new HttpConnection(handler).get(login);

The URL is valid, as far as I can see visually, but the HttpConnection fails because there's an illegal character in the URL at the index of the ampersand. What really flummoxes me is, the app has between 1,000 and 5,000 installs, and we have a total of two reports of this over the past year – both from American users using Samsung devices, so I doubt it's a character encoding issue.

4

3 回答 3

1

有一个原始的 & (amplisand) 会导致错误,你应该编码你的特殊字符。

做这个:

 String login = URLEncoder.encode(login);

http://developer.android.com/reference/java/net/URLEncoder.html#encode(java.lang.String)

于 2012-11-28T15:44:39.770 回答
1

不要忘记对您的参数进行 urlEncode。

login = login + "user=" + URLEncoder.encode( mySharedPreferences.getString("username", "invalid") ) + "&" + "key=" + URLEncoder.encode( mySharedPreferences.getString("key", "invalid") );
于 2012-11-28T15:45:47.623 回答
1

不知道登录是什么,但是最后有问号吗?如果您不是 www.example.comuser="something"&key="somethingElse" 之类的东西,那将行不通。

于 2012-11-28T15:43:52.557 回答