java.lang.IllegalStateException:目标主机不能为空,或参数设置 [英] java.lang.IllegalStateException: Target host must not be null, or set in parameters

查看:1986
本文介绍了java.lang.IllegalStateException:目标主机不能为空,或参数设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ULR:

<$p$p><$c$c>http://23.23.82.251:9116/solr/db/select?q=*:*+_val_:%22geodist%28%29%22&fq=(ProviderType:10)&rows=100&wt=json&indent=true&fl=ProviderID,ProviderType,ProviderName,ProviderAddress,CityAreaID,CityAreaName,City,State,Latlong,score&fq={!geofilt}&sfield=Latlong&pt=20.296059,85.82454&d=5&sort=geodist()%20asc

当我尝试使用这个网址的机器人与post请求我收到以下错误。

  10-22 13:54:49.946:E /信息搜索结果(6659):异常名称= java.lang.IllegalArgumentException异常
 

我是用URLEn coder.en code(),但仍然我得到同样的错误。可以在任何建议我在哪里写错误。低于code我使用

 的HttpParams httpParameters =新BasicHttpParams();
            //毫秒设置超时,直到建立了连接。
            //默认值是零,这意味着在超时不被使用。
            INT timeoutConnection = 10000;

            HttpConnectionParams.setConnectionTimeout(httpParameters,timeoutConnection);

            //设置默认套接字超时(SO_TIMEOUT)
            //毫秒这是超时等待数据。
            INT timeoutSocket = 15000;
            HttpConnectionParams.setSoTimeout(httpParameters,timeoutSocket);
            HttpClient的=新DefaultHttpClient(httpParameters);

            HttpPost httppost =新HttpPost(URL这里);
            JSONObject的JSON =新的JSONObject();
            //执行HTTP POST请求
            HTT presponse响应= httpclient.execute(httppost);
            HttpEntity httpEntity = response.getEntity();
            InputStream的河道= httpEntity.getContent();
            字符串结果= Utilities.convertStreamToString(河道);

            JSON =新的JSONObject的(结果);
 

解决方案

您不能连接$ C C完整的URL $,因为这将导致一些乱码,比如

<$p$p><$c$c>http%3A%2F%2F23.23.82.251%3A9116%2Fsolr%2Fdb%2Fselect%3Fq%3D*%3A*%2B_val_%3A%2522geodist%2528%2529%2522%26fq%3D(ProviderType%3A10)%26rows%3D100%26wt%3Djson%26indent%3Dtrue%26fl%3DProviderID%2CProviderType%2CProviderName%2CProviderAddress%2CCityAreaID%2CCityAreaName%2CCity%2CState%2CLatlong%2Cscore%26fq%3D{!geofilt}%26sfield%3DLatlong%26pt%3D20.296059%2C85.82454%26d%3D5%26sort%3Dgeodist()%2520asc

被用来作为URL。这将失败,因为它不能被解析为一个正常的HTTP URL。

相反,只要尝试使用新HttpPost(HTTP://23.23.82.251:9116 / ...);

Ulr:

http://23.23.82.251:9116/solr/db/select?q=*:*+_val_:%22geodist%28%29%22&fq=(ProviderType:10)&rows=100&wt=json&indent=true&fl=ProviderID,ProviderType,ProviderName,ProviderAddress,CityAreaID,CityAreaName,City,State,Latlong,score&fq={!geofilt}&sfield=Latlong&pt=20.296059,85.82454&d=5&sort=geodist()%20asc

When i am try to use this url in android with post request i am getting following error.

10-22 13:54:49.946: E/SearchResult(6659): Exception Name = java.lang.IllegalArgumentException

I was used URLEncoder.encode() but still i am getting same error. can any suggest where i am writing wrong. below code i am using

            HttpParams httpParameters = new BasicHttpParams();
            // Set the timeout in milliseconds until a connection is established.
            // The default value is zero, that means the timeout is not used. 
            int timeoutConnection = 10000;

            HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);

            // Set the default socket timeout (SO_TIMEOUT) 
            // in milliseconds which is the timeout for waiting for data.
            int timeoutSocket = 15000;
            HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);               
            httpclient = new DefaultHttpClient(httpParameters);             

            HttpPost httppost = new HttpPost(url here);
            JSONObject json = new JSONObject();
            // Execute HTTP Post Request
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity httpEntity = response.getEntity();
            InputStream instream = httpEntity.getContent();
            String result= Utilities.convertStreamToString(instream);

            json=new JSONObject(result);

解决方案

You should not encode the entire URL, since that will result in some gibberish like

http%3A%2F%2F23.23.82.251%3A9116%2Fsolr%2Fdb%2Fselect%3Fq%3D*%3A*%2B_val_%3A%2522geodist%2528%2529%2522%26fq%3D(ProviderType%3A10)%26rows%3D100%26wt%3Djson%26indent%3Dtrue%26fl%3DProviderID%2CProviderType%2CProviderName%2CProviderAddress%2CCityAreaID%2CCityAreaName%2CCity%2CState%2CLatlong%2Cscore%26fq%3D{!geofilt}%26sfield%3DLatlong%26pt%3D20.296059%2C85.82454%26d%3D5%26sort%3Dgeodist()%2520asc

being used as the URL. This will fail because it cannot be parsed as a normal HTTP URL.

Instead, just try using new HttpPost("http://23.23.82.251:9116/...");.

这篇关于java.lang.IllegalStateException:目标主机不能为空,或参数设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆