in the json file i receive as respone it contains a key "continue" and to create the model object for the response i have to use "continue" as a parameter in the method.
Activity
public object Model {
data class ResultOfContinue(val continue: Continue)
}
json
"batchcomplete": "",
"continue": {
"sroffset": 10,
"continue": "-||"
},
If you really wanna use keyword as a parameter. You can use like this :
data class ResultOfContinue(val `continue`: Continue) {
}
Related
I have an ArrayList of type Course. Course has id ( string ), name ( string ).
I want to display a list of courses on one screen. Give the user option to select multiple courses which they have completed and send these courses to next activity.
I am able to MultiSelect courses in a RecyclerView. But unable to send the data to another activity.
you can convert the data to JSON string & pass easily between activities using intent bundles...
create extension functions like these
// convert safely from json to class
fun <T> String.fromSafeJson(classOfT: Class<T>): T? {
return try {
Gson().fromJson(this, classOfT)
} catch (e: Exception) {
null
}
}
// convert any object to json
fun Any.toJson(): String {
return Gson().toJson(this)
}
after that you can convert any list of any type using yourList.toJson(), send it via bundle and in next activity get it from bundle and parse using stringName.fromSafeJson(YourListType)
remember to add Gson library... use can use the following
implementation 'com.google.code.gson:gson:2.9.0'
Well, I've seen alot of boilerplate code in my model classes when I need to parse json using GSON with Retrofit2. I would like to find out how to deal with it, because I'm pretty sure there is a way to make this look more elegant.
{
"data": [
{
"id": 2,
"price": 56,
"name": "Hello"
}
]
}
For parsing this json I would need to create 2 model classes. One would be for the inner object (id, price, name) and one would be Data.class which holds one attribute - List of this inner object.
It's totally okay to have different inner objects inside, but later on you will have many "Data.class" which has one attribute "data" which is List, but with different inner object type. How can I avoid this boiler-plate Data lookalike classes in my projects?
What I want:
Is to NOT create new Data class with "data" attribute changing inner object type whenever I create new "inner" object model class.
I had this problem and fix that with create one abstract class with name BaseResponse like this
public abstract class BaseResponseInterface2<T> {
#SerializedName("data")
private List<T> data;
public List<T> getData() {
return data;
}
public void setData(List<T> data) {
this.data = data;
}
}
And use that like this in api service interface
#GET("/api/")
Call<BaseResponseInterface2<innerClass>> getResponse(
#Path("id") int id
);
Hope it help
For example i have json looks like:
{
"data": [
{
"name": "one"
},
{
"name": "two"
}
]
}
For example i have object User with field name.
Is it possible write method which will parse data array to objects User?
something like
Call<List<User>> getUsers(#KeyPath("data"))
Now to do this, i need create a wrapper class something like
public class UsersWrapper {
#SerializeName("data")
public ArrayList<User> users;
}
and in service i do next
public interface Service {
#GET("users")
Call<UsersWrapper> getUsers()
}
But my all requests is just response with data but variable objects in array.
In this case i need create wrappers to any data requests. Pain :(
?
I'd do it this way:
Global class Wrapper<T> to parse the whole JSON
public class Wrapper<T> {
public List<T> data;
}
And User to parse actual array;
public class User {
public String name;
}
Then, the API interface:
#GET("/people")
Wrapper<User> getUsers();
And in DataSource class just do something like this:
#Override
public List<User> getUsers() {
Wrapper<User> usersWrapper = myApiInterface.getUsers();
return usersWrapper.data;
}
Upd1:
Another solution is to create custom JsonDeserializer (like described here) for List<User> type, register by registerTypeAdapter it with your custom Gson object and then you can deserialise your Json directly into List<User>. Though, this solution brings much more extra code and potential benefit is unclear for me.
I have a problem pretty much the same as this: retrofit returning valid json but pojo is empty
But my variables are not declared as static. The are all declared like:
#SerializedName("name")
#Expose
private String name;
I have tried removing the annotations, but that doesn't work.
what could be the problem?
EDIT:
Interface:
#GET("/MyController/MyAction/{name}")
void getSomeData(#Path("name") String name, Callback<List<DataItem>> cb);
Can you show me the actual received data(JSON or XML)? It seems that your callback structure is not matching with your data. For example, it would be possible that your data may have array that have a name, and you ignored it.
In my case, I declared like this,
void getList(#Path("data") String data,//
Callback<OrderList> callback);
OrderList is:
public class OrderList {
List<Order> order_list;
}
And my data is:
{
"order_list":
[
{ "id": "1001", "data": "a" },
{ "id": "1002", "data": "b" }
]
}
I mean, it seems that your data may have nested structure and your class may not matching with that.
Using Retrofit for Android I'm successfully connecting to a web service with a login and password:
getService().doSignIn( m_editEmail.getText().toString(), m_editPwd.getText().toString(),new Callback<Object>() {
#Override
public void success(Object login, Response response) {
}
#Override
public void failure(RetrofitError error) {
}
});
You may ask why am I using Object and not a class? Well, I could not get any return data unless I used the generic Object class. I tried to create a class with the property names I expected to get back but couldn't get it to work (just empty values for the class's properties would be returned).
So now when I use Object I get data back but its in the LinkedTreeMap type like this:
root // this is of type com.google.json.internal.LinkedTreeMap
[0]
key
value // this is of type com.google.json.internal.LinkedTreeMap
[0] REsult Code - > 0
[1] Result Message -> success
[2] ...
So basically it is returning a LinkedTreeMap within a LinkedTreeMap ---How can I map this to a class with matching properties for easier retrieval?