Java URI中的IllegalArgumentException
我想创建HttpPost请求,如下所示:Java URI中的IllegalArgumentException,java,android,url,Java,Android,Url,我想创建HttpPost请求,如下所示: http://site/searches.json -d"search[params_attributes[origin_name]=MOW"\ -d"search[params_attributes][destination_name]=IEV"\ -d"search[params_attributes[some]=SOME" 我用手工试过了,效果很好。 但我有一个错误: Caused by: java.lang.IllegalArgumentExc
http://site/searches.json -d"search[params_attributes[origin_name]=MOW"\ -d"search[params_attributes][destination_name]=IEV"\ -d"search[params_attributes[some]=SOME"
我用手工试过了,效果很好。
但我有一个错误:
Caused by: java.lang.IllegalArgumentException: Illegal character in path at index 43: http://somesite/searches.json -d"search[params_attributes][origin_name]=KBP"...
at java.net.URI.create(URI.java:776)
at org.apache.http.client.methods.HttpPost.<init>(HttpPost.java:79)
并获得:
Caused by: java.lang.IllegalStateException: Target host must not be null, or set in parameters.
at org.apache.http.impl.client.DefaultRequestDirector.determineRoute(DefaultRequestDirector.java:572)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:292)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
试用
URLEncoder.encode(url);
URLDecoder.decode(url);
对url进行编码和解码
尝试使用以下代码,并确保在清单文件中设置了所有权限
public String executeUrl(String url, Context context)
{
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse;
String result = null;
try
{
httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
if(httpEntity != null)
{
InputStream inputStream = httpEntity.getContent();
result = Parser.convertStreamToString(inputStream);
inputStream.close();
}
}
catch(UnknownHostException eUnknownHostException)
{
eUnknownHostException.printStackTrace();
result = null;
}
catch(ConnectTimeoutException eConnectTimeoutException)
{
// TODO: handle exception
}
catch(Exception exception)
{
exception.printStackTrace();
result = null;
}
return result;
}
private String convertStreamToString(InputStream ipStream)
{
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(ipStream), 8192);
StringBuilder stringBuilder = new StringBuilder();
String line = null;
try
{
while((line = bufferedReader.readLine()) != null)
{
stringBuilder.append(line + "\n");
}
}
catch(IOException ioException)
{
ioException.printStackTrace();
}
finally
{
try
{
ipStream.close();
}
catch(IOException ioException)
{
ioException.printStackTrace();
}
}
return stringBuilder.toString();
}
HttpPost请求=新的HttpPost(mMethodURI);HttpResponse response=mHttpClient.execute(请求);错误是在第一行所有权限设置,它的工作很好,直到我找到这个网址。问题出在url中。好的。。。下次在浏览器中粘贴url时,如果它正确,则只需继续编码。让我们看看有什么问题。我不能尝试在浏览器中的链接,因为它是一个POST方法。问题出现在意外的位置:)
public String executeUrl(String url, Context context)
{
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse;
String result = null;
try
{
httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
if(httpEntity != null)
{
InputStream inputStream = httpEntity.getContent();
result = Parser.convertStreamToString(inputStream);
inputStream.close();
}
}
catch(UnknownHostException eUnknownHostException)
{
eUnknownHostException.printStackTrace();
result = null;
}
catch(ConnectTimeoutException eConnectTimeoutException)
{
// TODO: handle exception
}
catch(Exception exception)
{
exception.printStackTrace();
result = null;
}
return result;
}
private String convertStreamToString(InputStream ipStream)
{
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(ipStream), 8192);
StringBuilder stringBuilder = new StringBuilder();
String line = null;
try
{
while((line = bufferedReader.readLine()) != null)
{
stringBuilder.append(line + "\n");
}
}
catch(IOException ioException)
{
ioException.printStackTrace();
}
finally
{
try
{
ipStream.close();
}
catch(IOException ioException)
{
ioException.printStackTrace();
}
}
return stringBuilder.toString();
}