• Post Reply Bookmark Topic Watch Topic
  • New Topic

Query a database with RetroFit  RSS feed

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I am currently trying to make an app. All I want it to do is query a database for information and display that information. Seems pretty simple right? Not if you are new to web services. :-/. What I am trying to do is use RetroFit to make that call since it does all the json parsing and puts it in a nice POJO for you. What i can't figure out is how to query the database using the Retrofit library.

Retrofit has a Query command but i am not sure if thats what i am looking for. I've looked at ton of tutorials on youtube and read online but i can't seem to get a straight answer. It could also be my lack of knowledge on the subject as well.

I've created the interface:
public interface LanIscAPI {

@GET("/feed/lanisc.json")
public void getFeed(Callback<List<DataObjects>> response);

Here is my implementation
private void requestData(String uri)
{

//RestAdapter is included in the RetroFit Lib
//We are goin to instantiate the object and pass in ENDPOINT constant then call the build method
RestAdapter adapter = new RestAdapter.Builder().setEndpoint(ENDPOINT).build();
LanIscAPI api = adapter.create(LanIscAPI.class); //Passing in class property of API interface
//defines where my web service is and how i want to call it. Bring in API interface you defined

api.getFeed(new Callback<List<DataObjects>>() { //Callback saying it is going to return a list of our dataobjects


//Retrofit does everything. Handles Async processing, Retrieves content from webservice which starts as a string
//It parses it as JSON and transforms it to list of objects (arg0)


@Override
public void success(List<DataObjects> arg0, Response arg1) {
// TODO Auto-generated method stub

dataList = arg0; //Since retrofit transforms our data all we need to do it place it in the list
updateDisplay();

}

@Override
public void failure(RetrofitError arg0) {
// TODO Auto-generated method stub

}
});


}

I ask that you please be patient with me as i am learning all of this for the first time. Sorry for all the comments. i use the comments to help me understand what is going on.

thanks
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if you are trying to create a app using retrofit then in starting you may faces many problems similar to me. its an easy thing to create but need complete information. We have a lot of network libraries that used to fetch and send the data from/to server.Retrofit is better alternative of other libraries in terms of performance, ease of use, extensibility and others.For network transactions we need to define Internet permission in our Manifest file.

For fetching response we need create POJO class that automatically parse the JSON data using Gson in background. We just need to create this POJO class. For creating POJO class first method is defining each and every variable by ourself and other best and fast method is using www.jsonschema2pojo.org/ platform. By using this platform you can easily convert your JSON response to POJO class, for this we need to copy our JSON response and paste it in jsonschema2pojo and it will create setter getter methods as per our requirement.

Read more about Retrofit http://abhiandroid.com/programming/retrofit
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!