目标不得为空

时间:2014-04-02 06:25:54

标签: android get androidhttpclient

Java代码:

try {
            // defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();
             HttpGet httpGet = new HttpGet(url);
            HttpResponse httpResponse =httpClient.execute(httpGet);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

url就像:

http://122.180.133.121:84/diogo/api/api.php?class=authenticate&method=login_check&param={'userpassword':'777','username':'www'}

和logcat错误是:

java.lang.IllegalStateException: Target host must not be null, or set in parameters. scheme=null, host=null, path=http://122.170.103.168:86/diogo/api/api.php?class=authenticate&method=login_check&param={'userpassword':'123','username':'krunal'}

我已经搜索了很多解决方案,但所有人都在谈论http或www ..但我不知道实际问题是什么。只是因为我的服务器IP地址或什么。?

2 个答案:

答案 0 :(得分:1)

更改网址编码方法。试试这个方法。它必须是网址编码问题 -

  

stringByAddingPercentEscapesUsingEncoding(urlString);

public static String stringByAddingPercentEscapesUsingEncoding(String input) {
    try {
        return stringByAddingPercentEscapesUsingEncoding(input, "UTF-8");
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(
                "Java platforms are required to support UTF-8");
        // will never happen
    }
}

public static String stringByAddingPercentEscapesUsingEncoding(
        String input, String charset) throws UnsupportedEncodingException {
    byte[] bytes = input.getBytes(charset);
    StringBuilder sb = new StringBuilder(bytes.length);
    for (int i = 0; i < bytes.length; ++i) {
        int cp = bytes[i] < 0 ? bytes[i] + 256 : bytes[i];
        if (cp <= 0x20
                || cp >= 0x7F
                || (cp == 0x22 || cp == 0x25 || cp == 0x3C || cp == 0x3E
                        || cp == 0x20 || cp == 0x5B || cp == 0x5C
                        || cp == 0x5D || cp == 0x5E || cp == 0x60
                        || cp == 0x7b || cp == 0x7c || cp == 0x7d)) {
            sb.append(String.format("%%%02X", cp));
        } else {
            sb.append((char) cp);
        }
    }
    return sb.toString();
}

答案 1 :(得分:0)

URIUtils.extractHost(final URI uri)无法获取目标主机,因为param首先包含位于索引88 {的非法字符。

如果使用默认的Java URLEncoder对完整的URL进行编码,则生成的URL为

http%3A%2F%2F122.180.133.121%3A84%2Fdiogo%2Fapi%2Fapi.php%3Fclass%3Dauthenticate%26method%3Dlogin_check%26param%3D%7B%27userpassword%27%3A%27777%27%2C%27username%27%3A%27www%27%7D

但是你不能使用那个URL,因为de default URI Java类无法解析它并获得方案,主机......等等。

解决方案,仅解析非法内容并传递给您的网址参数

String param = URLEncoder.encode("{'userpassword':'777','username':'www'}",
"UTF-8");

HttpGet request = new HttpGet("http://122.180.133.121:84/diogo/api/api.php?
class=authenticate&method=login_check&param=" + param);