Reputation: 1943
Hello I have a json structure and I need to get the datas from url. How can I do that? What are classes and functions. Please help me. Thanks.
here is my json.
{
nodes: [
{
node: {
title: "...",
field_news_image: [
{title="..."},
{title="..."},
{title="..."},
{title="..."},
{title="..."},
{title="..."},
{title="..."}
],
body: "...",
field_category: "...",
created: "..."
}
},
{
node: {
title: "...",
field_news_image: [
{title="..."},
{title="..."},
{title="..."},
{title="..."},
{title="..."}
],
body: "...",
field_category: "...",
created: "..."
}
}
]
}
Upvotes: 0
Views: 478
Reputation: 1010
http://www.jsonschema2pojo.org/
Put your JSON response here and download java code from this website . Then put this java code into your project and use it . This website will make all required classes with all required fields which will be perfect fit for your response of JSON. I have used it many time this will surely work.
http://square.github.io/retrofit/ here you can get detailed information about how to use retrofit to POST or GET data .
Retrofit turns your HTTP API into a Java interface.
public interface YourService {
@GET("users/{user}/repos")
Call<List<Repository>> listRepos(@Path("user") String user);
}
The Retrofit class generates an implementation of the interface.
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://api.yourAPI.com/")
.build();
YourService service = retrofit.create(YourService .class);
Call<List<Repository>> repos = service.listRepos("myrepository");
Use annotations to describe the HTTP request:
Upvotes: 1
Reputation: 3282
Use jsonschema2pojo.org Paste you responce and it will generate Java classes for you. You instantiate Retrofit instance by setting all necessary components like this:
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BuildConfig.BASE_URL)
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.build();
and your interfaces will look like:
@GET("api/service/schedule/{filial}")
Observable<Response<List<Event>>> getSchedule(@Path("filial") String filial);
where @GET- annotation defining request type, Response<> - type used by Retrofit, carrying information whether response is successful or not (see class methods). Observable<> - type from rxJava library, allowing to process request in reactive way. I strongly recommend to use RxJava, because it simplifies execution in background very much. (DON't use AsyncTasks!!).
in order to use Retrofit2 add following lines to your Gradle file:
//Reactive programming
compile 'io.reactivex:rxjava:1.1.5'
compile 'io.reactivex:rxandroid:1.2.0'
compile 'com.google.code.gson:gson:2.4'
//retrofit and okhttp
compile 'com.squareup.okhttp3:okhttp:3.2.0'
compile 'com.squareup.retrofit2:retrofit:2.0.2'
compile 'com.squareup.retrofit2:converter-gson:2.0.2'
compile 'com.squareup.retrofit2:adapter-rxjava:2.0.2'
You will instantiate Retrofit2 interface by a similar call: retrofit.create(Api.class)
Upvotes: 1