I'm using Retrofit
to collect and parse JSON
data I created and uploaded to the internet. I can successfully display all the data, but being new to Retrofit I'm struggling to understand how to query and display certain items from the JSON
data.
I manage to display all the JSON
data using the interface
:
@GET("d6jww")
Call<List<RetrofitVariables>> findPosts();
in the onResponse()
method of Retrofit
. But what if I only want to display, let's say, the names, or the id of the JSON
object. How do I query that?
I tried:
@GET("d6jww")
Call<List<RetrofitVariables>> getId(
@Query( "id" )
String id);
and:
@GET("d6jww")
Call<List<RetrofitVariables>> getId(
@Query("SELECT * FROM id")
String id);
But in my ViewModel
Android-Studio
wants me to add an argument when using the interface
, and I honestly don't know how to use it:
public Call<List<RetrofitVariables>> getRepositoryId() {
return this.repository.getRetrofitRepository().getId( ??? );
}
My JSON
looks like this:
[
{"id":231, "name": "Bob", "date":"3/13/2015",
"from":"8:00","until":"13:00"},
{"id":232, "name": "Joe", "date":"1/3/2015",
"from":"12.30","until":"13:00"}
]
To summarise my questions:
Can I query the
JSON
directly or do I first need to put it inRoom
and query it from there?If I can query
JSON
directly, how do I structure the interface (to collect the names, or id)? And what's with the missing argument?How do I query specific names? Example, if I want to query whether Bob is in the JSON data, how do I set up that interface?
Thanks a bunch in advance :)
This is not exactly what you would like to do but anyway.
For this:
- If I can query JSON directly, how do I structure the interface (to collect the names, or id)? And what's with the missing argument?
and comment:
So, there is no (good) way to take certain parts directly from the JSON data then?
You cannot affect what the service returns but you can choose what you pick. Say you have this kind of DAO normally (the full Post -or is it Person?- data):
public class Post {
private String id;
private String name;
private String date;
private String from;
private String until;
}
and normally you might have something like (not necessarli but as an example):
@GET("d6jww")
Call<List<Post>> findPosts();
To restrict data to only some fields you can declare a new DAO for it. like:
@Getter @Setter
public class PostId {
private String id;
}
and a new API method pointing the same endpoint:
@GET("d6jww")
Call<List<PostId>> getPostIds();
But anyway you need to do the filtering on the client side.