I am trying to pass an object containing Google Analytics Reporting data to Intent through a stream. The problem is deserialization, which returns a LinkedTreeMap instead of the original serialized object, which causes a ClassCastException to fail. I tried to execute all the answers found here on SO using TypeToken to change the ProGuard rules and nothing worked.
I was thinking of implementing the Parcelable interface, but the problem is that I have an internal AsyncTask private class where the data is collected and inserted into the intent, which will be sent via broadcast.
Here is the helper code where the data is serialized:
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();
IntentFilter filter = new IntentFilter("com.keyone.contactpackapp.ANALYTICS_DATA");
m_receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent)
{
String szJson = intent.getStringExtra("response");
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);
}
}