Android – 使用InputStream获取数据,但返回为空/空数据

huangapple go评论73阅读模式
英文:

Android - Get Data using InputStream returning nothing/empty

问题

以下是翻译好的内容:

我正在解析以以下内容开头的 JSON 数据...

[
   {"title":"dummy", "content":"ajsfhk"},
   {"title":"dummy", "content":"ajsfhk"},
   {"title":"dummy", "content":"ajsfhk"},
]

而且我的 Android 异步任务类如下。但是它显示了 "NegativeArraySizeException" 错误。

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

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

            responseCode = connection.getResponseCode();

            if (responseCode == HttpsURLConnection.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);
                Log.e(TAG, "解析数据 " + responseData);
            } else {
                Log.e(TAG, "解析数据失败");
            }

        } catch (MalformedURLException e) {
            Log.e(TAG, "URL 异常:", e);
        } catch (IOException e) {
            Log.e(TAG, "IO URL 异常:", e);
        } catch (Exception e) {
            Log.e(TAG, "捕获异常:", e);
        }

        return "响应代码 " + responseCode;
    }
}

这里是异常跟踪信息

2020-04-10 15:12:31.322 27385-27449/com.appatlantis.soldy E/MainActivity: 捕获异常:
    java.lang.NegativeArraySizeException: -1
        at com.appatlantis.soldy.MainActivity$RetrieveFeedTask.doInBackground(MainActivity.java:149)
        at com.appatlantis.soldy.MainActivity$RetrieveFeedTask.doInBackground(MainActivity.java:132)
        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)

我不太理解 JSON。

英文:

I am parsing data of JSON that starts something like this below...

[
   {&quot;title&quot;:&quot;dummy&quot;, &quot;content&quot;:&quot;ajsfhk&quot;},
   {&quot;title&quot;:&quot;dummy&quot;, &quot;content&quot;:&quot;ajsfhk&quot;},
   {&quot;title&quot;:&quot;dummy&quot;, &quot;content&quot;:&quot;ajsfhk&quot;},
]

And my android async task class is below. But it shows that "NegativeArraySizeException"

class RetrieveFeedTask extends AsyncTask&lt;Object, Void, String&gt; {

    @Override
    protected String doInBackground(Object... arg0) {
        int responseCode = -1;
        try {
            URL retailerUrl = new URL(&quot;https://clients.appatlantis.com/soldy/wp-json/wp/v2/soldy-clients?_embed&quot;);
            HttpsURLConnection connection = (HttpsURLConnection) retailerUrl.openConnection();
            connection.connect();

            responseCode = connection.getResponseCode();

            if (responseCode == HttpsURLConnection.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);
                Log.e(TAG, &quot;Parse Data &quot;+responseData);
            } else {
                Log.e(TAG, &quot;Parse Data Failed&quot;);
            }

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

        return &quot;Response Code &quot;+responseCode;
    }
}

And here is the exception trace

2020-04-10 15:12:31.322 27385-27449/com.appatlantis.soldy E/MainActivity: Exeption Caught: 
    java.lang.NegativeArraySizeException: -1
        at com.appatlantis.soldy.MainActivity$RetrieveFeedTask.doInBackground(MainActivity.java:149)
        at com.appatlantis.soldy.MainActivity$RetrieveFeedTask.doInBackground(MainActivity.java:132)
        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)

I don't understand much about JSON.

答案1

得分: 1

contentLength-1。 注意错误:

    java.lang.NegativeArraySizeException: -1
        at com.appatlantis.soldy.MainActivity$RetrieveFeedTask.doInBackground(MainActivity.java:149)

这告诉你,在第149行尝试使用大小为 -1 初始化数组,基于崩溃情况,我假设代码如下:

char[] charArray = new char[contentLength];

根据 getContentLength() 的文档,这意味着内容长度未知。你应该考虑直接从 InputStream 解析 JSON - 这会避免你自己尝试处理这个问题。

英文:

contentLength is -1. Note the error:

    java.lang.NegativeArraySizeException: -1
        at com.appatlantis.soldy.MainActivity$RetrieveFeedTask.doInBackground(MainActivity.java:149)

This tells you that you tried to initialize an array with a size of -1 on line 149, which I'm assuming based on the crash is

char[] charArray = new char[contentLength];

From the docs for getContentLength(), this means that the content length isn't known. You should look into parsing the JSON directly from an InputStream - it'll save you the trouble of trying to do this yourself.

huangapple
  • 本文由 发表于 2020年4月10日 18:05:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/61137992.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定