Android java.lang.IllegalStateException:目标主机不能为null,也不能在参数中设置
Ulr: 当我尝试在android中使用此url和post请求时,我遇到以下错误Android java.lang.IllegalStateException:目标主机不能为null,也不能在参数中设置,android,Android,Ulr: 当我尝试在android中使用此url和post请求时,我遇到以下错误 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,
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
我使用了URLEncoder.encode(),但仍然会遇到相同的错误。你能告诉我哪里写错了吗。下面是我正在使用的代码
10-22 13:54:49.946: E/SearchResult(6659): Exception Name = java.lang.IllegalArgumentException
请试试这个
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);
URLEncoder.encode(“,“UTF-8”);
您不应该对整个URL进行编码,因为这会导致类似于
URLEncoder.encode("<url here>", "UTF-8");
正在用作URL。这将失败,因为无法将其解析为正常的HTTP URL
相反,只需尝试使用newhttppost(“http://23.23.82.251:9116/...");代码>请用这种方法试试
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
HttpClient-HttpClient=newdefaulthttpclient();
HttpPost HttpPost=新的HttpPost(“http://www.yoursite.com/script.php");
试一试{
//添加您的数据
List nameValuePairs=新的ArrayList(2);
添加(新的BasicNameValuePair(“id”,“12345”);
添加(新的BasicNameValuePair(“stringdata”、“test”);
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
//执行HTTP Post请求
HttpResponse response=httpclient.execute(httppost);
}捕获(客户端协议例外e){
//TODO自动生成的捕捉块
}捕获(IOE异常){
//TODO自动生成的捕捉块
}
如果直接使用out编码,则会得到以下异常名称=java.lang.IllegalArgumentException。不幸的是,它在三星S3中工作,但在emulator和其他设备中不工作,这是不同的!第一个案例是IllegalStateException,第二个案例是IllegalArgumentException。发布新的异常…这两种情况下我得到了不同的异常就像我说的,请用新的异常更新你的问题。没有什么我删除了URLEncoder.encode(url在这里),然后我直接使用了HttpPost HttpPost=new HttpPost(url在这里);那一次我得到了java.lang.IllegalArgumentException。至少如果你想从网上窃取代码片段。@nneonneo->对不起。为了便于理解,我只是将示例代码粘贴到这里。。仅此而已……如果您从web上获取示例代码,您应该通过提供指向原始.str.replace(“,“%20”)的链接来引用它;这个替换声明为我解决了这个问题。
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
}