2

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 (timeout code based on this answer by kuester2000)

            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);
Ryan M
  • 18,333
  • 31
  • 67
  • 74
Srikanth
  • 584
  • 1
  • 6
  • 21

2 Answers2

8

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/...");.

nneonneo
  • 171,345
  • 36
  • 312
  • 383
  • If i use directly with out encoding i am getting following exception Exception Name = java.lang.IllegalArgumentException. Unfortunately it working in samsung S3 but not in emulator and other devices – Srikanth Oct 22 '12 at 08:25
  • It's different! First case you get IllegalStateException, second case you get IllegalArgumentException. Post the new exception... – nneonneo Oct 22 '12 at 08:28
  • S both case i am getting different Exception – Srikanth Oct 22 '12 at 08:31
  • Like I said, *please* update your question with the new exception. – nneonneo Oct 22 '12 at 08:44
  • Nothing much i removed the URLEncoder.encode(url here) then i used directly HttpPost httppost = new HttpPost(url here); that time i am getting java.lang.IllegalArgumentException. – Srikanth Oct 22 '12 at 09:03
  • So, you should *update your question*! And please show the exact URL you are trying to pass in... – nneonneo Oct 22 '12 at 09:04
-3

Please try this..

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