Example usage for android.os AsyncTask get

List of usage examples for android.os AsyncTask get

Introduction

In this page you can find the example usage for android.os AsyncTask get.

Prototype

public final Result get(long timeout, TimeUnit unit)
        throws InterruptedException, ExecutionException, TimeoutException 

Source Link

Document

Waits if necessary for at most the given time for the computation to complete, and then retrieves its result.

Usage

From source file:com.joshmoles.byobcontroller.Loading.java

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_loading);
    // Show the Up button in the action bar.
    setupActionBar();//from w w w  .  ja va2  s . c o  m

    // Get the variables passed from super
    Bundle extras = getIntent().getExtras();
    if (extras != null) {
        pass = extras.getString(Consts.GAME_PASSWORD);
    }

    if (pass == null) {
        // Pop up an error and go back.
        Toast.makeText(Loading.this, getString(R.string.toast_no_string_error), Toast.LENGTH_SHORT).show();
        NavUtils.navigateUpFromSameTask(this);

    }

    // Attempt to fetch the PubNub configuration information with provided password.

    String url = Consts.BASE_URL + "/client/getconfig/" + pass;
    Log.d(Consts.LOAD_FETCH, "URL is " + url);

    ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
    if (networkInfo != null && networkInfo.isConnected()) {
        // Execute the download of the page data.
        AsyncTask<String, Void, ConfigEntry> myTask = new DownloadWebPageTask(this).execute(url);
        // Wait for the task to finish.
        try {
            // Attempt to grab result from thread with a timeout.
            result = myTask.get(Consts.XML_FETCH_TIMEOUT, TimeUnit.MILLISECONDS);

            // Check if it was cancelled, if so, navigate up.
            if (myTask.isCancelled()) {
                NavUtils.navigateUpFromSameTask(this);
            }

            // Ensure result is not null and attempt to join a game.
            if (result != null) {
                attemptToJoin(result, pass);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
            goBackFailure();
        } catch (ExecutionException e) {
            e.printStackTrace();
            goBackFailure();
        } catch (TimeoutException e) {
            e.printStackTrace();
            goBackFailure();
        } catch (CancellationException e) {
            // Thread was cancelled (likely from 404 error).
            Toast.makeText(this, getString(R.string.invalid_password), Toast.LENGTH_SHORT).show();
            NavUtils.navigateUpFromSameTask(this);
        }
        // Task fetch completed.

    } else {
        // No network connection is available.
        Toast.makeText(Loading.this, getString(R.string.toast_no_connection_error), Toast.LENGTH_SHORT).show();
        NavUtils.navigateUpFromSameTask(this);

    }

}

From source file:com.example.zillowapplication.SlidingTabsBasicFragment.java

@SuppressWarnings("unchecked")
@SuppressLint("UseSparseArrays")
private void fetchPropertyDetails() {
    String propertyDetails = getActivity().getIntent().getStringExtra("propertyDetails");
    try {/*  w ww .java 2s. c  o m*/
        JSONObject propertyJSON = new JSONObject(propertyDetails);
        HashMap<Integer, String> yearsToUrl = new HashMap<Integer, String>();
        getDetailsListFromJSON(propertyJSON, this.detailRows, yearsToUrl);
        AsyncTask<HashMap<Integer, String>, Integer, HashMap<Integer, Bitmap>> task = new LoadImageTask()
                .execute(yearsToUrl);
        HashMap<Integer, Bitmap> yearToImageMap = null;
        try {
            yearToImageMap = (HashMap<Integer, Bitmap>) task.get(10000, TimeUnit.MILLISECONDS);

        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ExecutionException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (TimeoutException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        passImagesToFragment(yearToImageMap);
    } catch (Exception e) {
        e.printStackTrace();
    }
}