【问题标题】:android get request exception "java.lang.IllegalStateException: Target host must not be null, or set in parameters.android get request exception "java.lang.IllegalStateException: 目标主机不能为空,或在参数中设置。
【发布时间】:2014-12-11 18:12:35
【问题描述】:

android 获取请求异常“java.lang.IllegalStateException: 目标主机不能为空,或在参数中设置。scheme=null, host=null, path=http://stage1-go.university:80/user/profile{"userId":"0"}"

您好,我尝试发出 Get 请求,代码在这里:

public InputStream setGetRequest(String concreteUrl, String jsonAsString) throws IOException {
        String url = protocol + siteUrl + ":" + httpPort + "/" +  concreteUrl + jsonAsString;
        url = URLEncoder.encode(url, "UTF-8");

        HttpGet httpGet = new HttpGet(url);
        httpGet.setHeader("sessid", ClientAPI.getSessionId());

        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        HttpClient client = new DefaultHttpClient();

        String getServerString = client.execute(httpGet, responseHandler);
        return null;
    }

这里是我的日志:

12-11 17:59:50.731    2715-2733/com.podorojnik E/AndroidRuntime﹕ FATAL EXCEPTION: Thread-312
    Process: com.podorojnik, PID: 2715
    java.lang.IllegalStateException: Target host must not be null, or set in parameters. scheme=null, host=null, path=http://stage1-go.university:80/user/profile{"userId":"0"}
            at org.apache.http.impl.client.DefaultRequestDirector.determineRoute(DefaultRequestDirector.java:591)
            at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:293)
            at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
            at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:653)
            at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:627)
            at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:616)
            at com.zeoalliance.internship.pilotproject.server.NetworkRequest.setGetRequest(NetworkRequest.java:100)
            at com.zeoalliance.internship.pilotproject.server.NetworkRequest.getInputStream(NetworkRequest.java:60)
            at com.zeoalliance.internship.pilotproject.server.NetworkRequest.execute(NetworkRequest.java:45)
            at com.zeoalliance.internship.pilotproject.server.ClientAPI.getUserProfile(ClientAPI.java:156)
            at com.zeoalliance.internship.pilotproject.activities.MainActivity$1.run(MainActivity.java:58)
            at java.lang.Thread.run(Thread.java:818)

我还在清单中添加了一个特殊权限,但对我没有帮助

<uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

有人可以帮忙吗?

【问题讨论】:

标签: android get request


【解决方案1】:

你直接返回空值;

在您的 setGetRequest 方法中输入以下代码:

String getServerString = client.execute(httpGet, responseHandler);
        return getServerString ;

代替:

String getServerString = client.execute(httpGet, responseHandler);
        return null;

享受!!!

【讨论】:

    【解决方案2】:

    您的路径设置为:

    path=http://stage1-go.university:80/
    

    stage1-go.university 不是主机,甚至不是有效的 URL。需要像 .com、.net 或类似的顶级域。如果这可行,您应该可以在任何浏览器中访问http://stage1-go.university,但这不起作用 - 我刚刚尝试过

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-08-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多