java.lang.IllegalStateException:目标主机不能为null,或者在参数中设置

时间:2012-10-22 08:12:56

标签: android

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

当我尝试在android中使用此url并发布请求时,我收到了以下错误。

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

我使用了URLEncoder.encode(),但我仍然得到同样的错误。可以建议我写错了。下面我正在使用的代码

            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);

3 个答案:

答案 0 :(得分:8)

你不应该对整个网址进行编码,因为这会导致像

这样的一些乱码
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

用作网址。这将失败,因为它无法解析为普通的HTTP URL。

相反,只需尝试使用new HttpPost("http://23.23.82.251:9116/...");

答案 1 :(得分:0)

请尝试这种方式..

HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://www.yoursite.com/script.php");

    try {
        // Add your data
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("id", "12345"));
        nameValuePairs.add(new BasicNameValuePair("stringdata", "test"));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);

    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
    } catch (IOException e) {
        // TODO Auto-generated catch block
    }

答案 2 :(得分:-3)

请试试这个..

URLEncoder.encode("<url here>", "UTF-8");