安卓:IllegalStateException异常的HTTPGET [英] Android: IllegalStateException in HttpGet

查看:146
本文介绍了安卓:IllegalStateException异常的HTTPGET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用发送 GET 要求的HttpClient ,但我不断收到 IllegalStateException异常。任何想法是什么引起的?我一直在四处寻找一个解决方案,但我不明白是什么在日志中指主机= NULL。我如何设置主机,以及它是如何从路径有什么不同?这是我的logcat了:

 十一月7号至17日:54:18.002:W / System.err的(15422):java.lang.IllegalStateException:目标主机不能为空,或参数设置。计划= null,则主机= NULL,路径= google.com
十一月7号至17号:54:18.002:W / System.err的(15422):在org.apache.http.impl.client.DefaultRequestDirector.determineRoute(DefaultRequestDirector.java:591)
十一月7号至17号:54:18.002:W / System.err的(15422):在org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:293)
十一月7号至17号:54:18.002:W / System.err的(15422):在org.apache.http.impl.client.AbstractHttpClient $ 1.executeRequestSending(AbstractHttpClient.java:609)
十一月7号至17号:54:18.002:W / System.err的(15422):在org.apache.http.impl.client.naf.redirect.NafRequestExecutorWrapperRedirectionHandler.executeRequestSendingUsual(NafRequestExecutorWrapperRedirectionHandler.java:96)
十一月7号至17号:54:18.002:W / System.err的(15422):在org.apache.http.impl.client.naf.redirect.NafRequestExecutorWrapperRedirectionHandler.executeRequestSending(NafRequestExecutorWrapperRedirectionHandler.java:73)
十一月7号至17号:54:18.002:W / System.err的(15422):在org.apache.http.impl.client.naf.auth.NafHttpAuthStrategyDefault.sendFirstRequest(NafHttpAuthStrategyDefault.java:487)
十一月7号至17号:54:18.002:W / System.err的(15422):在org.apache.http.impl.client.naf.auth.NafHttpAuthStrategyDefault.performAuthExecutionUnsafe(NafHttpAuthStrategyDefault.java:388)
十一月7号至17号:54:18.002:W / System.err的(15422):在org.apache.http.impl.client.naf.auth.NafHttpAuthStrategyDefault.performAuthExecution(NafHttpAuthStrategyDefault.java:200)
十一月7号至17号:54:18.002:W / System.err的(15422):在org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:558)
十一月7号至17号:54:18.002:W / System.err的(15422):在org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:508)
十一月7号至17号:54:18.002:W / System.err的(15422):在org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:486)
十一月7号至17号:54:18.002:W / System.err的(15422):在myapp.htt prequest.free.GETActivity $ 1sendRequestTask.doInBackground(GETActivity.java:102)
十一月7号至17号:54:18.002:W / System.err的(15422):在myapp.htt prequest.free.GETActivity $ 1sendRequestTask.doInBackground(GETActivity.java:1)
十一月7号至17号:54:18.002:W / System.err的(15422):在android.os.AsyncTask $ 2.call(AsyncTask.java:264)
十一月7号至17号:54:18.002:W / System.err的(15422):在java.util.concurrent.FutureTask中$ Sync.innerRun(FutureTask.java:305)
十一月7号至17号:54:18.002:W / System.err的(15422):在java.util.concurrent.FutureTask.run(FutureTask.java:137)
十一月7号至17号:54:18.002:W / System.err的(15422):在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
十一月7号至17号:54:18.002:W / System.err的(15422):在java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:569)
十一月7号至17号:54:18.002:W / System.err的(15422):在java.lang.Thread.run(Thread.java:856)
 

和源:

  HttpClient的getClient =新DefaultHttpClient();
HTTPGET的getData =新HTTPGET(this.address);
尝试 {
    HTT presponse gameResponse = getClient.execute(的getData);
    返回EntityUtils.toString(gameResponse.getEntity());
}赶上(ClientProtocolException E){
    e.printStackTrace();
}赶上(IOException异常E){
    e.printStackTrace();
}赶上(IllegalStateException异常E){
    e.printStackTrace();
}
 

解决方案

我认为你需要使用.addHeader的HTTPGET,例如以JSON格式传输数据:

 公共类客户端{

    私人字符串服务器;

    公共客户端(服务器字符串){
        this.server =服务器;
    }

    私人字符串getBase(){
        返回服务器;
    }

    公共字符串getBaseURI(字符串str){
        字符串结果=;
        尝试 {
            DefaultHttpClient的HttpClient =新DefaultHttpClient();
            HTTPGET调用getRequest =新HTTPGET(getBase()+ STR);
            getRequest.addHeader(接受,应用/ JSON);
            HTT presponse响应= httpClient.execute(调用getRequest);
            结果=的getResult(响应)的ToString();
            。httpClient.getConnectionManager()关闭();
        }赶上(ClientProtocolException E){
            e.printStackTrace();
        }赶上(IOException异常E){
            e.printStackTrace();
        }
        返回结果;
    }

    私人的StringBuilder的getResult(Htt的presponse响应)抛出IllegalStateException异常,IOException异常{

        StringBuilder的结果=新的StringBuilder();
        的BufferedReader BR =新的BufferedReader(新的InputStreamReader((response.getEntity()的getContent())),1024。);
        字符串输出;
        而((输出= br.readLine())!= NULL)
            result.append(输出);
        返回结果;
    }
}
 

我也建议你使用timeoutConnection(直到建立连接),并timeoutSocket(超时等待数据。)了解更多信息看:<一href="http://stackoverflow.com/questions/11479746/java-jersey-restful-webservice-requests/11479884#11479884">Java球衣RESTful Web服务请求

另外请注意,您需要实现对API级别11或更高的一个单独的线程您的网络连接。

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

解决方案

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.

这篇关于安卓:IllegalStateException异常的HTTPGET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆