Android: IllegalStateException in HttpGet

橙三吉。 提交于 2019-12-17 10:04:20

问题


I'm trying to send a GET request using HttpClient, but I keep getting an IllegalStateException. Any idea what's causing this? I've been looking around for a solution, but I don't get what it means by "host=null" in the log. How do I set the host, and how is it different from the path? Here's my logcat out:

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)

and the source:

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

回答1:


I think you need to use .addHeader for HttpGet, for example to transfer data in Json format:

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

I also suggest you to use timeoutConnection (until a connection is established) and timeoutSocket (timeout for waiting for data.) for more information look at: Java jersey RESTful webservice requests

Also note that You need to implement your network connection on a separate thread for API level 11 or greater.




回答2:


you have to add the http:// to address, in order to get it work. Avoid the use of URLEncoder, if you do.




回答3:


When you do this:

HttpGet getData = new HttpGet(this.address);

what exactly is this.address? I'm assuming it is a String, if so it needs to look like this:

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

You've probably done this:

String address = "google.com";



回答4:


This is your Host error You are passing "www.host.com" instead you have to pass "http://www.w3schools.com"

reference post is here



来源:https://stackoverflow.com/questions/11526437/android-illegalstateexception-in-httpget

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!