Android AsyncTask / doInBackground template

This is more “how do I design” than “how do I code”, so I hope this is in the right place ...

I have a pretty standard class that extends from AsyncTask.

What is the best way for me to do this to accomplish many different tasks? For example, when working with GAE, I have standard methods get, insert, delete, update, which all should run asynchronously.

@Override
protected String doInBackground(String... params) {
    if (tipApiService == null) {
        TipBeanApi.Builder builder = new TipBeanApi.Builder(AndroidHttp.newCompatibleTransport(),
                new AndroidJsonFactory(), null)

                .setRootUrl("http://10.0.2.2:8080/_ah/api/").setGoogleClientRequestInitializer(new GoogleClientRequestInitializer() {
                    @Override
                    public void initialize(AbstractGoogleClientRequest<?> abstractGoogleClientRequest) throws IOException {
                        abstractGoogleClientRequest.setDisableGZipContent(true);
                    }
                });

        tipApiService = builder.build();
    }

    try {
        tipApiService.insert(tip).execute();
    } catch (IOException e) {
        Log.d(TAG, "pushToRemote: " + e.toString());
    }

    return null;
}

In the try block, you can see how to execute the insert method.

What if I want to do something else? Should I create a whole new class for each operation or pass in a condition that states what needs to be done? Is there an even cleaner / better way that I just don't know about?

+4
1

Android- , GAE Google Cloud, , , .

, :

public class ThisApp extends Application {

    private static TipBeanApi tipApiService;

    @Override
    public void onCreate() {
        super.onCreate();
        buildCloudEndpoints();
    }

    private void buildCloudEndpoints() {
        TipBeanApi.Builder builder = new TipBeanApi.Builder(AndroidHttp.newCompatibleTransport(),
        new AndroidJsonFactory(), null)
            .setRootUrl("http://10.0.2.2:8080/_ah/api/").setGoogleClientRequestInitializer(new GoogleClientRequestInitializer() {@Override
            public void initialize(AbstractGoogleClientRequest <? > abstractGoogleClientRequest) throws IOException {
                abstractGoogleClientRequest.setDisableGZipContent(true);
            }
        });

        tipApiService = builder.build();
    }

    public static TipBeanApi getApiService() {
        return tipApiService;
    }

}

, , ThisApp.getApiService().yourEndpoint().execute(); .

, AsyncTasks , java API, . , API , AsyncTask Activity, , . private class YourApiCallTask extends AsyncTask ... , , "ApiServiceEndpoints" API, . public static class YourApiCallTask extends AsyncTask ... 10 , AsyncTasks ApiServiceEndpoints, .

, , GAE/cloud Android-!

+4

All Articles