It would be nice if you help me to call a simple API GET method using Retrofit. I have added the Gson and Retrofit jar file to the build path.
Here is the interface
:
public interface MyInterface {
@GET("/my_api/shop_list")
Response getMyThing(@Query("mid") String param1);
}
I am getting results only (in log cat) if i call the following in AsyncTask else i am getting NetworkOrMainThreadException
How should i call this?
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint("http://IP:Port/")
.setLogLevel(RestAdapter.LogLevel.FULL).build();
MyInterface service = restAdapter
.create(MyInterface.class);
mResponse = service.getMyThing("455744");
return null;
}
This means your @GET or @DELETE should not have @Body parameter. You can use query type url or path type url or Query Map to fulfill your need. Else you can use other method annotation.
Approach. You will have to create a Request interceptor ( BasicAuthInterceptor ) which extends Interceptor class of OkHttp library. Then, override intercept function and add your credentials into the request. Generate basic credentials with Credentials class of package OkHttp by using its basic function.
Retrofit is a REST Client for Java and Android allowing to retrieve and upload JSON (or other structured data) via a REST based You can configure which converters are used for the data serialization, example GSON for JSON.
Retrofit gives you the option of synchronous and asynchronous. Depending on how you declare your interface method, it will either be synchronous or asynchronous.
public interface MyInterface {
// Synchronous declaration
@GET("/my_api/shop_list")
Response getMyThing1(@Query("mid") String param1);
// Asynchronous declaration
@GET("/my_api/shop_list")
void getMyThing2(@Query("mid") String param1, Callback<Response> callback);
}
If you declare your API synchronously then you'll responsible of executing it in a Thread
.
Please read the "SYNCHRONOUS VS. ASYNCHRONOUS VS. OBSERVABLE" section on Retrofit's website. This will explain to you the basics on how to declare your APIs for your different needs.
The simplest way of getting access to your JSON class object is to map it to a Java object and let Retrofit do the conversion for you.
If for example the returned JSON for your rest api was
[{"id":1, "name":"item1"}, {"id":2, "name":"item2"}]
Then you could create a Java
classes like so
public class Item {
public final int id;
public final String name;
public Item(int id, String name) {
this.id = id;
this.name = name;
}
}
Then simply declare your api like so
@GET("/my_api/shop_list")
void getMyThing(@Query("mid") String param1, Callback<List<Item>> callback); // Asynchronous
And use it
api.getMyThing("your_param_here", new Callback<List<Item>>() {
@Override
public void success(List<Item> shopList, Response response) {
// accecss the items from you shop list here
}
@Override
public void failure(RetrofitError error) {
}
});
Based on the JSON you've provided in the comments you would have do to something like this
public class MyThingResponse {
public InnerResponse response;
}
public class InnerResponse {
public String message;
public String status;
public List<Item> shop_list;
}
This is kind of ugly but it's because of the JSON. My recommendation would be to simplify your JSON by removing the "response" inner object if you can, like so
{
"message": "Shops shown",
"status": 1,
"shop_list": [
{
"id": "1",
"city_id": "1",
"city_name": "cbe",
"store_name": "s"
}
]
}
Then your POJO could then become simpler like so
public class MyThingResponse {
public String message;
public String status;
public List<Item> shop_list;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With