1

我正在尝试使用 发送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();
}
4

4 回答 4

5

您必须添加http://到地址才能使其正常工作。如果您这样做,请避免使用URLEncoder 。

于 2012-07-17T16:10:54.813 回答
2

当你这样做时:

HttpGet getData = new HttpGet(this.address);

究竟是this.address什么?我假设它是一个String,如果是这样,它需要看起来像这样:

String address = "http://www.google.com/path/to/document";

你可能已经这样做了:

String address = "google.com";
于 2012-07-17T16:09:31.553 回答
1

我认为您需要将 .addHeader 用于 HttpGet,例如以 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 或更高级别的单独线程上实现网络连接。

于 2012-07-17T16:18:31.407 回答
1

这是您的主机错误您正在传递“www.host.com”而不是您必须通过“ http://www.w3schools.com

参考帖在这里

于 2014-11-07T05:56:43.393 回答