String strUrl="http://192.168.16.xxx:8088/funadx-web/xxx/xxx/xxx/xxx";
HttpClient httpclient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(strUrl);
运行后,出现异常:java.net.URISyntaxException
网上找资料,原来是地址中涉及了特殊字符“-”,所以只能先把String转成URL,再能过URL生成URI的方法来解决问题
修改代码如下:
URL url = new URL(strUrl);
URI uri = new URI(url.getProtocol(), url.getHost(), url.getPath(), url.getQuery(), null);
HttpClient httpclient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(uri);
URI uri = new URI(url.getProtocol(), url.getHost(), url.getPath(), url.getQuery(), null);
HttpClient httpclient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(uri);
调试发现,post的时候端口不见了。。。
真真有点郁闷呀,还好我当时想这个肯定是可以设置端口的,但是怎么样设置端口呢?百度了一下,没找到解决方案(可能是我搜索时问题描述的不对)
还好此时想起程序猿男票说他看开源代码的好处。。。额。要不我看下源码。
哈哈,好开心,原来URI还有个构造函数,可以设置端口。bingo,问题解决。
最终代码如下:
URL postUrl = new URL(strUrl);
URI uri = new URI(url.getProtocol(), null, url.getHost(),url.getPort(),url.getPath(), url.getQuery(), null );
HttpClient httpclient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(uri);
URI uri = new URI(url.getProtocol(), null, url.getHost(),url.getPort(),url.getPath(), url.getQuery(), null );
HttpClient httpclient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(uri);
这次虽然解决的不是一个很难的问题,但对于我来说也算一个小突破了,还是小开心的,mark一下。