What is the relationship between AsyncTask and Activity? [closed] - android

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I have been given an assignment to develop an application that send server request and get response and then using JSON parsing, display the data content into a ListView.
I don't understand about AsyncTask and how to integrate all classes. Hope you will accommodate.
regards

What should you do?
The first, send a request to server
The second, get response
The thirds, Parse data from InputStream which you got from Response
The fourth, show on ListView
Oh, done.
Right now,
Look into the first step.
How to send a request to server?
You can use HttpURLConnection or HttpClient
So, What's problem when you send a request to server?
I think you know when you send a request to server, you will get some problem: Network bad, InputStream from Server too large, ...
And how to resolve?
With single statement, you can't take along time to do. So with task which will takes along time to do, you have to handle in other thread. That's reason why we should use Thread or AsyncTask.
What's AsyncTask?
You can read more by search on Google. I just tell you: How to use AsyncTask to solve your spec.
What does AsyncTask do?
When you create an instance of AsyncTask,
It's will follow:
-> Create -> PreExecute -> Execute (DoInBackground) - PostExecute
Ok.
Right now, I will answer your question:
Create an object which extends AsyncTask.
public class DownloadFile extends AsyncTask<String, Void, InputStream> {
#Override
public void onPreExecute() {
// You can implement this method if you want to prepare something before start execute (Send request to server)
// Example, you can show Dialog, or something,...
}
#Override
public InputStream doInBackground(String... strings) {
// This is the important method in AsyncTask. You have to implements this method.
// Demo: Using HttpClient
InputStream mInputStream = null;
try {
String uri = strings[0];
HttpClient mClient = new DefaultHttpClient();
HttpGet mGet = new HttpGet(uri);
HttpResponse mResponse = mClient.execute(mGet);
// There are 2 methods: getStatusCode & getContent.
// I dont' remember exactly what are they. You can find in HttpResponse document.
mInputStream = mReponse.getEntity().getContent();
} catch (Exception e) {
Log.e("TAG", "error: " + e.getMessage());
}
return mInputStream;
}
#Override
public void onPostExecute(InputStream result) {
//After doInBackground, this method will be invoked if you implemented.
// You can do anything with the result which you get from Result.
}
}
Ok. Now we have to use this class
In your MainActivity or where you want to invoke this class, create an instance of this class
DownloadFile mDownloader = new DownloadFile();
mDownloader.execute("your_url");
Using method mDownloader.get(); to get InputStream if you want to get. But you have to surround by try-catch
I know, if you want to use Dialog, you will search on Google how to show Dialog while download file from server.
And I suggest you that you should remember, you nead runOnUiThread if you want to Update UI.
Because an AsyncTask is Thread. So you can not Update UI if you are in another Thread which is not MainThread.

AsyncTask
AsyncTask enables proper and easy use of the UI thread. It is used when you want to perform long awaited task in background.
It publish result on the UI thread(display result to the UI) without having to manipulate any threads or handlers.It means that user doesn’t bother about Thread management, everything is managed by itself. And thats why it is known as Painless Threading, see below point.
It is also known as Painless Threading.
The result of AsyncTask operation is published on UI thread. It has basically 4 methods to override: onPreExecute, doInBackground, onProgressUpdate and onPostExecute
Never expect to be a programmer by referring short notes, study deep..
Look here for more detail.

Related

Get value for volley request to finish before proceeding with method? Android Java

its my first time asking a question here, so please tell me if I missed to include something.
I have a method that prints returns a String. Inside it, is an asychronous volley request that retrieves a specific value to be added to the String. The problem is that the request is made, but since it's an asynchronous request, the method still proceeds and returns the String even if it still doesn't have the value from the request. The request completes afterwards. So the question is, is there a way to make the method wait for a specific variable to have a value before proceeding, without freezing the UI? Should I create a new thread instead?
Code below is not the actual codes I have but it shows the outline.
public String getStr{
StringBuilder sb = new StringBuilder();
String strOne;
// code here..
// asynchronous request here..
// code here..
sb.append(strOne)
// code here..
return sb.toString();
}
The asychronous request refers to the volley request.
Any help is appreciated. Thanks in advance!
You should handle the response in onResponse (#Override) method. Take a look in this tutorial:
Asynchronous HTTP Requests in Android Using Volley
If you want to return the response to another class to handle it there, I suggest you to implement an interface. I could post the code for this here, but I aswered a question like this here:
Volley , Wait for response to return wait until use flag - answer

Recommended way / order to read data from a webservice, parse that data and insert it in a SQLite db

I'm one to start mentioning that I'm totally new to Android, I've just finished reading a quick, introductory book and now I have to implement my very first app.
This app is going to be use to take orders. Among all the data I'm going to store in a local db, two tables are the most important: Customers and Articles, being the latter the largest of all the tables (aprox 20000 records)
One of the main process in my app, fetches all the data that my app need to work off-line when the user press a button that starts the daily operations on the device.
Well, the process consists of the following steps :
a. Read a restful service to retrieve the Customers Data
b. Parse the response to Json Objects
c. Insert those customers to the Customers Table
d. Read a restful service to retrieve the Articles Data
e. Parse the response to Json Objects
f. Insert those articles to the Articles Table
This is what I've planned to do:
Write a helper class that handles all the HTTP GET requests. Then call this class whenever I need to download all the Customers and Articles data
Since all this process might take a lot of time, I need to do it the background. So based on some suggestions I'm going to put all this code inside a Bound Service.
While all this long processing is taking place in the background I'll have to show some sort of indicator (a ProgressDialog) This is the reason I opted for using a Bound Service
Though I think I've got the general idea of how to do most of these thing separately, I think that putting the all together is quite a different story.
So these are the questions I've got now that I have to put the puzzle together:
Do you think the order in which I'm executing all the 6 steps of the process described is correct / efficient? If you had to make some changes, what would you change?
If the activity that started the service is explicitly cancelled or is hidden by another activity, the service has to have a way to let the user know that the process has finished. How could I implement that?
Is it possible/ recommended to write to the SQLite DB within the service? Is it the same as when I do so within an activity?
In J2ME I've done something similar, and when I put something like the 6 steps I mentioned above all of them are executed sequentially, that is , one after the other. Is it the same in Android?
I hope I'm not asking too many questions. I just put them together because they are all related.
Basically in this question I'm not asking for working code ( though it'd be OK if you could provide some sample code) What I'm really after is some suggestions, some guidance. Something like "Hey, I think this might help you with point number 3" or "You might find this article useful", "I think you'd better off using this instead of that". That kind of thing.
I decided to come to you because you're the experts and I really need someone to put me in the right direction.
Thank you very much.
P.S. Please do not close this question, if your think I need to change something just let me know and I'll do it.
I decided to come to you because you're the experts
first i am not expert and also i am not knowledgeable you can find more expert people than me but this is my opinion hope to give you some help.
first of all forget to use AsyncTask to download because it must be used for short background jobs not like yours i think the amount of file you want to download is pretty large.(i think so)
check downloadmanager of google to see how it works it may help you.
http://developer.android.com/reference/android/app/DownloadManager.html
http://blog.vogella.com/2011/06/14/android-downloadmanager-example/
if you want to use service use unbound service because you do not want the service to be destroyed by android or user when the user close the apps do you? i think you want to get your data any way.
in order to be efficient i recommend these steps:
a. Read a restful service to retrieve the Customers Data
b. when you get Customer data do :
create another service or thread to Read a restful service to retrieve the Articles Data
Parse the response to Json Objects on your old service or thread
now you have 2 services or threads that run concurrently so follow the steps that obvious insert parse and so, on each service or thread.
why do not i combine a and d? because i think user do not like to wait much time behind download progress bar.
in order to insert your data to database use transaction and i recommend you use:
http://greendao-orm.com/
it is more efficient ORM than others for database and you get free from db implementation.
If the activity that started the service is explicitly cancelled or is hidden by another activity, the service has to have a way to let the user know that the process has finished. How could I implement that?
use notification:
http://developer.android.com/training/notify-user/build-notification.html
http://www.tutorialspoint.com/android/android_notifications.htm
http://www.vogella.com/tutorials/AndroidNotifications/article.html
While all this long processing is taking place in the background I'll have to show some sort of indicator (a ProgressDialog) This is the reason I opted for using a Bound Service`
how can I update the UI from an Unbound Service?`
Use a LocalBroadCastManager, or in general BroadCastReciever
Android update activity UI from service
In J2ME I've done something similar, and when I put something like the 6 steps I mentioned above all of them are executed sequentially, that is , one after the other. Is it the same in Android?
this is depends on your steps, if you follow my idea you run concurrently and if you run your idea you will run sequentially.
Good Luck.
I did something like yours.
In first step I get data from webservice in HTTP GET of POST method using AsyncTask like this:
public class GetService extends AsyncTask<String, String, String> {
private String mRestUrl;
private ServiceCallback mCallback;
private final HttpClient Client = new DefaultHttpClient();
private String Content;
private String url;
private String Error;
private ProgressDialog barProgressDialog;
private ProgressDialog Dialog;
public GetService(String restUrl, ServiceCallback callback) {
this.mRestUrl = restUrl;
this.mCallback = callback;
this.url = restUrl;
Dialog = new ProgressDialog(AppContext.CurrentContext);
}
#Override
protected void onPreExecute() {
super.onPreExecute();
}
#Override
protected String doInBackground(String... urls) {
Content = null;
BufferedReader reader = null;
try {
StringBuilder builder = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(this.url);
HttpResponse response = client.execute(get);
int status = response.getStatusLine().getStatusCode();
if (status == 200) // sucess
{
HttpEntity e = response.getEntity();
// String data = EntityUtils.toString(e);
InputStream content = e.getContent();
reader = new BufferedReader(new InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null) {
builder.append(line);
}
Content = builder.toString();
} else if (status == 401) {
return "-Auth Failed Error Code 400";
} else {
return "-Error Code: " + status;
}
} catch (Exception ex) {
Error = ex.getMessage();
} finally {
Dialog.dismiss();
try {
reader.close();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
return Content;
}
#Override
protected void onPostExecute(String result) {
try {
GetService.this.get(20000, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
}
mCallback.onTaskComplete(result);
super.onPostExecute(result);
}
}
and my callback class is:
public abstract class ServiceCallback{
public abstract void onTaskComplete(String result);
}
I call AsyncTask in my code everywhere I want to get data from webservice:
new GetService(url, new ServiceCallback() {
public void onTaskComplete(String response) {
// Parse Response of WebService
}
}).execute();
In second step I Parse response of WebService in onTaskComplete method using json helper Libraries like Gson or Jackson. for example in jackson:
List<YourClass> data = new ObjectMapper()
.readValue(
response,
new TypeReference<List<YourClass>>() {
});
At the end I store Data in Database. for connecting to DB I Prefer to use GreenDao as my ORM. In this way storin data in DB can be done in one line code like this:
//after converting json to object
YourORMDaoClass.insertOrReplaceInTx(data);
To Use GreenDao ORM this link is very helpful;
Do you think the order in which I'm executing all the 6 steps of the
process described is correct / efficient? If you had to make some
changes, what would you change?
It depends. If this data is related and cannot exists without each other then you should change the order like this:
a. Read a restful service to retrieve the Customers Data
b. Parse the response to Json Objects
d. Read a restful service to retrieve the Articles Data
e. Parse the response to Json Objects
c. Insert those customers to the Customers Table
f. Insert those articles to the Articles Table
Steps c and f should be combined in transaction.
Otherwise, the order does not matter. If data is not related then separating these processes and running them in sequence might be a good idea.
If the activity that started the service is explicitly cancelled or is
hidden by another activity, the service has to have a way to let the
user know that the process has finished. How could I implement that?
I suggest to start with implementation of the IntentService class. It handles for you background thread and works like a queue of events where single Intent delivers a data to process.
Actually you could implement one of the patterns presented by Google on one of their IO conferences. I have implemented an option A shown on the video. It works for me really well. The trick is that using ContentProvider from the background automatically updates UI which listen for changes thanks to CursorAdapter.
To update UI progress you can use LocalBroadcastManager or event bus libraries e.g. Otto.
You can also extend your tables and store status and progress, updating tables would automatically update UI as well, just keep in mind that these updates should be rare e.g. control in the service background how often table progress is updated calculating the progress first and checking with service local variable if it's changed.
In case your app is in the background, post status notification. User should be able to navigate back to your app clicking on the notification.
Is it possible/ recommended to write to the SQLite DB within the
service? Is it the same as when I do so within an activity?
You can do it within the Service. Actually, if you follow the pattern I have mentioned above, you would do it in the Processor tier on the background service thread.
In J2ME I've done something similar, and when I put something like the
6 steps I mentioned above all of them are executed sequentially, that
is , one after the other. Is it the same in Android?
It's completely up to you how communication with the server will work. If you decide to use IntentService class then it will work in a sequence which is not a bad idea on Android. On the other hand you may extend Service class directly and implement own thread executor with a thread pool. You can also have dedicated IntentService classes for unrelated operations.
I also recommend to read lessons:
Transferring Data Without Draining the Battery
Transferring Data Using Sync Adapters
If you don't want to play directly with HTTP connection implementation then consider using Retrofit or Volley
If you just need JSON parsers then these 2 are the best:
GSON
Jackson

Best practice for threaded getter method

I also faced with the NetworkOnMainThreadException in my application but I don't see how to resolve it.
I have a class with a getter method. Like:
public ArrayList<News> get(int i){
// get the list of news from a HTML on the net. The news are split up into web pages on the site
// and i is the page number
return NewsParser(i);
}
Since Android throws the exception I come up with an idea of a downloader class which downloads the HTML content in a separate thread
pubic ArrayList<News> get(int i){
Downloader dl = new Downloader(i);
String HTMLcontent = dl.getContent(); <-- AsyncTask starts in getContent()
return NewsParser(HTMLcontent); <-- What happens here in the main thread???
}
Any ideas/best practices for this problem?
Just looking at your code and your question, it seems like you don't have a very solid understanding of how AsyncTask (or threads in general) works.
I would recommend reading this article.
Basically, your AsyncTask should query the web URL and download the data. Once the data is complete, your AsyncTask should send the HTMLContent to a handler object. The handler will be running on your main thread, so you can display the information to the user at that point.
You shouldn't be calling
dl.getContent();
to retrieve the content. AsyncTask runs on a separate thread, so you can't just call methods like this from your main thread. You need to create the Downloader object (like you did) and then call
dl.execute();
to start the AsyncTask.
run the get method inside a thread,
new Thread(new Runnable() {
#Override
public void run() {
// call get method here
}
}).start();
Since Honeycomb (Android 3.0) you can't use Networking Operations in the MainThread to avoid freezes on the Phone. This is important in order to make your app responsive.
More info:
NetworkOnMainThreadException
Responsiveness

How to cancel DefaulHttpClient execution process in multithread

I am developing an android application that rely very much on internet, I retrieve data frequently using this RestClient Class, that wrap some detail on using DefaultHttpClient to do network request.
And I always use different thread to do the HTTP request, I create a class like this:
public class AsyncWorker {
final String SERVER_URL = "http://api.blabla.com";
RestClient client = new RestClient();
public void requestHttp(final String url, final ArrayList<NameValuePair> params, final RequestListener listener) {
new Thread(new Runnable() {
public void run() {
try {
client.setUrl(url);
client.setParams(params);
client.Execute(RestClient.RequestMethod.POST);
String response = client.getResponse();
listener.onComplete(response);
} catch (Exception ex) {
Log.d("LOGIN", ex.getMessage());
}
}
}).start();
}
etc...
So whenever I need to do a HTTP request, I only need to create AsyncWorker object, and provide the RequestListener callback interface.
But the problem is, how can I cancel the HTTP Request when the user press the back/cancel button? and in this case the application still is in one activity, for example I create a dialog, and the user do a request from that dialog, and then back button pressed, the dialog dismissed, and I need to cancel the request on that time.
I had the same issue and was able to find a fix. Here is what I did:
I used CloseableHttpClient along with other related classes, instead of the DefaultHttpClient that by default comes with Android.
These classes are from https://hc.apache.org/downloads.cgi. OR for direcet access: http://apache.mirrors.hoobly.com//httpcomponents/httpclient/binary/httpcomponents-client-4.3.2-bin.tar.gz
With this, calling the abort() method on the Request object will actually halt the connection. However, using this library is not the solution; reason being that Android already has the outdated HTTPCLIENT library inbuilt, and most classes in the library pointed to by the above link would appear to be missing at runtime.
The problem is that both the packages in the above library and the inbuilt org.apache httpclient package have same namespace, and would result in the use of only the inbuilt org.apache classes provided by Android at compilation.
An example of this issue is found here: java.lang.NoSuchFieldError: org.apache.http.message.BasicLineFormatter.INSTANCE.
Thanks to the guys who provided http://code.google.com/p/httpclientandroidlib/ as an option (found in the answer section of java.lang.NoSuchFieldError: org.apache.http.message.BasicLineFormatter.INSTANCE)
Recommendation: one place to actually cancel an http request could be within OnCancel Listener of a progress dialog, instead of the AyncTask's onCancelled() callback method.
The RestClient object your using doesn't expose any interrupt() method of DefaultHttpClient (which is the backing object doing most of the work). Not a problem - the DefaultHttpClient doesn't seem to have any interrupt or abort functionality to expose in the first place.
So, your left with a blocking operation on client.Execute().
Your half way to having a solution - which is to put the blocking operation into a Thread. Where your falling down is your architecture - your using a Thread/Listener setup which doesn't give you alot of wiggle room.
Try switching your anonymous Thread to an AsyncTask. This won't solve the problem of you're client.Execute() from blocking but will allow you to throw away the listener (replacing it with onProgressUpdate() or onPostExecute()).
What this will do is allow you call task.cancel(), signalling to the Task it is no longer needed. This will allow you to reassign a new AsyncTask, orphaning the cancelled task, the orphaned thread will then finish quickly as its able and die quietly while the rest of your application gets on with what it needs to.
((On an unrelated note, "Execute()" is a method and shouldn't be capitalised))
I suggest you take a look at the ClientConnectionManager interface. This allows you to do stuff like releasing a connection, shutting down a connection etc. You may need to enhance the implementation of RestClient though - since your RestClient does not expose the underlying DefaultHttpClient object (from which you can get to the ClientConnectionManager using the getClientConnectionManager() method).
Use the threading primitives. Have a flag running and set it to true initially. Do your REST requests in a while(running) loop.
In your onPause(), set the running flag to false.

Need help with AsyncTasks - Android Application

Hey, I have an application which logs onto a few sites using defaulthttpclient and I've found I'm going to need to use the AsyncTask as the requests hold up the UI thread. In my code, I create an instance of a state class i.e. State state = new O2State(); with different states for different sites.
I then call state.logon(String username, String password); which returns a string containing details of the result so:
String result = state.logon(username, password);
I've been trying to implement asynctasks to run this code in another thread and return the string back to the UI thread on completion. The idea is I will display a progress dialog, run the thread, and on complete, will display a dialog with the result.
I've been looking at this example:
public void onClick(View v) {
new DownloadImageTask().execute("http://example.com/image.png");
}
private class DownloadImageTask extends AsyncTask {
protected Bitmap doInBackground(String... urls) {
return loadImageFromNetwork(urls[0]);
}
protected void onPostExecute(Bitmap result) {
mImageView.setImageBitmap(result);
}
}
Where I'm stuck is:
I don't think I'll need any arguments, but doinbackground seems to require a list of parameters. I'm also unfamiliar with this time of method argument declaration.
Secondly:
I'm not sure how to return the resulting string when the thread is finished executing. Should I just create a "DoThisWhenTheThreadIsFinished(String result)" and call this from onPostExecute?
Anyway, I hope this isn't too confusing to read and I'd really appreciate any help you can offer.
Thanks
Where you don't need parameters just specify the type (e.g. String) and ignore it, or you could use the Void class (note the capital V).
What you suggest for how to return control back to the UI thread to reflect the update is a good approach. i.e. in onPostExecute() call a method on the activity to update the UI.
As a general rule if any operations will take more than a couple of hundred milliseconds, use a separate thread. You may also want to use a rotating progress indicator to show the app is doing something.
(when people answer your questions, always rate the ones you like, and pick one as the "best" answer. you get points doing this, and it helps others later).

Categories

Resources