我正在开发一个电子商务应用程序,所以我需要在android studio中创建一个带有sharedpreference的cartManager。我已经创建了Map<项目,项目计数>然后将字符串放入sharedpreferences中一切都很好,但在getCartItem((中我收到了这个错误-
com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:应为BEGIN_OBJECT,但在第1行第3列路径$处为STRING
我已经写了下面所有相关的代码。我是安卓工作室的新手,请帮帮我。
这是布局(递增-递减cartmanager值(
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">
<androidx.cardview.widget.CardView
android:id="@+id/cardViewDec"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
app:cardCornerRadius="8dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginStart="12dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="12dp"
android:layout_marginBottom="8dp"
android:gravity="center"
android:text="-"
android:textColor="@color/black" />
</androidx.cardview.widget.CardView>
<TextView
android:id="@+id/textViewItemCount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="0"
android:textColor="@color/black" />
<androidx.cardview.widget.CardView
android:id="@+id/cardViewInc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
app:cardCornerRadius="8dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginStart="12dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="12dp"
android:layout_marginBottom="8dp"
android:gravity="center"
android:text="+"
android:textColor="@color/black" />
</androidx.cardview.widget.CardView>
</LinearLayout>
这是CartManager
public class CartManager {
private static final String CART_ITEMS = "cartItems";
private static SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(App.getApp());
public static void setCartItems(Map<Item, Integer> mapCartItem) {
SharedPreferences.Editor editor = preferences.edit();
Gson gson = new Gson();
String jsonCartItems = gson.toJson(mapCartItem);
editor.putString(CART_ITEMS, jsonCartItems);
editor.apply();
}
public static Map<Item, Integer> getCartItems() {
String json = preferences.getString(CART_ITEMS, null);
Gson gson = new Gson();
Type collectionType = new TypeToken<Map<Item, Integer>>() {
}.getType();
return gson.fromJson(json, collectionType);
}
这是活动类
binding.cardViewInc.setOnClickListener(v -> {
int itemCount = Integer.parseInt(binding.textViewItemCount.getText().toString());
vibrator.vibrate(200);
binding.textViewItemCount.setText(String.valueOf(++itemCount));
if (CartManager.getCartItems() != null) {
Map<Item, Integer> cart = CartManager.getCartItems();
Log.d(TAG, "onCreate: " + cart);
cart.put(item, itemCount);
CartManager.setCartItems(cart);
} else {
Map<Item, Integer> cart = new HashMap<>();
cart.put(item, itemCount);
CartManager.setCartItems(cart);
}
});
binding.cardViewDec.setOnClickListener(v -> {
int itemCount = Integer.parseInt(binding.textViewItemCount.getText().toString());
if (itemCount == 0)
return;
vibrator.vibrate(200);
binding.textViewItemCount.setText(String.valueOf(--itemCount));
Map<Item, Integer> cart = CartManager.getCartItems();
Log.d(TAG, "onCreate: " + cart);
cart.put(item, itemCount);
CartManager.setCartItems(cart);
});
这是我现在遇到的错误
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 3 path $.
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:226)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:41)
at com.google.gson.internal.bind.MapTypeAdapterFactory$Adapter.read(MapTypeAdapterFactory.java:186)
at com.google.gson.internal.bind.MapTypeAdapterFactory$Adapter.read(MapTypeAdapterFactory.java:145)
at com.google.gson.Gson.fromJson(Gson.java:927)
at com.google.gson.Gson.fromJson(Gson.java:892)
at com.google.gson.Gson.fromJson(Gson.java:841)
at com.account.CartManager.getCartItems(CartManager.java:31)
at com.activities.ProductActivity.lambda$onCreate$1$ProductActivity(ProductActivity.java:61)
at com.activities.-$$Lambda$ProductActivity$eIp-9al-fb-L4Pi9XbJi9DUaRDU.onClick(Unknown Source:6)
at android.view.View.performClick(View.java:7184)
at android.view.View.performClickInternal(View.java:7161)
at android.view.View.access$3500(View.java:818)
at android.view.View$PerformClick.run(View.java:27677)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:224)
at android.app.ActivityThread.main(ActivityThread.java:7562)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)
Caused by: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 3 path $.
at com.google.gson.stream.JsonReader.beginObject(JsonReader.java:385)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:215)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:41)
at com.google.gson.internal.bind.MapTypeAdapterFactory$Adapter.read(MapTypeAdapterFactory.java:186)
at com.google.gson.internal.bind.MapTypeAdapterFactory$Adapter.read(MapTypeAdapterFactory.java:145)
at com.google.gson.Gson.fromJson(Gson.java:927)
at com.google.gson.Gson.fromJson(Gson.java:892)
at com.google.gson.Gson.fromJson(Gson.java:841)
at com.account.CartManager.getCartItems(CartManager.java:31)
at com.activities.ProductActivity.lambda$onCreate$1$ProductActivity(ProductActivity.java:61)
at com.activities.-$$Lambda$ProductActivity$eIp-9al-fb-L4Pi9XbJi9DUaRDU.onClick(Unknown Source:6)
at android.view.View.performClick(View.java:7184)
at android.view.View.performClickInternal(View.java:7161)
at android.view.View.access$3500(View.java:818)
at android.view.View$PerformClick.run(View.java:27677)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:224)
at android.app.ActivityThread.main(ActivityThread.java:7562)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)
2021-06-19 08:51:50.623 30179-30179/com I/Process: Sending signal. PID: 30179 SIG: 9
这是我得到的jsonString
{"Item{_idu003du002760c8bfc64a5f0e3f042cb674u0027, nameu003du0027fffu0027, priceu003d888, sizeu003d6, discountu003d888, coloru003du0027Blacku0027, deliveryFeeu003d88, imageUrlu003du0027https://firebasestorage.googleapis.com/v0/b/shoes-footwear.appspot.com/o/items%2F1623769024945?altu003dmediau0026tokenu003d6094d72b-2f98-483a-a9e5-0d39d13ce066u0027, genderu003d0, createdAtu003d0}":1}
问题是您试图解析JSON
对象,但JSON响应中有String
。