英文:
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...
[
   {"title":"dummy", "content":"ajsfhk"},
   {"title":"dummy", "content":"ajsfhk"},
   {"title":"dummy", "content":"ajsfhk"},
]
And my android async task class is below. But it shows that "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, "Parse Data "+responseData);
            } 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 "+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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论