我正在尝试从服务器获取数据并将包含 2D 数组的一行转换为列表,但执行此操作时出现错误。
这是我的日志猫监视器:
08-16 10:48:50.146 4914-4914/? I/zygote: Not late-enabling -Xcheck:jni
(already on)
08-16 10:48:50.261 4914-4914/? W/zygote: Unexpected CPU variant for X86
using defaults: x86
08-16 10:48:51.295 4914-4914/com.example.alexlevine.oceanapp I/InstantRun:
starting instant run server: is main process
08-16 10:48:52.253 4914-4914/com.example.alexlevine.oceanapp I/zzbx: Making
Creator dynamically
08-16 10:48:52.354 4914-4914/com.example.alexlevine.oceanapp W/zygote:
Skipping duplicate class check due to unrecognized classloader
08-16 10:48:52.390 4914-4914/com.example.alexlevine.oceanapp
I/Google Maps Android API: Google Play services client version: 11020000
08-16 10:48:52.395 4914-4914/com.example.alexlevine.oceanapp
I/Google Maps Android API: Google Play services package version: 11302470
08-16 10:48:52.415 4914-4921/com.example.alexlevine.oceanapp I/zygote: Do
partial code cache collection, code=30KB, data=13KB
08-16 10:48:52.415 4914-4921/com.example.alexlevine.oceanapp I/zygote: After
code cache collection, code=26KB, data=13KB
08-16 10:48:52.415 4914-4921/com.example.alexlevine.oceanapp I/zygote:
Increasing code cache capacity to 128KB
08-16 10:48:52.685 4914-5006/com.example.alexlevine.oceanapp W/afz:
copyMemory is missing from platform - proto runtime falling back to safer
methods.
08-16 10:48:52.718 4914-4990/com.example.alexlevine.oceanapp
D/NetworkSecurityConfig: No Network Security Config specified, using
platform default
08-16 10:48:52.869 4914-5022/com.example.alexlevine.oceanapp
D/OpenGLRenderer: HWUI GL Pipeline
08-16 10:48:52.989 4914-5022/com.example.alexlevine.oceanapp
I/OpenGLRenderer: Initialized EGL, version 1.4
08-16 10:48:52.989 4914-5022/com.example.alexlevine.oceanapp
D/OpenGLRenderer: Swap behavior 1
08-16 10:48:52.989 4914-5022/com.example.alexlevine.oceanapp
W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED,
retrying without...
08-16 10:48:52.990 4914-5022/com.example.alexlevine.oceanapp
D/OpenGLRenderer: Swap behavior 0
08-16 10:48:53.025 4914-5022/com.example.alexlevine.oceanapp
D/EGL_emulation: eglCreateContext: 0xa3f06500: maj 2 min 0 rcv 2
08-16 10:48:53.030 4914-5022/com.example.alexlevine.oceanapp
D/EGL_emulation: eglMakeCurrent: 0xa3f06500: ver 2 0 (tinfo 0xa3f035b0)
08-16 10:48:53.127 4914-5022/com.example.alexlevine.oceanapp
W/android.hardware.graphics.mapper@2.0::Mapper: getService: found null
hwbinder interface
08-16 10:48:53.129 4914-5022/com.example.alexlevine.oceanapp I/vndksupport:
sphal namespace is not configured for this process. Loading
/system/lib/hw/gralloc.ranchu.so from the current namespace instead.
08-16 10:48:53.239 4914-4921/com.example.alexlevine.oceanapp I/zygote: Do
partial code cache collection, code=60KB, data=49KB
08-16 10:48:53.239 4914-4921/com.example.alexlevine.oceanapp I/zygote: After
code cache collection, code=60KB, data=49KB
08-16 10:48:53.239 4914-4921/com.example.alexlevine.oceanapp I/zygote:
Increasing code cache capacity to 256KB
08-16 10:48:53.240 4914-4921/com.example.alexlevine.oceanapp I/zygote: Do
full code cache collection, code=60KB, data=66KB
08-16 10:48:53.241 4914-4921/com.example.alexlevine.oceanapp I/zygote: After
code cache collection, code=0B, data=22KB
08-16 10:48:53.441 4914-5022/com.example.alexlevine.oceanapp
D/EGL_emulation: eglMakeCurrent: 0xa3f06500: ver 2 0 (tinfo 0xa3f035b0)
08-16 10:48:53.456 4914-5016/com.example.alexlevine.oceanapp
D/EGL_emulation: eglCreateContext: 0xa13f85a0: maj 1 min 0 rcv 1
08-16 10:48:53.502 4914-5016/com.example.alexlevine.oceanapp
D/EGL_emulation: eglMakeCurrent: 0xa13f85a0: ver 1 0 (tinfo 0xa26647f0)
08-16 10:48:53.568 4914-4923/com.example.alexlevine.oceanapp I/zygote:
Waiting for a blocking GC ObjectsAllocated
08-16 10:48:53.574 4914-4923/com.example.alexlevine.oceanapp I/zygote:
WaitForGcToComplete blocked for 6.769ms for cause ObjectsAllocated
08-16 10:48:53.591 4914-5022/com.example.alexlevine.oceanapp
D/EGL_emulation: eglMakeCurrent: 0xa3f06500: ver 2 0 (tinfo 0xa3f035b0)
08-16 10:48:53.612 4914-5022/com.example.alexlevine.oceanapp
D/RenderScript HIDL Adaptation: IRenderScriptDevice::getService()
08-16 10:48:53.616 4914-5022/com.example.alexlevine.oceanapp
W/android.hardware.renderscript@1.0::Device: getService: found null hwbinder
interface
08-16 10:48:53.616 4914-5022/com.example.alexlevine.oceanapp
D/RenderScript HIDL Adaptation: IRenderScriptDevice::getService() returned
0x0
08-16 10:48:53.617 4914-5022/com.example.alexlevine.oceanapp
D/RenderScript HIDL Adaptation: Using Fallback Path.
08-16 10:48:53.658 4914-5022/com.example.alexlevine.oceanapp D/RenderScript:
Successfully queried cache dir:
/data/user_de/0/com.example.alexlevine.oceanapp/code_cache
08-16 10:48:53.658 4914-5022/com.example.alexlevine.oceanapp D/RenderScript:
Setting cache dir:
/data/user_de/0/com.example.alexlevine.oceanapp/code_cache
08-16 10:48:53.829 4914-4927/com.example.alexlevine.oceanapp I/zygote:
NativeAllocBackground concurrent copying GC freed 3145(241KB) AllocSpace
objects, 5(144KB) LOS objects, 50% free, 2MB/4MB, paused 6.862ms total
47.477ms
08-16 10:48:53.874 4914-5022/com.example.alexlevine.oceanapp
D/EGL_emulation: eglMakeCurrent: 0xa3f06500: ver 2 0 (tinfo 0xa3f035b0)
08-16 10:48:53.999 4914-4927/com.example.alexlevine.oceanapp I/zygote:
NativeAllocBackground concurrent copying GC freed 1640(91KB) AllocSpace
objects, 0(0B) LOS objects, 49% free, 2MB/4MB, paused 9.294ms total 44.961ms
08-16 10:48:54.272 4914-5020/com.example.alexlevine.oceanapp
E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
Process: com.example.alexlevine.oceanapp, PID: 4914
java.lang.RuntimeException: An error occurred while executing
doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:325)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:383)
at java.util.concurrent.FutureTask.setException(FutureTask.java:252)
at java.util.concurrent.FutureTask.run(FutureTask.java:271)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243)
at
java.util.concurrent.ThreadPoolExecutor.runWorker
(ThreadPoolExecutor.java:1162)
at
java.util.concurrent.ThreadPoolExecutor$Worker.run
(ThreadPoolExecutor.java:636)
at java.lang.Thread.run(Thread.java:764)
Caused by: com.google.gson.JsonSyntaxException:
java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line
1 column 1 path $
at com.google.gson.Gson.fromJson(Gson.java:873)
at com.google.gson.Gson.fromJson(Gson.java:826)
at com.google.gson.Gson.fromJson(Gson.java:775)
at
com.example.alexlevine.oceanapp.fetchSOCATData.doInBackground
(fetchSOCATData.java:67)
at
com.example.alexlevine.oceanapp.fetchSOCATData.doInBackground
(fetchSOCATData.java:29)
at android.os.AsyncTask$2.call(AsyncTask.java:305)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243)
at
java.util.concurrent.ThreadPoolExecutor.runWorker
(ThreadPoolExecutor.java:1162)
at
java.util.concurrent.ThreadPoolExecutor$Worker.run
(ThreadPoolExecutor.java:636)
at java.lang.Thread.run(Thread.java:764)
Caused by: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was
STRING at line 1 column 1 path $
at com.google.gson.stream.JsonReader.beginArray(JsonReader.java:351)
at
com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read
(CollectionTypeAdapterFactory.java:80)
at
com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read
(CollectionTypeAdapterFactory.java:61)
at com.google.gson.Gson.fromJson(Gson.java:861)
at com.google.gson.Gson.fromJson(Gson.java:826)
at com.google.gson.Gson.fromJson(Gson.java:775)
at
com.example.alexlevine.oceanapp.fetchSOCATData.doInBackground
(fetchSOCATData.java:67)
at
com.example.alexlevine.oceanapp.fetchSOCATData.doInBackground
(fetchSOCATData.java:29)
at android.os.AsyncTask$2.call(AsyncTask.java:305)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243)
at
java.util.concurrent.ThreadPoolExecutor.runWorker
(ThreadPoolExecutor.java:1162)
at java.util.concurrent.ThreadPoolExecutor$Worker.run
(ThreadPoolExecutor.java:636)
at java.lang.Thread.run(Thread.java:764)
08-16 10:48:54.838 4914-5018/com.example.alexlevine.oceanapp
W/DynamiteModule: Local module descriptor class for
com.google.android.gms.googlecertificates not found.
08-16 10:48:54.870 4914-5053/com.example.alexlevine.oceanapp I/Process:
Sending signal. PID: 4914 SIG: 9
以下是我的数据的前几行: 请注意,完整的数据有数千行:
{
"table": {
"columnNames": ["year", "longitude", "latitude", "fCO2_recommended",
"time"],
"columnTypes": ["int", "double", "double", "double", "String"],
"columnUnits": [null, "degrees_east", "degrees_north", "uatm", "UTC"],
"rows": [
[2015, -8.0721, -70.533, 394.45842722124314, "2015-01-01T00:00:01Z"],
[2015, -8.0721, -70.533, 393.47776480047287, "2015-01-01T00:01:07Z"],
[2015, -8.0721, -70.533, 394.12413998170973, "2015-01-01T00:02:16Z"],
[2015, -8.0721, -70.533, 393.19812650057384, "2015-01-01T00:03:22Z"],
[2015, -8.0721, -70.533, 393.06835948609546, "2015-01-01T00:04:28Z"],
[2015, -8.0721, -70.533, 392.560674296763, "2015-01-01T00:05:35Z"]
]
}
}
我只是希望能够获取"行"并将其用作数组列表。请注意,我无权操作这些数据。
以下是导致问题的代码片段:
Gson gson = new Gson();
Type collectionType = new
TypeToken<Collection<OfficialSOCATPoint.TableBean>>() {}.getType();
Collection<OfficialSOCATPoint.TableBean> enums = gson.fromJson(result,
collectionType);
SOCATCO2DataPoint.FeaturesBean[] protoExtract = enums.toArray(new
SOCATCO2DataPoint.FeaturesBean[enums.size()]);
List<List<String>> dataArray = (List<List<String>>) protoExtract[3];
谢谢!
您在此处将表定义为集合Collection<OfficialSOCATPoint.TableBean>
。将其更改为单个对象:
TableBean table = gson.fromJson(result, TableBean.class);