I wondering if anyone can offer some suggestions on where I should be putting API Rest calling code. I of course could put this in every single AsyncTask that I am using. Which works but it doesn't make it very usable.
The other way I thought was creating an ApiConsumer class that would have all the rest calls list "getList()", "getExpiredItems()", "deleteItem(int id)" etc.
I though this would be ideal so they would be able to be used from everywhere. Inside these methods I am still using HttpUrlConnection to make network calls so I would still need an AsyncTask but instead of implementing the HttpUrlConnection stuff inside the AsyncTask then I would call the "ApiConsumer" methods that I setup before.
In my mind this seems a logical way of going, is it?
Am I trying to do something that has already has a different solution?
Anyone know of any articles or examples outlining similar solutions as I can't seem to find one.
I know placing HttpUrlConnection code directly inside the AsyncTask is ok, but I just don't see it very reusable and wanted to abstract it out.
Retrofit is something you should look at, it pretty easy to integrate, will save lots of development time.
Easy syntax,Support annotations
In built GSON deserializer
Customizable according to your needs
Content format Agnostic
Lots of devs are using it, so you will get good support over web
Volley & AsyncHttp are also good alternatives, you will find plenty of such libraries.
P.S: I am using retrofit for most of my projects
Related
Ok so, I know the question is pretty much google-able and I did google it and found out a few answers ,so I am not asking this question knowing completly nothing.
So, I have an application (Cannot specify much about what it does or is or so...) and I need to get some data from a database.
As far as I know, the Volley is the best way to go ,but I am still looking for more details.
Could anyone tell me which is the best way to go?
NOTE: I am NOT looking for code,I am looking for methods.A name would sufice ,as I can do the digging myself.Eventually links to documentations would be awesome, but again, I can find documentations.I just need to know which is the fastest way and the most optimized.
Based on your comment, it looks like there are two different aspects to your application:
Sending out the HTTP requests from your client- i.e. your android app
Processing and sending the response to these requests on the server side, by setting up a web service
Looks like you have part 1 figured out. Note that, volley is a library, similar to android HTTP library, but optimized for faster networking.
For part 2, unless you're planning to use embedded database like sqlite, you'd need to learn about writing web services to provide database access.
Spring Data JPA is one of the many ways to do it.
I hear Parse is great too, but not free.
I'm currently using what I assume is a common pattern in Android, of fetching data from a network using various AsyncTasks, and updating a simple ArrayAdapter on completion using an interface callback mechanism.
cwac-endless is reasonably easy to plug in to add pagination type scrolling, aside from the fundamental issue that it assumes it will handle running the background task for you. Does this mean I basically have to rip up all my AsyncTask classes and associated interfaces, and move all the code from doInBackground into my EndlessAdapter's cacheInBackground?
Most solutions I've tried seem to end up duplicating much of the code already in cwac-endless, so I feel there must be cleaner solution to using this adapter with an existing AsyncTask?
cwac-endless is reasonably easy to plug in to add pagination type scrolling, aside from the fundamental issue that it assumes it will handle running the background task for you
Handling "running the background task for you" is most of the point behind the adapter.
Does this mean I basically have to rip up all my AsyncTask classes and associated interfaces, and move all the code from doInBackground into my EndlessAdapter's cacheInBackground?
If you wish to use EndlessAdapter as it stands, yes.
You are certainly welcome to grab the code, modify getView() to not use its own executeAsyncTask and AppendTask and replace that logic with your own. However, pretty much every line of code in AppendTask is necessary for EndlessAdapter to work. And I have no idea how any endless construct would work with "custom AsyncTasks" plural, as there has to be a clear end to the work so that we know to show the new rows in the list and get rid of the pending View. Hence, you would still need to designate some AsyncTask of yours as playing the role of AppendTask, doing everything that you are presently doing and all of the logic in AppendTask. Whether that is simpler than just using the existing EndlessAdapter code base, I cannot say, as I do not know your code.
I feel there must be cleaner solution to using this adapter with custom AsyncTasks?
You are the first person to ever raise the issue, and hence I have never considered it prior to typing in this answer. I will give it some thought and may try to do some refactoring to help with your scenario.
I'm looking to establish some "best practices" for Android, with regards to code reuse, ease of programming/understanding, performance and memory. So, you know, just good code all around.
It seems like a lot of the Android documentation and object design pushes you towards having lots of inner classes. AsyncTask likes to load data right into Views. Adaptors like to have access to the LayoutInflator.
From a code reuse viewpoint, it would be nice to build a few adaptors or AsyncTasks that solve your problems, and reuse them as needed. This means passing around your context though, which feels dirty and can lead to memory pitfalls if not done carefully. The other option is to bake all the AsyncTasks and Adaptors that an activity needs directly into the Activity. This makes it hard to reuse code, but you can see where things are going easily and since you're using the context directly it's harder to hold onto things forever.
I'm also concerned about writing code that will look familiar to programmers we might hire in the future.
What are the coding standards for Android? What is the "best design" for an application which needs to load nearly all of it's data from the web, have a UI that works on phones and tablets (with different activities in each), and be easy to work with and extend for years to come?
You should look at this on How to code in android.
And you can use inner class or make a seprate class according to your need. For example when all data is being loaded from web in json format i always use a seprate class with a static method which will return the jsonObject and then i can call this method anywhere in my app and extract data from it.
Also i use single inner class of asyncTask perfoming different task in my activity like seraching and loading data in listview, loading data on user prefrences change.and so on.
In custom adapter i always prefer different class for them. It'll really make easy to work with them.
Hope it will help.
I've asked this before, and this is the way I do it.
If I'm going to use the adapter many times, I place all of my adapters into a package called "com.myapp.adapters" As for the AsyncTask, I always use asynctasks as part of activities.
If you only have a short adapter that does a little bit of work in an activity, there's no need to create a separate class file for it. Just stick it into the activity.
I am developing application for iPhone and android. In code i need to access the data from database(oracle) .
I am planning to use REST web services to return JSON data to devices. I dont know much about developing web servies(all i did is 'consumption' part).
Now the question came to my mind, i can still manage to return the JSON data from java servlets.
IS there any advantage of using web services. I know for sure I am missing something but want to know what is it.
Now the question came to my mind, i can still manage to return the JSON data from java servlets.
Yes, you can return JSON from java servlets. There are plenty of libraries out there for taking what ever data you've retrieved from you database and serializing it into JSON.
IS there any advantage of using web services. I know for sure I am missing something but want to know what is it.
This is a super general question, but yes. I think the best answer is that (among other things) it gives you an API that arbitrary clients can plug into. So you can expand to new platforms trivial. That's the main benefit as far I see it.
I have a WCF RESTful application I wrote based off of WcfRestContrib and hosting in iis7.5, which passes a pdf as a byte[]. I'm trying to figure out how, and what the best way would be, to consume this service in an Android application. I believe I took care of all the Java/WCF interop issues because the service is used by an outside company that uses java. But I don't see to many examples out there. I've read that I'm going to have to make a method that passes JSON instead XML, but otherwise I believe it should be usable for an Android app. I'm not seeing too many examples of Android apps that consume RESTful services, is this not the preferred method for Android?
I seen a couple articles (http://romenlaw.blogspot.com/2008/08/consuming-web-services-from-android.html) but most seem kind of old, though they still may be useful. I'm just curious how any experts out there would suggest I handle such a project, I can create another kind of service if need be. I just want to be able to load pdf's from a service based on some parameters. Security is a concern, but not mandatory at this stage. Service currently uses basic auth and is on a https.
Any thoughts? Suggestions?
While also adressing your security concernt, you could use an HttpsURLConnection and then use getInputStream() from that to put it in a bytearray using read().