gpt4 book ai didi

android - 无法转换 com.google.gson.internal.LinkedTreeMap

转载 作者:太空宇宙 更新时间:2023-11-03 12:45:42 26 4
gpt4 key购买 nike

我第一次使用 GSON 进行改造。它在静态数据上运行流畅,但在动态数据的情况下显示上述错误。在设置文本值时在适配器中显示错误

主 Activity

public class MainActivity extends AppCompatActivity {


String Navigation_Url = "http://www.xxxxxxxx.com";
ListView listView;
ArrayList<ListPojo> listPojos = new ArrayList<>();
ListRetrofitAndroid listRetrofitAndroid;


ArrayList countries = new ArrayList();

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

listView = (ListView) findViewById(R.id.listView);
getRetrofitData();
}


void getRetrofitData() {
Retrofit retrofit = new Retrofit.Builder().baseUrl(Navigation_Url).addConverterFactory(GsonConverterFactory.create()).build();
CountryArrayAPI api = retrofit.create(CountryArrayAPI.class);
Call<List> call = api.getCountries();


call.enqueue(new Callback<List>() {

@Override
public void onResponse(Call<List> call, Response<List> response) {
Gson gson = new Gson();
try {
listPojos = (ArrayList) response.body();
System.out.println("the ArrayList is" + listPojos);


// ArrayList<ListPojo> merchantOfferModels = gson.fromJson(response, new TypeToken<ListPojo>() {}.getType());









listRetrofitAndroid = new ListRetrofitAndroid(getApplicationContext(), listPojos);
listView.setAdapter(listRetrofitAndroid);


} catch (Exception e) {
Log.d("onResponse", "There is an error");
e.printStackTrace();
}

}

@Override
public void onFailure(Call<List> call, Throwable t) {
Log.d("Failure", t.toString());
}
});

}

}

ListRetrofitAndroid(适配器)

public class ListRetrofitAndroid extends BaseAdapter {

Context mContext;
ArrayList<ListPojo> arrayList = new ArrayList<>();
String TAG = "HomeTab_adapter";


public ListRetrofitAndroid(Context context, ArrayList<ListPojo> listPojos) {
super();
this.mContext = context;
this.arrayList = listPojos;

}

@Override
public int getCount() {


return arrayList.size();
}

@Override
public Object getItem(int position) {
return arrayList.get(position);
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final Holder viewHolder;

if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// convertView = inflater.inflate(R.layout.list, parent, false);
convertView = inflater.inflate(R.layout.listview_item, parent, false);

// well set up the ViewHolder
// viewHolder = new ClassScheduleStudentAdapter.Holder();
viewHolder = new Holder();
viewHolder.textName = convertView.findViewById(R.id.list_item_main);
convertView.setTag(viewHolder);


} else {

viewHolder = (Holder) convertView.getTag();
}

viewHolder.textName.setText(arrayList.get(position).getTitle());


return convertView;
}

class Holder {
TextView textName;
}


}

模型(ListPojo)

public class ListPojo {

String title;

public ListPojo(String title) {
this.title = title;
}

public String getTitle() {
return title;
}
}

界面

public interface CountryArrayAPI {
@GET("/api/popular_destinations")
public Call<List> getCountries();
}

JSON

[
{
"id": 1,
"title": "Hiranya Varna Mahavihar",
"location": "Kwalakhu Rd, Patan 44600, Nepal",
"main_image": "images/popular_places/patan-durbar-square.jpg"
},
{
"id": 2,
"title": "Yasodhar Mahavihar-Bubahal",
"location": "Jana Bahal Rd, Patan 44600, Nepal",
"main_image": "images/popular_places/patan-durbar-square.jpg"
},
{
"id": 3,
"title": "Krishna Temple Sundhara",
"location": "Tyagal - Lalitpur Rd, Patan 44600, Nepal",
"main_image": "images/popular_places/Patan-2.jpg"
},
{
"id": 4,
"title": "Hiranya Varna Mahavihar",
"location": "Kwalakhu Rd, Patan 44600, Nepal",
"main_image": "images/popular_places/asd.jpg"
}
]

StsckTrace

09-19 05:15:32.475 18949-18949/com.example.user_pc.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.user_pc.myapplication, PID: 18949
java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to com.example.user_pc.myapplication.pojo.ListPojo
at com.example.user_pc.myapplication.adpater.ListRetrofitAndroid.getView(ListRetrofitAndroid.java:71)
at android.widget.AbsListView.obtainView(AbsListView.java:2255)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1263)
at android.widget.ListView.onMeasure(ListView.java:1175)
at android.view.View.measure(View.java:16497)
at android.support.constraint.ConstraintLayout.internalMeasureChildren(ConstraintLayout.java:934)
at android.support.constraint.ConstraintLayout.onMeasure(ConstraintLayout.java:973)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:391)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291)
at android.view.View.measure(View.java:16497)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1912)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1109)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1291)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:996)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5600)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
at android.view.Choreographer.doCallbacks(Choreographer.java:574)
at android.view.Choreographer.doFrame(Choreographer.java:544)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
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)

如何解决这个问题?

最佳答案

您正在使用 ArrayList<ListPojo>所以你需要确保解析应该使用正确的类型 List<ListPojo> 进行而不是 list没有类型

由于缺少类型信息,gson正在返回 List内部的创建对象 LinkedTreeMap并且由于您使用的是 raw类型(应该是对未经检查的强制转换的警告)声明为具体类型,这将在适配器实际期待 PojoObject 时产生问题但在内部是另一回事。所以解决方案

Never use raw-types anywhere possible

就这样

public interface CountryArrayAPI {
@GET("api/popular_destinations")
public Call<List<ListPojo>> getCountries();
}

同时删除 /之前 api通话就像

call.enqueue(new Callback<List<ListPojo>>() {

@Override
public void onResponse(Call<List<ListPojo>> call, Response<List<ListPojo>> response) {
listPojos = response.body();

并且始终编码为interface所以

List<ListPojo> listPojos = ...;

关于android - 无法转换 com.google.gson.internal.LinkedTreeMap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46296747/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com