I would like to insert the data JSONArray to the adapter
but I have a problem to send it
//the JSON
{
"data": {
"products": {
"datum": [
{
"product_id": 7,
"seller_id": 14,
"product_name": "Cap",
"price": "55000"
},
{
"product_id": 8,
"seller_id": 14,
"product_name": "Jacket",
"price": "300000"
},
{
"product_id": 9,
"seller_id": 14,
"product_name": "T-shirts Blue",
"price": "100000"
}
]
}
}
}
//ProductActivity.java
ProductAdapter padapter;
List<Datum> data = new ArrayList<>();
try {
Moshi moshi = new Moshi.Builder().build();
final String dataJsonResponse = response.body().getData().getProducts().getDatum().toString();
Type type = Types.newParameterizedType(List.class, Datum.class);
JsonAdapter<List<Datum>> adapter = moshi.adapter(type);
data = adapter.fromJson(dataJsonResponse);
} catch (Exception e) {
Log.d("onResponse", "There is an error");
e.printStackTrace();
}
padapter = new ProductAdapter(getApplication(), data);
recyclerView.setAdapter(padapter);
//Datum.class
@Override public String toString() {
return String.format("%s%s%s", sellerId, productName, price);
}
when I check the in Debug, the 'dataJsonResponse' value is
[14Cloth55000, 14Jacket3000, 14T-shirts Blue100000]
and the 'data' size = 0
is the 'dataJsonResponse' is right?
why the size of 'data' is 0?
The logged exception will tell you the problem.
On Thu, Aug 31, 2017, 12:34 AM regifr notifications@github.com wrote:
I would like to insert the data JSONArray to the adapter
but I have a problem to send it//the JSON
{
"data": {
"products": {
"datum": [
{
"product_id": 7,
"seller_id": 14,
"product_name": "Cap",
"price": "55000"
},
{
"product_id": 8,
"seller_id": 14,
"product_name": "Jacket",
"price": "300000"
},
{
"product_id": 9,
"seller_id": 14,
"product_name": "T-shirts Blue",
"price": "100000"
}
]
}
}
}//ProductActivity.javaProductAdapter padapter;List
data = new ArrayList<>();
try {
Moshi moshi = new Moshi.Builder().build();
final String dataJsonResponse = response.body().getData().getProducts().getDatum().toString();
Type type = Types.newParameterizedType(List.class, Datum.class);
JsonAdapter> adapter = moshi.adapter(type);
data = adapter.fromJson(dataJsonResponse);
} catch (Exception e) {
Log.d("onResponse", "There is an error");
e.printStackTrace();
}
padapter = new ProductAdapter(getApplication(), data);
recyclerView.setAdapter(padapter);//Datum.class@Override public String toString() {
return String.format("%s%s%s", sellerId, productName, price);
}when I check the in Debug, the 'dataJsonResponse' value is
[14Cloth55000, 14Jacket3000, 14T-shirts Blue100000]
and the 'data' size = 0is the 'dataJsonResponse' is right?
why the size of 'data' is 0?—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/square/moshi/issues/347, or mute the thread
https://github.com/notifications/unsubscribe-auth/AAEEEReefU32XMe7C_-UdeVZ6nIQmVUCks5sdjfEgaJpZM4PISlu
.
what should i do?
this the error
com.squareup.moshi.JsonEncodingException: Use JsonReader.setLenient(true) to accept malformed JSON at path $[0]
at com.squareup.moshi.JsonReader.syntaxError(JsonReader.java:213)
at com.squareup.moshi.JsonUtf8Reader.checkLenient(JsonUtf8Reader.java:1012)
at com.squareup.moshi.JsonUtf8Reader.doPeek(JsonUtf8Reader.java:345)
at com.squareup.moshi.JsonUtf8Reader.hasNext(JsonUtf8Reader.java:164)
at com.squareup.moshi.CollectionJsonAdapter.fromJson(CollectionJsonAdapter.java:75)
at com.squareup.moshi.CollectionJsonAdapter$2.fromJson(CollectionJsonAdapter.java:53)
at com.squareup.moshi.JsonAdapter$2.fromJson(JsonAdapter.java:128)
at com.squareup.moshi.JsonAdapter.fromJson(JsonAdapter.java:35)
at com.squareup.moshi.JsonAdapter.fromJson(JsonAdapter.java:39)
Your JSON is malformed. You either need to fix your server, pre-process it
to correct the format, or enable lenient parsing.
On Sun, Sep 3, 2017, 11:20 AM regifr notifications@github.com wrote:
what should i do?
this the errorcom.squareup.moshi.JsonEncodingException: Use JsonReader.setLenient(true) to accept malformed JSON at path $[0]
at com.squareup.moshi.JsonReader.syntaxError(JsonReader.java:213)
at com.squareup.moshi.JsonUtf8Reader.checkLenient(JsonUtf8Reader.java:1012)
at com.squareup.moshi.JsonUtf8Reader.doPeek(JsonUtf8Reader.java:345)
at com.squareup.moshi.JsonUtf8Reader.hasNext(JsonUtf8Reader.java:164)
at com.squareup.moshi.CollectionJsonAdapter.fromJson(CollectionJsonAdapter.java:75)
at com.squareup.moshi.CollectionJsonAdapter$2.fromJson(CollectionJsonAdapter.java:53)
at com.squareup.moshi.JsonAdapter$2.fromJson(JsonAdapter.java:128)
at com.squareup.moshi.JsonAdapter.fromJson(JsonAdapter.java:35)
at com.squareup.moshi.JsonAdapter.fromJson(JsonAdapter.java:39)—
You are receiving this because you commented.Reply to this email directly, view it on GitHub
https://github.com/square/moshi/issues/347#issuecomment-326811223, or mute
the thread
https://github.com/notifications/unsubscribe-auth/AAEEEepQ3TSFb1A_-sCga6fZiBDCp7OTks5sesPBgaJpZM4PISlu
.
No action for Moshi to take here.
If you think it's a bug in Moshi, a contained test case is the best way to show it.
Otherwise and in all likelihood, what Jake said.
thanks for the response, I got the point 👍
It was added on the Moshi factory, do you guys think is possible to add the same to the Moshi.Builder? Sometimes we receive the server as it is, broken and incomplete, like my dreams :'(
@desgraci Do you mean the lenient option?
static final JsonAdapter.Factory LENIENT_FACTORY = new JsonAdapter.Factory() {
@Override
public JsonAdapter<?> create(Type type, Set<? extends Annotation> annotations, Moshi moshi) {
return moshi.nextAdapter(this, type, annotations).lenient();
}
};
Install this as your first factory on your Moshi.Builder.
Or, if you are using Retrofit, MoshiConverterFactory.create(moshi).asLenient() will create lenient adapters.
Most helpful comment
@desgraci Do you mean the lenient option?
Install this as your first factory on your Moshi.Builder.
Or, if you are using Retrofit,
MoshiConverterFactory.create(moshi).asLenient()will create lenient adapters.