【问题标题】:url returns true json but android HttpURLConnection return falseurl 返回 true json 但 android HttpURLConnection 返回 false
【发布时间】:2018-06-30 07:22:15
【问题描述】:

我有一个 Https url,它返回一个像这样的 json:

{success:true, success_msg:'Register/Unregister was accomplished successfully',desc:'',redirect:false}

当我想在 android 中使用 HttpURLConnection 获取 url 响应时,它会返回这个 json:

{success:false, title:'Traffic Authentication Page',fail_msg:'Register/Unregister was failed',desc:''}

这里是 http utils 类。我使用了 from doGet(String url) 方法

public class HttpUtil {
    static {
        try {
            TrustManager[] trustAllCerts = { new X509TrustManager() {
                public X509Certificate[] getAcceptedIssuers() {

                    return null;
                }

                public void checkClientTrusted(X509Certificate[] certs,
                        String authType) {
                }

                public void checkServerTrusted(X509Certificate[] certs,
                        String authType) {
                }
            } };
            SSLContext sc = SSLContext.getInstance("SSL");

            HostnameVerifier hv = new HostnameVerifier() {
                public boolean verify(String arg0, SSLSession arg1) {
                    return true;
                }
            };
            sc.init(null, trustAllCerts, new SecureRandom());

            HttpsURLConnection
                    .setDefaultSSLSocketFactory(sc.getSocketFactory());
            HttpsURLConnection.setDefaultHostnameVerifier(hv);
        } catch (Exception localException) {
        }
    }

    private static HttpUtil httpUtil;

    HttpUtil() {
    }

    public static HttpUtil getHttpUtil() {
        if (httpUtil == null)
            httpUtil = new HttpUtil();

        return httpUtil;
    }

    public String doGet(String url) throws Exception {

        // configure the SSLContext with a TrustManager

        URL urlObj = new URL(url);
        HttpURLConnection conn = (HttpURLConnection) urlObj.openConnection();
        conn.setDoOutput(true);

        String line;
        StringBuffer buffer = new StringBuffer();
        BufferedReader reader = null;
        try
        {
            reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            while ((line = reader.readLine()) != null) {
                buffer.append(line);
            }
        } catch(Exception localException) {

        }
        reader.close();
        conn.disconnect();

        return buffer.toString();
    }
}

当我记录 url 时,它似乎是正确的。我通过从用户获得的参数在 asynctask doInBackground 中生成 url。此 url 返回 true json,但通过 android 发送此请求返回 false json。为什么它返回假?请帮帮我。

【问题讨论】:

  • 该消息清楚地表明有一些失败可能是身份验证失败。仔细检查 API,看看是否缺少任何标头信息。

标签: android


【解决方案1】:

Json中的真假值由0和1设置 并使用Boolean.valueOf("1") => return true 将其转换为实际值

【讨论】:

    【解决方案2】:

    我的解决方案解决了。我只是删除了conn.setDoOutput(true);,因为在 GET 方法中没有输出流。

    这个问题帮助了我:What is the use of HttpURLConnection class's setDoOutput & setDoInput methods

    【讨论】:

      猜你喜欢
      • 2012-06-29
      • 2013-02-26
      • 1970-01-01
      • 2014-10-21
      • 2013-02-23
      • 2020-02-12
      • 1970-01-01
      • 2011-03-02
      • 2016-04-16
      相关资源
      最近更新 更多