【问题标题】:Target must not be null目标不能为空
【发布时间】:2014-04-02 06:25:54
【问题描述】:

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

网址是这样的:

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地址还是什么。?

【问题讨论】:

  • 确保其网址有效
  • 我已经检查过了..它有效。
  • 尝试对 url 进行编码。一个疯狂的猜测
  • 检查参数名称或对您的 URL 进行编码。使用 URL.encode()
  • @Raghunandan 我已经通过了编码的网址

标签: android get androidhttpclient


【解决方案1】:

更改 url 编码方式。试试这个方法。一定是url编码问题-

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

【讨论】:

  • 可能是..但在我的情况下,每次它都正确编码:/
【解决方案2】:

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,因为默认的 URI Java 类无法解析它并获取方案、主机...等

解决办法,只解析非法内容并传递给你的URL参数

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-29
    • 2020-09-22
    • 1970-01-01
    相关资源
    最近更新 更多