Java URL非法字符
这是我的密码:Java URL非法字符,java,android,url,uri,Java,Android,Url,Uri,这是我的密码: HttpClient client = new DefaultHttpClient(); client.getParams().setParameter(CoreProtocolPNames.USER_AGENT, "android"); HttpGet request = new HttpGet(); request.setHeader("Content-Type", "text/plain; charse
HttpClient client = new DefaultHttpClient();
client.getParams().setParameter(CoreProtocolPNames.USER_AGENT, "android");
HttpGet request = new HttpGet();
request.setHeader("Content-Type", "text/plain; charset=utf-8");
Log.d("URL", convertURL(URL));
request.setURI(new URI(URL));
HttpResponse response = client.execute(request);
bufferedReader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer stringBuffer = new StringBuffer("");
String line = "";
String NL = System.getProperty("line.separator");
我不知道我的URL中有哪个错误:
http://localhost/CyborgService/chatservice.php?action=recive_game&nick_sender=mkdarkness&pass=MV030595&date_last=2012-11-18 09:46:37&id_game=1
我已经使用了一个函数来转换URL,但还没有工作。但是,如果我尝试在浏览器中打开此URL,它将成功打开
这是我的错误:
有一个空白字符:
…2012-11-18 09:46:37…
(在索引127处,正如错误消息所说)
尝试将其替换为
%20
您的URL中的位置127处有一个空格。生成的日期为“date_last=2012-11-18 09:46:37”,这会在打开URL时导致错误
URL中不正式接受空格,但您的浏览器会很高兴地将其转换为“%20”或“+”,这两种格式都是URL中空格的有效表示形式。您应该转义所有字符:您可以用“+”替换空格,或者只需传递字符串就可以了
要使用URLEncoder,请参见例如:仅使用URLEncoder参数值编码,而不是完整的URL。或者为URI使用一个构造函数,它有几个参数,而不是一个参数。您没有显示构造URL的代码,因此我无法对其进行显式注释。但是如果您有一个参数映射parameterMap
,它将类似于:
String url = baseUrl + "?";
for (String key : parameterMap.keys())
{
String value = parameterMap.get(key);
String encoded = URLEncoder.encode(value, "UTF-8");
url += key + "&" + encoded;
}
改天,我们可以讨论为什么Java需要设置编码,然后要求编码为“UTF-8”,而不是仅仅使用“UTF-8”作为默认编码,但现在这段代码应该可以做到这一点。这样做肯定会帮到你
HttpClient myClient = new DefaultHttpClient();
HttpPost myConnection = new HttpPost("http://192.168.1.2/AndroidApp/SendMessage");
try {
//Your parameter should be as..
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("messageText", msgText));
nameValuePairs.add(new BasicNameValuePair("senderUserInfoId", loginUserInfoId));
//set parameters to ur URL
myConnection.setEntity(new UrlEncodedFormEntity(nameValuePairs));
//execute the connection
HttpResponse response = myClient.execute(myConnection);
}
catch (ClientProtocolException e) {
//e.printStackTrace();
} catch (IOException e) {
//e.printStackTrace();
}
HttpClient myClient=newdefaulthttpclient();
HttpPost myConnection=新的HttpPost(“http://192.168.1.2/AndroidApp/SendMessage");
试一试{
//您的参数应为。。
List nameValuePairs=新的ArrayList(2);
添加(新的BasicNameValuePair(“messageText”,msgText));
添加(新的BasicNameValuePair(“SenderUserInfo”,LoginUserInfo));
//将参数设置为URL
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
//执行连接
HttpResponse response=myClient.execute(myConnection);
}
捕获(客户端协议例外e){
//e、 printStackTrace();
}捕获(IOE异常){
//e、 printStackTrace();
}
我还没有算出127个字符,但我猜问题出在2012-11-18 09:46:37
中的空格……是的,您需要使用URLCoder。问题远远不止于空间。这将生成在所有情况下使用的编码URL+1 LexinBlue,我尝试使用URLEncoder,但没有成功,我的url是:http%3A%2F%2F192.168.0.182%2FCyborgService%2Fchatservice.php%3partion%3receive_game%26nick_发送者%3dmkddark%26pass%3DMV030595%26date_last%3D2012-11-19+12%3A40%3A05%26id_game%3D1和其他错误:java.lang.IllegalStateException:Target主机不能为空,或者设置参数。感谢您的接受,我将添加一段关于如何使用URLCoder的内容。
HttpClient myClient = new DefaultHttpClient();
HttpPost myConnection = new HttpPost("http://192.168.1.2/AndroidApp/SendMessage");
try {
//Your parameter should be as..
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("messageText", msgText));
nameValuePairs.add(new BasicNameValuePair("senderUserInfoId", loginUserInfoId));
//set parameters to ur URL
myConnection.setEntity(new UrlEncodedFormEntity(nameValuePairs));
//execute the connection
HttpResponse response = myClient.execute(myConnection);
}
catch (ClientProtocolException e) {
//e.printStackTrace();
} catch (IOException e) {
//e.printStackTrace();
}