Java 带泛型的gson的正确语法

Java 带泛型的gson的正确语法,java,android,generics,android-asynctask,gson,Java,Android,Generics,Android Asynctask,Gson,我正在尝试编写一个可重用的asynctask,其中定义了Gson应该在asynctask的构造函数中反序列化的类的类型。由于以前从未使用过Java泛型,我对如何继续操作有些迷茫。我无法找出fromJson方法的正确语法 我收到的错误是 Cannot resolve method'fromJson(java.io.InputStream, java.lang.Class<T>)' 无法解析方法'fromJson(java.io.InputStream,java.lang.Class)

我正在尝试编写一个可重用的asynctask,其中定义了Gson应该在asynctask的构造函数中反序列化的类的类型。由于以前从未使用过Java泛型,我对如何继续操作有些迷茫。我无法找出fromJson方法的正确语法

我收到的错误是

Cannot resolve method'fromJson(java.io.InputStream, java.lang.Class<T>)'
无法解析方法'fromJson(java.io.InputStream,java.lang.Class)'
完整的异步任务

public class AsyncGet<T> extends AsyncTask<String,String,ApiResponse> {

    private String TAG = "AsyncGet";
    private HttpURLConnection mConnection;
    private IApiCallback mCallback;
    private Context mContext;
    private Class<T> type;

    public AsyncGet(IApiCallback callback, Class<T> classType, Context context) {
        this.mCallback = callback;
        this.mContext = context;
        this.type = classType;
    }

    @Override
    protected ApiResponse doInBackground(String... uri) {

        try {

            URL url = new URL(uri[0]);
            mConnection = (HttpURLConnection) url.openConnection();
            mConnection.setConnectTimeout(5000);
            mConnection.setReadTimeout(60000);
            mConnection.addRequestProperty("Accept-Encoding", "gzip");
            mConnection.addRequestProperty("Cache-Control", "no-cache");
            mConnection.connect();

            String encoding = mConnection.getContentEncoding();

            InputStream inStream;
            if (encoding != null && encoding.equalsIgnoreCase("gzip")) {
                inStream = new GZIPInputStream(mConnection.getInputStream());
            } else {
                inStream = mConnection.getInputStream();
            }

            if (inStream != null) {

                try {
                    Gson gson = new Gson();
                    ApiResponse response = new ApiResponse();
                    response.data = gson.fromJson(inStream, type); // What is wrong here?
                    response.responseCode = mConnection.getResponseCode();
                    response.responseMessage = mConnection.getResponseMessage();

                    return response;

                } catch (Exception e) {
                    Log.i(TAG, "Exception");
                    if (e.getMessage() != null) {
                        Log.e(TAG, e.getMessage());
                    }
                } finally {
                    inStream.close();
                }
            }

        } catch (SocketTimeoutException e) {
            Log.i(TAG, "Socket Timeout occurred");
            FileLogger.getFileLogger(mContext).ReportException(TAG + ", SocketTimeoutException ", e);
        } catch (MalformedURLException e) {
            FileLogger.getFileLogger(mContext).ReportException(TAG + ", MalformedUrlException ", e);
        } catch (IOException e) {
            Log.i(TAG," IO Exception");
            FileLogger.getFileLogger(mContext).ReportException(TAG + ", IOException ", e);

            if (e.getMessage() != null) {
                Log.i(TAG, e.getMessage());
            }

        } finally {
            mConnection.disconnect();
        }

        return null;
    }

    @Override
    protected void onPostExecute(ApiResponse response) {

        if (!isCancelled())
            mCallback.Execute(response);
    }
}
公共类AsyncGet扩展了AsyncTask{
私有字符串TAG=“AsyncGet”;
私有HttpURLConnection mConnection;
私人IApiCallback McCallback;
私有上下文;
私人阶级类型;
公共AsyncGet(IApiCallback回调、类类型、上下文){
this.mCallback=回调;
this.mContext=上下文;
this.type=classType;
}
@凌驾
受保护的ApiResponse doInBackground(字符串…uri){
试一试{
URL=新URL(uri[0]);
mConnection=(HttpURLConnection)url.openConnection();
mConnection.setConnectTimeout(5000);
mConnection.setReadTimeout(60000);
addRequestProperty(“接受编码”、“gzip”);
addRequestProperty(“缓存控制”、“无缓存”);
mConnection.connect();
字符串编码=mConnection.getContentEncoding();
流内输入流;
if(encoding!=null&&encoding.equalsIgnoreCase(“gzip”)){
inStream=newgzip输入流(mConnection.getInputStream());
}否则{
inStream=mConnection.getInputStream();
}
如果(流内!=null){
试一试{
Gson Gson=新的Gson();
ApiResponse=新的ApiResponse();
response.data=gson.fromJson(inStream,type);//这里怎么了?
response.responseCode=mConnection.getResponseCode();
response.responseMessage=mConnection.getResponseMessage();
返回响应;
}捕获(例外e){
Log.i(标记“例外”);
如果(如getMessage()!=null){
Log.e(标记,e.getMessage());
}
}最后{
流内关闭();
}
}
}捕获(SocketTimeoutException e){
Log.i(标记“发生套接字超时”);
getFileLogger(mContext).ReportException(标记+”,SocketTimeoutException“,e);
}捕获(格式错误){
getFileLogger(mContext).ReportException(标记+”,MalformedUrlexException“,e);
}捕获(IOE异常){
Log.i(标记“IO异常”);
getFileLogger(mContext).ReportException(标记+”,IOException“,e);
如果(如getMessage()!=null){
Log.i(标记,例如getMessage());
}
}最后{
mConnection.disconnect();
}
返回null;
}
@凌驾
受保护的void onPostExecute(ApiResponse响应){
如果(!isCancelled())
执行(响应);
}
}

Gson
没有一个方法
fromJson(..)
,该方法需要一个
InputStream
作为其第一个参数。但是,它确实有这样一种方法,可以接受
读取器
。因此,只需将您的
InputStream
包装在
Reader
实现中,确切地说,
InputStreamReader

response.data = gson.fromJson(new InputStreamReader(inStream), type);

在上课之前,请先复习一遍。

您还没有告诉我们出了什么问题。我们猜不到。
response.data的声明类型是什么?response.data只是一个对象。编译错误是gson.fromJson方法签名。传入类型将创建一个错误。我正在与之通信的API有多个响应对象,因此我希望使用一个异步任务,而不管我将收到哪个响应对象。为什么不发布错误…?编辑我的原始帖子以包含编译错误快照。。。你的权利,这是一个坏的复制/粘贴。我在资料中遗漏了读者。我确信这是泛型,因为我以前从未使用过它们。谢谢。@Redshirt不客气。您应该查看错误消息。它们很清楚。