Android 在服务上使用Gson

Android 在服务上使用Gson,android,gson,Android,Gson,为了发出http请求,我将使用一个Intent服务和一个ResultReceiver。问题是我需要将我想要的结果类型传递给服务。基本上,需要在服务中使用Gson,以便在后台进行此处理。像这样的 Type collectionType = new TypeToken<TYPE>(){}.getType(); receiver.send(200, gson.fromJson(json, collectionType)); 我知道这个类必须是可序列化的,或者是可分包的。但是,即使在一个新

为了发出http请求,我将使用一个Intent服务和一个ResultReceiver。问题是我需要将我想要的结果类型传递给服务。基本上,需要在服务中使用Gson,以便在后台进行此处理。像这样的

Type collectionType = new TypeToken<TYPE>(){}.getType();
receiver.send(200, gson.fromJson(json, collectionType));

我知道这个类必须是可序列化的,或者是可分包的。但是,即使在一个新类中封装类型,我也无法使它工作。

不确定,不如改为在intent中传递对象的完全限定名(字符串格式)。并调用
Class.forName(fullyQualifiedName)
来传递一个类
gson.fromJson(json,ResultTofClassForName)
看看这个教程,希望对你有所帮助


我的问题不是如何使用Gson。Class.forName()不适用于“java.util.Collection”之类的类型。你知道怎么解决这个问题吗?
protected void request(String url, Bundle parametros, int method, HttpInterceptor.Receiver callback){
    Type type = callback.getType();
    HttpInterceptor receiver = new HttpInterceptor(activity, new Handler());
    receiver.setReceiver(callback);
    receivers.add(receiver);
    Intent intent = new Intent(activity, RESTService.class);
    intent.setData(Uri.parse(url));
    intent.putExtra(RESTService.EXTRA_AUTH, true);
    intent.putExtra(RESTService.EXTRA_PARAMS, parametros);
    intent.putExtra(RESTService.EXTRA_HTTP_VERB, method);
    intent.putExtra(RESTService.EXTRA_RESULT_RECEIVER, receiver);
    intent.putExtra(RESTService.EXTRA_TYPE, type); //not working
    activity.startService(intent);
}