【问题标题】:Problems to receive JSON from https server on Android在 Android 上从 https 服务器接收 JSON 的问题
【发布时间】:2011-06-19 10:08:57
【问题描述】:

这是我的问题/问题:

我正在向通过 https 通过 SSL 进行身份验证的服务器发出 URL 请求,但是当我将输入转换为字符串时,得到答案时总是返回空。 另一个不带参数的调用总是运行顺利

有什么想法吗?

网址:https://www.server.com/api/getResources.php?res[]=Argentina&res[]=Australia&res[]=Bolivia

还有代码:

HttpURLConnection http = conectar(URL);
    InputStream urlInputStream = null;
    try {
        urlInputStream = http.getInputStream();
    } catch (IOException e) {

    }

连接法:

private HttpURLConnection conectar(String surl) {
    URL url = null;
    try {
        url = new URL(surl);
    } catch (MalformedURLException e) {

    }

    HttpURLConnection http = null;

    if (url.getProtocol().toLowerCase().equals("https")) {
        trustEveryone();
        HttpsURLConnection https = null;
        try {
            https = (HttpsURLConnection) url.openConnection();
        } catch (IOException e) {

        }
        // https.setHostnameVerifier(DO_NOT_VERIFY);
        http = https;
    } else {
        try {
            http = (HttpURLConnection) url.openConnection();
        } catch (IOException e) {

        }
    }
    Authenticator.setDefault(new MyAuthenticator());
    return http;
}

trustEveryone 方法:

HttpsURLConnection
                .setDefaultHostnameVerifier(new HostnameVerifier() {
                    public boolean verify(String hostname,
                            SSLSession session) {
                        return true;
                    }
                });
        SSLContext context = SSLContext.getInstance("TLS"); // TLS
        context.init(null, new X509TrustManager[] { new X509TrustManager() {
            public void checkClientTrusted(X509Certificate[] chain,
                    String authType) throws CertificateException {
            }

            public void checkServerTrusted(X509Certificate[] chain,
                    String authType) throws CertificateException {
            }

            public X509Certificate[] getAcceptedIssuers() {
                return new X509Certificate[0];
            }
        } }, new SecureRandom());
        HttpsURLConnection.setDefaultSSLSocketFactory(context
                .getSocketFactory());

【问题讨论】:

  • 另一个没有参数的调用总是运行顺利
  • 这个问题很奇怪,因为我明白了,如果我调用其他文件,包括参数调用没有给我任何问题

标签: android json ssl https


【解决方案1】:

您发布的 URL 字符串是否正确?如果是这样,您需要在将方括号传递给URL 构造函数之前对其进行编码。 documentation for URL 声明:

URL 类本身不会根据 RFC2396 中定义的转义机制对任何 URL 组件进行编码或解码。调用者有责任对在调用 URL 之前需要转义的任何字段进行编码,并对从 URL 返回的任何转义字段进行解码

编辑
RFC2396 声明:

必须对与排除字符对应的数据进行转义,以便在 URI 中正确表示。

方括号算作排除字符。

因此,您需要在将参数字符串包含在 URL 中之前对其进行编码。您不想对整个 URL 进行编码,否则它将对诸如 : 之类的字符进行编码。这是一个代码sn-p:

String parameters = "res[]=Argentina&res[]=Australia&res[]=Bolivia";
String encodedParameters = URLEncoder.encode(parameters , "UTF-8");

然后您可以将其附加到您的主机和路径并将其传递给您的 URL 构造函数。

【讨论】:

  • 是的,网址是正确的。你能举个例子吗?非常感谢
  • 好的,谢谢你的例子,我已经测试过了,应该可以工作,但不行。我尝试的另一件事是使用 http(不是 https)进行查询,它可以正常工作。有什么想法吗?
  • @Jorge Garrido 你能控制服务器代码吗?如果是这样,我将从一个简单的参数test=value 开始。看看这是否可行,如果可行,建立更复杂的。
  • 是的,我用参数和不带参数都试过了,什么都没有,我一直得到 0
  • @Jorge Garrido 我很困惑,正如您之前所说,没有参数的调用“运行顺利”。您能否编辑您的问题以准确列出哪些 URL 有效,哪些失败?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多