简体   繁体   English

线程“主”中的异常 java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap 无法转换

[英]Exception in thread “main” java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast t

I am trying to consume rest service using retrofit2 and it works fine when my pojo does not have a generic type.我正在尝试使用 retrofit2 使用 rest 服务,当我的 pojo 没有泛型类型时它工作正常。 When I don't use generic everything works fine.当我不使用通用时,一切正常。 Here is my retrofit Service.这是我的 retrofit 服务。 The error is occurring at the point of iterating over the Array List as shown in the picture in the This Link .This Link中的图片所示,在遍历 Array List 时发生错误。

   public interface EposService 
{
    @POST("v1/api/product/load_product")
    public Call<RestResponseObject> getProducts(@Body ProductParam udata);    

}
public class RestResponseObject<T> {
       public String message;
        private Object payload;
        private boolean responseStatus,status;
        List<T> data;//getter setter
}
public class ProductBean  {

     private String make, manufacture_year, name;//getter setter

}
private static RestResponseObject<ProductBean> findProducts(ProductParam pay) {
         RestResponseObject mv=new RestResponseObject();
          EposService service = retrofit.create(EposService.class);

        final Call<RestResponseObject> call = service.getProducts(pay);

        try {
            Response<RestResponseObject> resp = call.execute();
           if(resp!=null)
                {
            if (resp.code() == 200) {


                       mv=resp.body();

            } 
            else{

               // mv.setStatus(false);//("05");
                //mv.setMessage("connection error");
            }
        }

        } catch (Exception e) {

          e.printStackTrace();

        }

        return mv;
    }

Here is full stack trace.这是完整的堆栈跟踪。

Exception in thread "main" java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to api.bean.ProductBean
    at retrofit.Exec.main(Exec.java:63)

The object being received is being converted into a LinkedTreeMap by your JSON converter, and you shall somehow need to convert it into a ProductBean for you to access it JSON 转换器正在将收到的 object 转换为 LinkedTreeMap,您需要以某种方式将其转换为 ProductBean 以便您访问它

Comment out the for loop you have and paste the below code below it and see what happens:注释掉你拥有的 for 循环,然后将下面的代码粘贴到它下面,看看会发生什么:

for(Object b: ps)
{
    ProductBean bean = new Gson().fromJson(new Gson().toJson(((LinkedTreeMap<String, Object>) b)), ProductBean.class);
    System.out.println(bean.getName());
}

(Based on this answer) (基于这个答案)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to com.google.gson.internal.LinkedTreeMap - java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to com.google.gson.internal.LinkedTreeMap java.lang.ClassCastException:com.google.gson.internal.LinkedTreeMap无法强制转换为com.example - java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to com.example java.lang.ClassCastException:com.google.gson.internal.LinkedTreeMap无法转换为java.util.LinkedHashMap - java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to java.util.LinkedHashMap java.lang.ClassCastException:com.google.gson.internal.LinkedTreeMap无法转换为asd.Grade - java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to asd.Grade java.lang.ClassCastException:com.google.gson.internal.LinkedTreeMap 无法转换为模型 - java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to model Android:Loop for正在获取java.lang.ClassCastException:com.google.gson.internal.LinkedTreeMap无法强制转换 - Android: Loop for is getting java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap 无法转换为模型类 android - java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to model class android java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to pojo.class - java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to pojo.class 为什么 java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap 无法转换为 com.readbook.chinesepoetry.data.model.Response? - why java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to com.readbook.chinesepoetry.data.model.Response? 传递参数的通用方法-java.lang.ClassCastException:com.google.gson.internal.LinkedTreeMap - generic method passing parramaters - java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM