Disabling Google Analytics deserialization using Gson returns LinkedTreeMap

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();

        // 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);
    }
}
+4
1

, Gson, Java Serializable Android Parcelable - , .

,

0

Source: https://habr.com/ru/post/1654352/


All Articles