java.util.LinkedHashMap无法强制转换为DataList

ans*_*hul 4 android jackson retrofit

我正在尝试使用改造库实现服务器连接.一切似乎都很好但是当我收到有关成功回调的数据时,它会因以下异常而崩溃.

java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to aandroid.com.retrofitframework.requestData.DataList
        at cuiserve.com.volleyframework.activity.RetrofitActivity$1.onDataReceived(RetrofitActivity.java:18)
        at cuiserve.com.volleyframework.httpConnection.ConnectionHelper.success(ConnectionHelper.java:44)
        at retrofit.CallbackRunnable$1.run(CallbackRunnable.java:45)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5001)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
        at dalvik.system.NativeStart.main(Native Method)
Run Code Online (Sandbox Code Playgroud)

这是进行服务器调用的活动类

public class RetrofitActivity extends SuperActivity {


    private ConnectionHelper.ServerListener<DataList> httpListener = new ConnectionHelper.ServerListener<DataList>() {

        @Override
        public void onDataReceived(DataList data) {
            hideProgressBar();
            Log.d("ANSH",data.getBalance());
        }

        @Override
        public void onErrorReceived(String errorMsg) {
            hideProgressBar();
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        showProgressBar();

        ConnectionHelper<DataList> helper = new ConnectionHelper<DataList>(HttpRequestConstant.LOGIN_REQUEST,httpListener);
        helper.getResponse();
    }

    @Override
    public View getLayoutResource() {
        return null;
    }

    @Override
    protected void internetAvailable() {

    }
}
Run Code Online (Sandbox Code Playgroud)

ConnectionHelper.class发出请求

public class ConnectionHelper<T> implements Callback<T> {


    private int requestType;
    private ServerListener<T> listener;

    public ConnectionHelper(int requestType, ServerListener listener) {
        this.requestType = requestType;
        this.listener = listener;
    }

    public void getResponse() {
        switch (requestType) {
            case HttpRequestConstant.LOGIN_REQUEST:
                IServerConnection<T> connection = restAdapter.create(IServerConnection.class);
                connection.login(this);
                break;
        }
    }


    RestAdapter restAdapter = new RestAdapter.Builder()
            .setEndpoint("http://www.json-generator.com/api")
            .setLogLevel(RestAdapter.LogLevel.FULL)
            .setConverter(new JacksonConverter(Mapper.get()))
            .build();

    @Override
    public void success(T t, Response response) {
        listener.onDataReceived(t);
        // success callback false here with the exception
        // as ClassCastException and says LinkedHashMap can not be cast to DataList(which i pass as class that the response has to be mapped to)
    }

    @Override
    public void failure(RetrofitError error) {

    }


    public interface ServerListener<T> {

        public void onDataReceived(T data);

        public void onErrorReceived(String errorMsg);

    }
}
Run Code Online (Sandbox Code Playgroud)

具有改进注释的界面和方法

public interface IServerConnection<T> {

    @GET(HttpRequestConstant.JACKSON_FETCH)
    void login(Callback<T> cb);
}
Run Code Online (Sandbox Code Playgroud)

自定义JacksonConverter是我的怀疑

public class JacksonConverter implements Converter {
    private ObjectMapper mapper = new ObjectMapper();

    public JacksonConverter(ObjectMapper objectMapper) {
        this.mapper = objectMapper;
    }

    @Override
    public Object fromBody(TypedInput body, Type type) throws ConversionException {
        JavaType javaType = mapper.getTypeFactory().constructType(type);

        try {
            return mapper.readValue(body.in(), javaType);
        } catch (IOException e) {
            throw new ConversionException(e);
        }
    }

    @Override
    public TypedOutput toBody(Object object) {
        try {
            String charset = "UTF-8";
            String json = mapper.writeValueAsString(object);
            return new JsonTypedOutput(json.getBytes(charset));
        } catch (IOException e) {
            throw new AssertionError(e);
        }
    }

    private static class JsonTypedOutput implements TypedOutput {
        private final byte[] jsonBytes;

        JsonTypedOutput(byte[] jsonBytes) {
            this.jsonBytes = jsonBytes;
        }

        @Override
        public String fileName() {
            return null;
        }

        @Override
        public String mimeType() {
            return "application/json; charset=UTF-8";
        }

        @Override
        public long length() {
            return jsonBytes.length;
        }

        @Override
        public void writeTo(OutputStream out) throws IOException {
            out.write(jsonBytes);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

和DataList

package cuiserve.com.volleyframework.requestData;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

/**
 * Created by ansh on 5/4/15.
 */


@JsonIgnoreProperties(ignoreUnknown = true)
public class DataList extends SomeClass{

    private String _id;

    private int index;

    private String guid;

    private boolean isActive;

    private String balance;

    private String picture;

    private int age;

    public String get_id() {
        return _id;
    }

    public void set_id(String _id) {
        this._id = _id;
    }

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }

    public String getGuid() {
        return guid;
    }

    public void setGuid(String guid) {
        this.guid = guid;
    }

    public boolean isActive() {
        return isActive;
    }

    public void setActive(boolean isActive) {
        this.isActive = isActive;
    }

    public String getBalance() {
        return balance;
    }

    public void setBalance(String balance) {
        this.balance = balance;
    }

    public String getPicture() {
        return picture;
    }

    public void setPicture(String picture) {
        this.picture = picture;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}
Run Code Online (Sandbox Code Playgroud)

现在的问题是,当我不使用泛型(我已在代码中完成)这样做的事情工作正常,并没有给出任何例外,但在泛型的情况下它失败.

为什么我得到LinkedHashMap classCastException,其中没有任何东西与我的代码中的相关.请帮助.

Jak*_*ton 6

通过使用泛型参数,实际类型信息完全丢失到运行时并且无法推断.它本质上最终是相同的Object.当Gson发现你想要一个Object类型时,它使用a Map来放置JSON信息.这样,如果你重新序列化该对象实例,数据将被保留.

您不能将通用接口与Retrofit一起使用.当您尝试执行此操作时添加了一个例外,而不是让它以这种方式在下一个版本中失败.