I'm getting this exception while trying to populate my listview with JsonConverter
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 1 column 1 path $
This is my JsonConverter class :
public class JsonConverter<T> {
public JsonConverter() {
}
public ArrayList<T> toArrayList(String jsonString, Class<T> clazz) {
GsonBuilder builder = new GsonBuilder();
builder.setDateFormat("dd/MM/yy HH:mm:ss");
Gson gson = builder.create();
JsonConverter.ListParameterizedType type = new JsonConverter.ListParameterizedType(clazz);
ArrayList list = (ArrayList)gson.fromJson(jsonString, type);
return list;
}
public List<T> toList(String jsonString, Class<T> clazz) {
ArrayList list = this.toArrayList(jsonString, clazz);
return list;
}
private static class ListParameterizedType implements ParameterizedType {
private Type type;
private ListParameterizedType(Type type) {
this.type = type;
}
public Type[] getActualTypeArguments() {
return new Type[]{this.type};
}
public Type getRawType() {
return ArrayList.class;
}
public Type getOwnerType() {
return null;
}
}
}
This is my model class :
public class Product implements Serializable {
@SerializedName("pid")
public int pid;
@SerializedName("name")
public String name;
@SerializedName("qty")
public int qty;
@SerializedName("price")
public String description;
@SerializedName("image_url")
public String image_url;
@SerializedName("date")
public String date;
}
And I'm calling it like this :
private ArrayList<Product> productList;
@Override
public void processFinish(String s) {
productList = new JsonConverter<Product>().toArrayList(s, Product.class);
.
.
.
Don't Know if I did something wrong here