Android – use gson’s Google Analytics to deserialize and return the linkedtreemap
I'm trying to pass objects containing analytics reporting data in intent through broadcast. The problem is that deserialization returns linkedtreemap instead of the original serialized object, resulting in ClassCastException crash. I try to use typetoken to modify Proguard rules according to all the answers on so, but it has no effect
I want to implement the Parcelable interface, but the problem is that I have an internal private asynctask class that collects data and pushes it to the intent to be sent over the broadcast
The following is the helper code for data serialization:
public class AnalyticsHelper
{
...
private class GoogleBatchTask extends AsyncTask<GetReportsRequest,Void,GetReportsResponse>
{
@Override
protected GetReportsResponse doInBackground(@NonNull GetReportsRequest... reports)
{
GetReportsResponse response = null;
try {
if (m_reports == null)
return null;
response = m_reports.reports().batchGet(reports[0]).execute();
} catch (IOException e) {
Console.log(e);
}
return response;
}
@Override
protected void onPostExecute(GetReportsResponse response)
{
Intent intent = new Intent();
intent.setAction("com.keyone.contactpackapp.ANALYTICS_DATA");
intent.putExtra("response", new Gson().toJson(response));
Context context = PackConfig.instance().context();
if (context == null)
return;
context.sendBroadcast(intent);
}
}
}
Analyticsfragment.java, where deserialization occurs:
public class AnalyticsFragment extends Fragment
{
@Override
public void onResume()
{
super.onResume();
// Listen to custom intent with data
IntentFilter filter = new IntentFilter("com.keyone.contactpackapp.ANALYTICS_DATA");
m_receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent)
{
// Get data from intent and pass it to the right fragment
String szJson = intent.getStringExtra("response");
//m_response = new Gson().fromJson(szJson, GetReportsResponse.class);
Type listType = new TypeToken<GetReportsResponse>(){}.getType();
m_response = new Gson().fromJson(szJson, listType);
Fragment fragment = m_activity.currentFragment();
fragment.updateData();
}
};
if (m_activity != null)
m_activity.registerReceiver(m_receiver, filter);
}
}
resolvent:
Due to the nature of the object to be serialized, using gson does not use either the Java serializable interface or the Android Parcelable interface, and the object cannot be deserialized in a correct way
So I choose to call an instance of the recipient human and pass the object data through its methods