AsyncTask's doInBackground(Params... params) - android

I'm unfamiliar with the parameter syntax in doInBackground(Params... params)
What is this type of thing called, and how do I utilize it?

As devA and VVV have said, that is called "varargs". Effectively, the following two lines of code are equivalent:
public void makeLemonade(String[] args) {
and
public void makeLemonade(String... args) {
the code inside the method would be the same, but when it was called, they would be called differently. The first would need to be called like this:
makeLemonade(new String[]{"lemon1", "lemon2", "lemon3"});
while the second one's method signature could have 0 to (an assumed)infinite number of arguments, but they would all need to be String arguments. All of the following calls would work:
makeLemonade("lemon1");
makeLemonade("lemon4", "lemon7", "lemon11", "lemon12");
makeLemonade();
// ... etc ...
A subtle difference between the two is that you can call makeLemonade() legally here if you're using varargs.

They are called Var Args.. Just like an array.. U can pass multiple items and just access like params[0].. etc..

Params... params represents a vararg. It was added in J2SE5.0.
It means you can pass any number of arguments or you can say array of arguments params[0]
Thanks
Deepak

Related

AsyncTask Params Object parameter

I know how to handle the asynctask but i have some doubt now. Pls check my aysnctask code below
public class MyAsynTask extends AsyncTask<Object, Object, Object> {
#Override
protected Object doInBackground(Object... params) {
Log.d("Main params ", "" + params[0] );
return null;
}
}
There is no issues in code and it's running. my doubt is, when i am using different parameter in execute method i am getting result. Pls refer the code below
new MyAsynTask().execute("Welcome");
new MyAsynTask().execute(10);
new MyAsynTask().execute(10.5f);
new MyAsynTask().execute(11.12d);
here am passing different types of value but my doInBackground method have object type parameter. While am passing different values how the doInBackground gives me a result. Pls explain if anyone knows.
Thanks in advance
As you know object is the super class in java so in doinBackgroung after getting params[0] you should type cast after getting that value like below
//this is for string case
String stringValue = params[0].toString();
Every time you past the parameter to doInBackground compiler casts it to object type. Every primitive type int, float, bool... have object type analogue Integer, Float, Boolean... And in this case compiler will cast it to object type analogues at first, so it would be safely cast to Object type later. Object type is superclass for all object types (it is a root for class hierarchy) and it have .toString() function, but .toString is override in child classes (Integer, Float...). And according to inheritance rules child function will be called. So integer, float... will be correctly translate to strings and print in Log.

How to create Observable for async response?

I have one async-method, like this:
void getPlaceInfo(Place place, PlaceCallback callback)
For example my PlaceCallback has one method:
void success(InfoPlace place);
I want create Observable for waiting response from two requests:
getPlaceInfo(...); // first call
getPlaceInfo(...); // second call
And then I want to get both response at the same time.
Can I make it?
So you need to combine 2 callbacks to evaluate a function like:
Response computeResponse(InfoPlace infoPlace, InfoPlace infoPlace2) {
...
}
And you want to use Rx for this. There is two problem here; wrapping the callback method into Observable, and combine them.
You can't just instantiate an Observable and call myObservable.send(value) or myObservable.next(value). The first solution is to use a Subject. Another solution (the one below) is to create the observable with Observable.create(). You call the callback method and create the listener inside the method Observable.create(), because it's inside Observable.create() that you can call onSuccess() method, the method who told the Observable to pass down a value:
Single<InfoPlace> getInfoPlaceSingle(Place place) {
return Single.create(e -> getPlaceInfo(place, new PlaceCallback() {
#Override
public void success(InfoPlace infoPlace) {
e.onSuccess(infoPlace);
}
}));
}
Or with Java 8:
Single<InfoPlace> getInfoPlaceSingle(Place place) {
return Single.create(e -> getPlaceInfo(place, e::onSuccess));
}
Note, I used Single over Observable, since you await only one value. The interest is a more expressive contract for your function.
Wrapping things into Observable is a pain, but now you are in the reactive realm where Rx provide you a complete toolbox to deal with them. The second part - combining the observables - is easy:
Single.zip(getInfoPlaceSingle(place1), getInfoPlaceSingle(place2), this::computeResponse);
Wrap your async calls using Observable.fromEmitter() and you can then use Observable.zip() to combine the calls.

Convert Async task params

I have a method inside AsyncTask class which I want to reuse. But it uses the object of type String... url which is accessed as url[0],url[1]
To reuse this function I need create a object of Type String... or
convert String... to String[]
I did not find any documentation for String...
#Override
protected String doInBackground(String... url) {
Thread.currentThread().setName("MetaDataDownloader");
String jsonStr=metadataDownloader(url);
//this uses param as url[0] and so on
}
That is because AsyncTask by design could receive more than one parameter (hence the varargs...).
Change your method like this:
#Override
protected String doInBackground(String... urls) {
Thread.currentThread().setName("MetaDataDownloader");
// if you want to handle all URLs
if (urls != null) {
for (String url : urls) {
String jsonStr=metadataDownloader(url);
}
}
}
You can create constructor in async task and pass what argument you would like to . Do not use parameters in doInBackground.
You can look at this Java Document. It's arbitrary number of arguments.
ou can use a construct called varargs to pass an arbitrary number of values to a method. You use varargs when you don't know how many of a particular type of argument will be passed to the method. It's a shortcut to creating an array manually (the previous method could have used varargs rather than an array).
To use varargs, you follow the type of the last parameter by an ellipsis (three dots, ...), then a space, and the parameter name. The method can then be called with any number of that parameter, including none.
--> to use your doInBackground(String... url), just treat it as array of parameters and cast it to whatever you like.

How can I overwrite the data in an element of an array of structures?

So, I have this structure:
typedef struct{
int serialNumber;
char name[100];
float price;
int quantity;
}Products;
And I created an array of structures dynamically.
The task was to 'simulate' a grocery store, with the user able to add and edit the items sold by the store. The following code snippets are for editing structure data.
void overwrite(Products store){
printf("Enter new serial number: ");
scanf("%d", &(store.serialNumber));
getchar();
printf("Enter new product name: ");
fgets(store.name, 100, stdin);
store.name[strlen(store.name)-1]='\0';
printf("Enter new product price: ");
scanf("%f", &(store.price));
printf("Enter new product quantity: ");
scanf("%d", &(store.quantity));
}
void editData(Products *store, int storeCapacity){ //storeCapacity needed to invoke printData(), assume a working code for the function.
int choice;
printData(store, storeCapacity);
printf("Enter slot number of product here: ");
scanf("%d", &choice);
overwrite(store[choice]);
}
Here's the catch, even though this code works, when I try to print the data, the data displays the values which should be overwritten. Have I forgotten to do something? I wish you could help me.
BTW, I code on an Android phone.
void overwrite(Products store){
C is pass by value, you need to pass a pointer to Products (i.e., Products *store) and modify the overwrite call in editData accordingly.
Basically the problem is that in C you pass arguments by value. So when you specify this signature
void overwrite(Products store)
and you invoke it somewhere:
Products myStore;
overwrite(myStore);
what happens is that a copy of myStore is created and placed on the stack, then the value is passed to the function. This means that every modification done to the Products object inside overwrite applies on the passed copy, not on the original object. This copy is then discarded when exiting the scope of overwrite function.
To solve this problem you must pass a pointer to the object, that is passed by value but being an address will point to the exact same myStore object. This is done in this way:
void overwrite(Products* store)
{
..
scanf("%f", &store->price);
..
}
Products myStore;
overwrite(&myStore);
According to Ouah, I passed the structure as the value itself, which did happen in my code.
So what I did is...
void overwrite(Products * store){ //formal parameter changed into a pointer
//codes here
}
And...
overwrite(&(store[choice])); /* actual parameter changed into a pointer by affixing ampersand*/
Further explanations of the codes' misbehavior were explained by Jack. I extend my gratitudes to you. The code now worked as it should be.

Can somebody explain how to use ASyncTask with Android?

I've been doing a bunch of research and looking over the documentation for ASyncTask in Android, but I just can't seem to wrap my head around it. I simply want to run some methods while a fragment is visible in an application, but in order to more easily do that, I think I should be using ASyncTask. My example code is as follows:
private class syncExample extends ASyncTask <Void, Void, Void>
{
#Override
protected void onPreExecute()
{
}
#Override
protected void doInBackground(Void... voids)
{
}
#Override
protected void onProgressUpdate()
{
}
#Override
protected void onPostExecute()
{
}
}
Now my questions are as follows:
In the angle brackets, I have Void, Void, Void. What exactly do those represent and how do I know what's correct to place in there?
For each method within the class, I have the each method called as void. When should they be different than void (like boolean, String, long, etc.)?
For the doInBackground method, I have Void... voids in the parenthesis. What exactly should I be putting in there? What do they represent?
Thank you for your help. The documentation on this is not very clear for a beginner like myself.
AsyncTask uses parameterized types (java generics) so that you can specify the types it uses when you define your own AsyncTask. Perhaps it's easier to explain in this form:
public abstract class AsyncTask<Params, Progress, Result> {
...
protected abstract Result doInBackground(Params... params);
protected abstract void onProgressUpdate(Progress... progress);
protected abstract void onPostExecute(Result result);
...
}
There are no classes named Params, Progress, or Result. These are instead generic types. They are just placeholders for types you wish to use when you define your own AsyncTask subclass. The above could equally be written as such:
public abstract class AsyncTask<A, B, C> {
...
protected abstract C doInBackground(A... params);
protected abstract void onProgressUpdate(B... progress);
protected abstract void onPostExecute(C result);
...
}
Suppose I were defining an AsyncTask that takes a list of Strings representing URLs, and it will ping each one to see if it's reachable, then return the number that were reachable. Meanwhile, with each test, it will update a ProgressBar as each test completes. It might look something like this:
public class MyAsyncTask extends AsyncTask<String, Integer, Integer> {
#Override
protected Integer doInBackground(String... params) {
int total = params.length;
int successfulPings = 0;
for (int i = 0; i < total; i++) {
if (isReachable(params[i])) {
successfulPings++;
}
publishProgress(i, total);
}
return successfulPings;
}
#Override
protected void onProgressUpdate(Integer... progress) {
int testsSoFar = progress[0];
int totalTests = progress[1];
progressBar.setMax(totalTests);
progressBar.setProgress(testSoFar);
}
#Override
protected void onPostExecute(Integer result) {
Toast.makeTest(context, "Reached " + result + " sites.", Toast.LENGTH_SHORT).show();
}
}
I would initiate this as follows:
String[] urls = ...
MyAsyncTask task = new MyAsyncTask();
task.execute(urls);
The argument passed into execute will be passed into doInBackground. Whatever you do in doInBackground, you need to return something that gets passed in as the argument to onPostExecute. While in doInBackground, you can call publishProgress, where you can do something like I did (but you don't have to).
My basic suggestion is to research more about AsyncTask but I'll as well try and Answer your questions:
1- In the angle brackets, I have Void, Void, Void. What exactly do those represent and how do I know what's correct to place in there?
They are the input argument types for the overridden methods: doInBackground() onPreExecute(), onProgressUpdate() and ... respectively.
2- For each method within the class, I have the each method called as void. When should they be different than void (like boolean, String,
long, etc.)?
I'll give you an example: onPostExecute() is guaranteed to be called after doInBackground(), so if onPostExecute(int someCounter) has an integer in its input, then doInBackground() should return an integer rather than void. So it will be declared like this:
int doInBackground(){
//bla bla bla
int someIntegerValue;
return someIntegerValue;
}
3- For the doInBackground() method, I have Void... voids in the parenthesis. What exactly should I be putting in there? What do they
represent?
They are var-args, basically speaking, they represent Variable number of Arguments.
Read more about them here.
Once again I suggest you delve into threading in Android and get a sense of why AsyncTask is needed.
Good Luck,
The three types used by an asynchronous task are the following:
Params, the type of the parameters sent to the task upon execution. i.e if you want to send some variable/array to your async task background task. You use that information using that variable.
Progress, the type of the progress units published during the background computation. i.e to show the progress of your background progress. ( such as showing how much a video/image is downloaded)
Result, the type of the result of the background computation. i.e the result that you calculated in background process used for passing result to onPostExecute method.
String[] username;
username[0]="user1";
username[1]="user2";
new asynctask().execute(username);
private class asynctask extends AsyncTask <USER, Integer, String>
{
#Override
protected void onPreExecute()
{
// anything you want to do prior starting the async task.
}
#Override
protected String doInBackground(USER... users)
{
int count = users.length;
for (int i = 0; i < count; i++)
retriveinformation(users[i]);
return "Hey";
}
protected void onProgressUpdate(Integer... progress) {
setProgressPercent(progress[0]);
}
protected void onPostExecute(String result) {
// the value result is HEY that is returned by doInBackground.
}
}
Not all types are always used by an asynchronous task. To mark a type as unused, simply use the type Void.
information taken from https://developer.android.com/reference/android/os/AsyncTask.html
Let me try my hand at this. All the info I am parsing is from the link shared by Karakuri in the comments to the question.
The angle brackets' contents, in order, are called "parameters", "progress", and "result". Parameters is the type of parameters (using varargs) that will be fed to "doInBackground". They will be receive by that method in an array. You will use these parameters as your data to work on in the background thread.
"Progress" is the types of values fed into the onProgressUpdate. When you override this method, you will take in data of this type, format it, and use it in the progress display. From my reading, it is up to you to publish your progress in doInBackground to be consumed by onProgressUpdate.
"Result" is the data type of the result of the operation. This will be the return type of doInBackground, as well as the parameter type of onPostExecute.
For parameters that include the "..." (called varargs), you can put in as many arguments as you want. They must be separated by commas, and must all be of the type specified before the "...". The items will be passed to the function in an array. So, to answer your questions:
You'll change the first parameter in the angle brackets from void when your background task needs to receive data to work on. The type will be the type of data you'd like to pass in. You'll change the second parameter when you wish to keep track of the progress. The type will be whatever type you'd like to use for keeping track, and you will need to do all the tracking yourself in "doInBackground". You'll change the third parameter when your background task needs to return data. The type will be the type of data you wish to return. The data will be supplied to "onPostExecute". This is important because AsyncTask.execute() has no return value, so you must use any necessary data in that method to get it out to the class that called AsyncTask.execute().
onPreExecute appears to always be type void. doInBackground will be of the type of result, the 3rd argument in your angle brackets. onProgressUpdate appears to always be void. onPostExecute appears to be always void.
The parameter of doInBackground will be a type label followed by the three dot and a name. The type for the label must be the same as the first parameter in the angle brackets. The name is anything you want. That name will then be assigned to an array of the type named in the type label, and passed in for you to use in the body of doInBackground.
Some thoughts: if you are a beginner, leave "progress" void for now, and don't override onProgressUpdate. It is very important in production code, but a lot of extra hard work for beginner learning projects. onPreExecute is also likely more trouble than it is worth at your level. Just remember they exist, and when you need them, you will likely know. Also, good work on not avoiding the subject of multithreading just because it is difficult.

Categories

Resources