android get request exception" java.lang.IllegalStateException:目标主机不能为null,或者在参数中设置。

时间:2014-12-11 18:12:35

标签: android get request

android get request exception" java.lang.IllegalStateException:目标主机不能为null,或者在参数中设置。 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" />

有人可以帮忙吗?

2 个答案:

答案 0 :(得分:0)

你直接返回空值;

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

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

而不是:

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

享受!!!

答案 1 :(得分:0)

您将路径设置为:

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

stage1-go.university不是主持人,甚至不是有效的网址。需要一个顶级域名,如.com,.net或类似的东西。如果这样可行,您应该可以在任何浏览器中转到http://stage1-go.university,但这不会起作用 - 我只是尝试过它