我正在尝试使用GET
发送HttpClient
请求,但是我一直收到IllegalStateException
。知道是什么原因造成的吗?我一直在寻找解决方案,但是日志中的"host=null"
并没有得到它的含义。如何设置主机,它与路径有何不同?这是我的日志:
07-17 11:54:18.002: W/System.err(15422): java.lang.IllegalStateException: Target host must not be null, or set in parameters. scheme=null, host=null, path=google.com
07-17 11:54:18.002: W/System.err(15422): at org.apache.http.impl.client.DefaultRequestDirector.determineRoute(DefaultRequestDirector.java:591)
07-17 11:54:18.002: W/System.err(15422): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:293)
07-17 11:54:18.002: W/System.err(15422): at org.apache.http.impl.client.AbstractHttpClient$1.executeRequestSending(AbstractHttpClient.java:609)
07-17 11:54:18.002: W/System.err(15422): at org.apache.http.impl.client.naf.redirect.NafRequestExecutorWrapperRedirectionHandler.executeRequestSendingUsual(NafRequestExecutorWrapperRedirectionHandler.java:96)
07-17 11:54:18.002: W/System.err(15422): at org.apache.http.impl.client.naf.redirect.NafRequestExecutorWrapperRedirectionHandler.executeRequestSending(NafRequestExecutorWrapperRedirectionHandler.java:73)
07-17 11:54:18.002: W/System.err(15422): at org.apache.http.impl.client.naf.auth.NafHttpAuthStrategyDefault.sendFirstRequest(NafHttpAuthStrategyDefault.java:487)
07-17 11:54:18.002: W/System.err(15422): at org.apache.http.impl.client.naf.auth.NafHttpAuthStrategyDefault.performAuthExecutionUnsafe(NafHttpAuthStrategyDefault.java:388)
07-17 11:54:18.002: W/System.err(15422): at org.apache.http.impl.client.naf.auth.NafHttpAuthStrategyDefault.performAuthExecution(NafHttpAuthStrategyDefault.java:200)
07-17 11:54:18.002: W/System.err(15422): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:558)
07-17 11:54:18.002: W/System.err(15422): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:508)
07-17 11:54:18.002: W/System.err(15422): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:486)
07-17 11:54:18.002: W/System.err(15422): at myapp.httprequest.free.GETActivity$1sendRequestTask.doInBackground(GETActivity.java:102)
07-17 11:54:18.002: W/System.err(15422): at myapp.httprequest.free.GETActivity$1sendRequestTask.doInBackground(GETActivity.java:1)
07-17 11:54:18.002: W/System.err(15422): at android.os.AsyncTask$2.call(AsyncTask.java:264)
07-17 11:54:18.002: W/System.err(15422): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
07-17 11:54:18.002: W/System.err(15422): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
07-17 11:54:18.002: W/System.err(15422): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
07-17 11:54:18.002: W/System.err(15422): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
07-17 11:54:18.002: W/System.err(15422): at java.lang.Thread.run(Thread.java:856)
以及来源:
HttpClient getClient = new DefaultHttpClient();
HttpGet getData = new HttpGet(this.address);
try {
HttpResponse gameResponse = getClient.execute(getData);
return EntityUtils.toString(gameResponse.getEntity());
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
}
最佳答案
我认为您需要对HttpGet使用.addHeader,例如以Json格式传输数据:
public class Client {
private String server;
public Client(String server) {
this.server = server;
}
private String getBase() {
return server;
}
public String getBaseURI(String str) {
String result = "";
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet getRequest = new HttpGet(getBase() + str);
getRequest.addHeader("accept", "application/json");
HttpResponse response = httpClient.execute(getRequest);
result = getResult(response).toString();
httpClient.getConnectionManager().shutdown();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
private StringBuilder getResult(HttpResponse response) throws IllegalStateException, IOException {
StringBuilder result = new StringBuilder();
BufferedReader br = new BufferedReader(new InputStreamReader((response.getEntity().getContent())), 1024);
String output;
while ((output = br.readLine()) != null)
result.append(output);
return result;
}
}
我还建议您使用timeoutConnection(直到建立连接)和timeoutSocket(等待数据超时)。有关更多信息,请参见:Java jersey RESTful webservice requests
另请注意,您需要在API级别11或更高版本的单独线程上实现网络连接。
关于android - Android:HttpGet中的IllegalStateException,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11526437/