Skip to content

This is an update version of Generic AsyncTask which allows you to call get result in different methods.

Notifications You must be signed in to change notification settings

kasirye/generic_asynctask_v2

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Generic Asynctask 1.2

This is an update version of Generic AsyncTask library (https://github.com/kosalgeek/generic_asynctask).

This is a custom class that performs a POST call from Android App to Web Server and get the response's result back.

For the Introduction and the motivation of this library, please read https://github.com/kosalgeek/generic_asynctask. In this page, only the usage and setup will be provided.

Usage

Class: PostResponseAsyncTask

1. Constuctor: PostResponseAsyncTask(Context context, AsyncResponse delegate)

public PostResponseAsyncTask(Context context, AsyncResponse delegate)

In the previous version where this constructor has only one argument, this version of constructor needs 2 inputs. The first one is the context which is your Activity class. And the second one is the AsyncResponse interface. The big difference of this version is the second input. This input will let you make a different call to get a different response result. Even better, you can use anonymous class call here. As you can see an example below, two different tasks can have a complete different thread call to get a different result at a different time (First in, First Out):

//10.0.3.2 is the alias IP for GenyMotion
//10.0.2.2 is the alias IP for Android native Emulator
String url1 = "https://10.0.3.2/client/post.php?format=json"; 
PostResponseAsyncTask task1 = new PostResponseAsyncTask(MainActivity.this, new AsyncResponse() {
    @Override
    public void processFinish(String s) {
        Toast.makeText(MainActivity.this, s, Toast.LENGTH_LONG).show();
    }
});
task1.execute(url1);

String url2 = "https://someurl.com/read.php";
PostResponseAsyncTask task2 = new PostResponseAsyncTask(MainActivity.this, new AsyncResponse() {
    @Override
    public void processFinish(String s) {
        Toast.makeText(MainActivity.this, s, Toast.LENGTH_LONG).show();
    }
});
task.execute(url2);

2. Constructor: PostResponseAsyncTask(Context context, boolean showLoadingMessage, AsyncResponse delegate)

public PostResponseAsyncTask(Context context, boolean showLoadingMessage, AsyncResponse delegate);

This constructor is not so different to the previous one except it has boolean showLoadingMessage where you can set false to disable the loading message. By default, it shows a loading message everytime you retrieve data. Sometimes the data is too small to retriev so you can disable the loading message to make it look better for user experience. However, I would prefer not to set it to false while even the small data can take a very long time due to a slow internet speed. So, be causious when setting it to false. Below is sample code:

String url1 = "https://someurl.com/read.php";
PostResponseAsyncTask task1 = new PostResponseAsyncTask(MainActivity.this, false, new AsyncResponse() {
    @Override
    public void processFinish(String s) {
        Toast.makeText(MainActivity.this, s, Toast.LENGTH_LONG).show();
    }
});
task1.execute(url1);

3. Constructor: PostResponseAsyncTask(Context context, HashMap<String, String> postData, AsyncResponse delegate);

This is another important constructor. This constructor has three inputs instead of two. The first one is your Activity class; the second one is the postData where you post data as HashMap into the server; and the last one is the usual AsyncResponse. See the example below:

String url = "https://10.0.3.2/client/post.php";
HashMap postData = new HashMap();
postData.put("id", "1");
postData.put("name", "John");
PostResponseAsyncTask readTask = new PostResponseAsyncTask(MainActivity.this, postData, new AsyncResponse() {
  @Override
  public void processFinish(String s) {
    Toast.makeText(MainActivity.this, s, Toast.LENGTH_LONG).show();
  }
});
readTask.execute(url);

4. Constructor: PostResponseAsyncTask(Context context, HashMap<String, String> postData, boolean showLoadingMessage, AsyncResponse delegate);

This is like the previous one except the third argument is where you can set false to disable loading message.

String url = "https://10.0.3.2/client/post.php";
HashMap postData = new HashMap();
postData.put("id", "1");
postData.put("name", "John");
PostResponseAsyncTask readTask = new PostResponseAsyncTask(MainActivity.this, postData, false, new AsyncResponse() {
  @Override
  public void processFinish(String s) {
    Toast.makeText(MainActivity.this, s, Toast.LENGTH_LONG).show();
  }
});
readTask.execute(url);

Other Constructor:

public PostResponseAsyncTask(Context context,
                                 String loadingMessage,
                                 AsyncResponse delegate);

The above constructor can let you customize your loadin message text while posting data.

public PostResponseAsyncTask(Context context,
                                 HashMap<String, String> postData,
                                 String loadingMessage,
                                 AsyncResponse delegate);

The above constructor can let you customize your loadin message text while posting data and retreiving data.

Set Up

  1. Download GenAsync.1.2.jar from this github to your local machine.
  2. Go to your project in Android Studio, on a Project panel (on the left), go to app > libs. You copy the GenAsync.jar and paste it in the libs folder > Press OK if a asked message appeared.
  3. Right click on the jar file > Choose Add As Library. And if you are still not sure how to do that, please check out this link https://code2care.org/pages/import-external-jars-to-android-studio-project/.
  4. Try this code below:
PostResponseAsyncTask task;
  1. If it shows no errors and you see import com.kosalgeek.genasync12.PostResponseAsyncTask; correctly, then you are good to go.

Follow Me

LICENSE

(The MIT License) Copyright (c) 2015 KosalGeek. (kosalgeek at gmail dot com)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

About

This is an update version of Generic AsyncTask which allows you to call get result in different methods.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Java 100.0%