Gson 序列化Javabean的时候要注意的地方

错误一:
08-11 01:19:28.126: E/AndroidRuntime(1306): FATAL EXCEPTION: main
08-11 01:19:28.126: E/AndroidRuntime(1306): Process: com.topnews, PID: 1306
08-11 01:19:28.126: E/AndroidRuntime(1306): java.lang.NumberFormatException: Invalid long: "2015-08-11 13:18"
08-11 01:19:28.126: E/AndroidRuntime(1306): 	at java.lang.Long.invalidLong(Long.java:124)
08-11 01:19:28.126: E/AndroidRuntime(1306): 	at java.lang.Long.parse(Long.java:361)
08-11 01:19:28.126: E/AndroidRuntime(1306): 	at java.lang.Long.parseLong(Long.java:352)
08-11 01:19:28.126: E/AndroidRuntime(1306): 	at java.lang.Long.parseLong(Long.java:318)
08-11 01:19:28.126: E/AndroidRuntime(1306): 	at java.lang.Long.valueOf(Long.java:476)
08-11 01:19:28.126: E/AndroidRuntime(1306): 	at com.topnews.tool.DateTools.getSection(DateTools.java:139)
08-11 01:19:28.126: E/AndroidRuntime(1306): 	at com.topnews.adapter.NewsAdapter.initDateHead(NewsAdapter.java:72)
08-11 01:19:28.126: E/AndroidRuntime(1306): 	at com.topnews.adapter.NewsAdapter.<init>(NewsAdapter.java:55)
08-11 01:19:28.126: E/AndroidRuntime(1306): 	at com.topnews.fragment.NewsFragment$1.handleMessage(NewsFragment.java:135)
08-11 01:19:28.126: E/AndroidRuntime(1306): 	at android.os.Handler.dispatchMessage(Handler.java:102)
08-11 01:19:28.126: E/AndroidRuntime(1306): 	at android.os.Looper.loop(Looper.java:136)
08-11 01:19:28.126: E/AndroidRuntime(1306): 	at android.app.ActivityThread.main(ActivityThread.java:5017)
08-11 01:19:28.126: E/AndroidRuntime(1306): 	at java.lang.reflect.Method.invokeNative(Native Method)
08-11 01:19:28.126: E/AndroidRuntime(1306): 	at java.lang.reflect.Method.invoke(Method.java:515)
08-11 01:19:28.126: E/AndroidRuntime(1306): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
08-11 01:19:28.126: E/AndroidRuntime(1306): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
08-11 01:19:28.126: E/AndroidRuntime(1306): 	at dalvik.system.NativeStart.main(Native Method)

错误二:

如果是php做后台的时候,传回时间一般都用时间戳,不要传回一个string  而是传回一个数字

08-11 01:11:01.096: W/System.err(1230): com.google.gson.JsonSyntaxException: java.lang.NumberFormatException: Invalid double: "1970-01-01 08:00:00"
08-11 01:11:01.106: W/System.err(1230): 	at com.google.gson.internal.bind.TypeAdapters$8.read(TypeAdapters.java:263)
08-11 01:11:01.116: W/System.err(1230): 	at com.google.gson.internal.bind.TypeAdapters$8.read(TypeAdapters.java:253)
08-11 01:11:01.116: W/System.err(1230): 	at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:93)
08-11 01:11:01.116: W/System.err(1230): 	at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:172)
08-11 01:11:01.126: W/System.err(1230): 	at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:40)
08-11 01:11:01.126: W/System.err(1230): 	at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:81)
08-11 01:11:01.126: W/System.err(1230): 	at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:60)
08-11 01:11:01.126: W/System.err(1230): 	at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:93)
08-11 01:11:01.126: W/System.err(1230): 	at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:172)
08-11 01:11:01.126: W/System.err(1230): 	at com.google.gson.Gson.fromJson(Gson.java:803)
08-11 01:11:01.126: W/System.err(1230): 	at com.google.gson.Gson.fromJson(Gson.java:768)
08-11 01:11:01.136: W/System.err(1230): 	at com.google.gson.Gson.fromJson(Gson.java:717)
08-11 01:11:01.136: W/System.err(1230): 	at com.topnews.asyncTask.NewsAsyncTask.doInBackground(NewsAsyncTask.java:45)
08-11 01:11:01.146: W/System.err(1230): 	at com.topnews.asyncTask.NewsAsyncTask.doInBackground(NewsAsyncTask.java:1)
08-11 01:11:01.146: W/System.err(1230): 	at android.os.AsyncTask$2.call(AsyncTask.java:288)
08-11 01:11:01.146: W/System.err(1230): 	at java.util.concurrent.FutureTask.run(FutureTask.java:237)
08-11 01:11:01.156: W/System.err(1230): 	at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
08-11 01:11:01.156: W/System.err(1230): 	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
08-11 01:11:01.176: W/System.err(1230): 	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
08-11 01:11:01.176: W/System.err(1230): 	at java.lang.Thread.run(Thread.java:841)
08-11 01:11:01.186: W/System.err(1230): Caused by: java.lang.NumberFormatException: Invalid double: "1970-01-01 08:00:00"
08-11 01:11:01.196: W/System.err(1230): 	at java.lang.StringToReal.invalidReal(StringToReal.java:63)
08-11 01:11:01.196: W/System.err(1230): 	at java.lang.StringToReal.parseDouble(StringToReal.java:269)
08-11 01:11:01.206: W/System.err(1230): 	at java.lang.Double.parseDouble(Double.java:295)
08-11 01:11:01.206: W/System.err(1230): 	at com.google.gson.stream.JsonReader.nextLong(JsonReader.java:953)
08-11 01:11:01.216: W/System.err(1230): 	at com.google.gson.internal.bind.TypeAdapters$8.read(TypeAdapters.java:261)
08-11 01:11:01.266: D/dalvikvm(1230): GC_FOR_ALLOC freed 803K, 11% free 7309K/8188K, paused 53ms, total 53ms
08-11 01:11:01.266: W/System.err(1230): 	... 19 more

错误三:当你服务器端传回的json确定没有问题的时候,但是反序列化的时候老是报错

[android]com.google.gson.JsonSyntaxException: java.lang.IllegalStateException

// NewsBean newsbean = gson.fromJson(jsonStr, NewsBean.class); 这是我以前错误的代码,如果这样写的话,newsbean始终为空值,仅仅这一行代码不过 

因为前面看到那个博客里面他反序列的时候只有这一行代码,好像是无法解析成功的,

之后改正的代码如下:

java.lang.reflect.Type type = new TypeToken<NewsBean>() {}.getType();   NewsBean newsbean = gson.fromJson(jsonStr, type);

只有这样才能把服务器端传回的json字符串 string类型的,转换成为javabean对象

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

扫码关注云+社区

领取腾讯云代金券