【问题标题】:Android - JSON cleartext parsing from URL returning null but connection is okayAndroid - 从返回 null 但连接正常的 URL 解析 JSON 明文
【发布时间】:2020-04-11 20:57:38
【问题描述】:

我正在从 URL 解析数据。我正在使用下面的代码在 logcat 中获取明文数据。但是,虽然没有问题,但它只显示空值。

class RetrieveFeedTask extends AsyncTask<String, Void, String> {

        @Override
        protected String doInBackground(String... arg0) {
            int responseCode = -1;
            //String responseData;
            try {
                URL retailerUrl = new URL("http://clients.appatlantis.com/soldy/wp-json/wp/v2/soldy-clients");
                HttpURLConnection connection = (HttpURLConnection) retailerUrl.openConnection();
                connection.connect();

                responseCode = connection.getResponseCode();


                if (responseCode == HttpURLConnection.HTTP_OK) {
                    InputStream inputStream = connection.getInputStream();

                    Reader reader = new InputStreamReader(inputStream);
                    int contentLength = connection.getContentLength();
                    char[] charArray = new char[contentLength];
                    reader.read(charArray);
                    String responseData = new String(charArray);
                    data = responseData;
                    Log.v(TAG, "Parse Data "+data);
                } else {
                    Log.e(TAG, "Parse Data Failed");
                }

            } catch (MalformedURLException e) {
                Log.e(TAG, "URL Exeption: ", e);
            } catch (IOException e) {
                Log.e(TAG, "IO URL Exeption: ", e);
            } catch (Exception e) {
                Log.e(TAG, "Exeption Caught: ", e);
            }

            return "Response Code "+data;
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);

            Log.v(TAG, "Parse Data "+data);

        }
    }

这是完整的回溯。请注意,错误被抛出为 NegetiveArraySizeException ,因为没有结果。

2020-04-12 02:41:03.853 7262-7373/com.appatlantis.jsonsample D/NetworkSecurityConfig: No Network Security Config specified, using platform default
2020-04-12 02:41:03.867 7262-7373/com.appatlantis.jsonsample W/System: ClassLoader referenced unknown path: system/framework/mediatek-cta.jar
2020-04-12 02:41:03.870 7262-7373/com.appatlantis.jsonsample I/System.out: e:java.lang.ClassNotFoundException: com.mediatek.cta.CtaHttp
2020-04-12 02:41:03.881 7262-7373/com.appatlantis.jsonsample W/System: ClassLoader referenced unknown path: system/framework/mediatek-cta.jar
2020-04-12 02:41:03.884 7262-7373/com.appatlantis.jsonsample I/System.out: e:java.lang.ClassNotFoundException: com.mediatek.cta.CtaHttp
2020-04-12 02:41:03.945 7262-7373/com.appatlantis.jsonsample I/System.out: [OkHttp] sendRequest>>
2020-04-12 02:41:03.947 7262-7373/com.appatlantis.jsonsample I/System.out: [OkHttp] sendRequest<<
2020-04-12 02:41:04.402 7262-7373/com.appatlantis.jsonsample E/MainActivity: Exeption Caught: 
    java.lang.NegativeArraySizeException: -1
        at com.appatlantis.jsonsample.MainActivity$RetrieveFeedTask.doInBackground(MainActivity.java:85)
        at com.appatlantis.jsonsample.MainActivity$RetrieveFeedTask.doInBackground(MainActivity.java:58)
        at android.os.AsyncTask$2.call(AsyncTask.java:333)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
        at java.lang.Thread.run(Thread.java:764)
2020-04-12 02:41:04.402 7262-7262/com.appatlantis.jsonsample V/MainActivity: Parse Data null

【问题讨论】:

    标签: java android android-json


    【解决方案1】:

    这是因为 connection.getContentLength() 返回 -1。发生这种情况是因为内容长度未知。或者更具体地说,服务器没有在响应消息中设置“Content-Length”标头。

    你可以做的是

                    InputStream inputStream = connection.getInputStream();
                    Scanner s = new Scanner(inputStream).useDelimiter("\\A");
                    data = s.hasNext() ? s.next() : "";
                    Log.v(TAG, "Parse Data " + data);
    

    这会起作用。

    【讨论】:

      【解决方案2】:

      试试这个解决方案

      将以下属性添加到AndroidManifest.xml 中的&lt;application 标记:

      android:usesCleartextTraffic="true"
      

      如果需要更多详细信息please see this answer

      【讨论】:

        猜你喜欢
        • 2023-03-08
        • 1970-01-01
        • 2013-10-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-07-16
        相关资源
        最近更新 更多