I am using Gson class to convert JSON string data to Intent object. But I am getting exception.
JSON string of Intent:
{
"targetIntent":{
"mAction":"android.intent.action.VIEW",
"mData":{
"authority":{
"decoded":"com.android.contacts",
"encoded":"com.android.contacts"
},
"fragment":{
},
"path":{
"decoded":"NOT CACHED",
"encoded":"/contacts/lookup/1471i541ce89b89ad6a59"
},
"query":{
},
"scheme":"content",
"uriString":"NOT CACHED",
"host":"NOT CACHED",
"port":-2
},
"mFlags":0
}
}
Conversion code:
Intent targetIntent =
gson.fromJson(histroyDataObject.getString("targetIntent"), Intent.class);
This gives exception:
Cause: Instantiation Exception
Detail Message: Failed to invoke private android.net.Uri() with no args
Stack trace:
08-31 01:08:19.045: W/System.err(290): Caused by: java.lang.InstantiationException: android.net.Uri
08-31 01:08:19.065: W/System.err(290): at java.lang.reflect.Constructor.constructNative(Native Method)
08-31 01:08:19.065: W/System.err(290): at java.lang.reflect.Constructor.newInstance(Constructor.java:446)
08-31 01:08:19.065: W/System.err(290): at com.google.gson.internal.ConstructorConstructor$2.construct(ConstructorConstructor.java:91)
08-31 01:08:19.076: W/System.err(290): ... 24 more
Thanks for your help.
My bet is
Intent#uriStringis of typeandroid.net.Uriwhich has no no-arg public constructor. I means you have to writeJsonDeserializer<Uri>like this:and then register it via
GsonBuilder:EDIT:
You have to register custom adapters in case any property doesn’t define public no-arg constructor. You may read more in Gson docs – Custom Serialization and Deserialization.