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() throws InterruptedException, ExecutionException 

Source Link

Document

Waits if necessary for the computation to complete, and then retrieves its result.

Usage

From source file:com.microsoft.activitytracker.Core.NetworkCalls.java

public static Object getAuthority(String endpoint) {
    try {/*from   w  ww .  j ava 2 s  . c o m*/
        AsyncTask authTask = new getAuthorityTask(endpoint).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
        return authTask.get();
    } catch (Exception ex) {
        ex.printStackTrace();
    }

    return null;
}

From source file:conexionSiabra.PeticionGet.java

public String Ejecutar() {
    AsyncTask<String, String, String> variable = new PeticionHttp().execute(url, "nada", result);
    try {//from  w w w. j a  v  a2 s . c o m
        result = variable.get();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ExecutionException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return result;
}

From source file:conexionSiabra.PeticionPost.java

public String Ejecutar() {

    AsyncTask<ArrayList<NameValuePair>, Void, String> variable = new PeticionHttp();
    ((PeticionHttp) variable).setURL(url);
    variable.execute(listaDePares);//www .j a va 2 s  . c om
    try {
        result = variable.get();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ExecutionException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return result;
}

From source file:org.oucho.whois.MainActivity.java

private String runLookupTask(LookupTask lookupTask, String lookupString) {
    AsyncTask<String, Void, String> task = lookupTask.execute(lookupString);
    String result = null;//from  w  w  w  . j a va  2  s . com
    try {
        result = task.get();
        if (result == null) {

            String message = lookupTask.getExceptionMsg();
            String title = lookupTask.getException().toString();
            displayMessageDialog(message, title);
        }

    } catch (Exception taskE) {
        String message = taskE.getMessage();
        String title = taskE.toString();
        displayMessageDialog(message, title);
    }
    return result;
}

From source file:thiru.in.basicauthwebview.ShowWebViewActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_show_web_view);

    // Initialize the Cookie Manager
    CookieSyncManager.createInstance(this);
    CookieManager cookieManager = CookieManager.getInstance();
    cookieManager.setAcceptCookie(true);

    String url = "http://auth.thiru.in/";
    // Call the REST Service to Authenticate
    // This is sample app, the app can have a login form, accept input, encode the
    // id and passowrd using Base64 for Validating
    AsyncTask<String, Void, CookieStore> cookieStoreTask = new Authenticate(this).execute(url + "app-auth",
            "ZGVtbzpkZW1v"); //Base64 Encoded for demo:demo

    try {//from ww w . j a  v a 2s  .c om
        cookieManager.removeAllCookie();
        CookieStore cookieStore = cookieStoreTask.get();
        List<Cookie> cookies = cookieStore.getCookies();
        // If the User id and password is wrong, the cookie store will not have any cookies
        // And the page will display 403 Access Denied
        for (Cookie cookie : cookies) {
            Log.i("Cookie", cookie.getName() + " ==> " + cookie.getValue());
            // Add the REST Service Cookie Responses to Cookie Manager to make it
            // Available for the WebViewClient
            cookieManager.setCookie(url, cookie.getName() + "=" + cookie.getValue());
        }
    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (ExecutionException e) {
        e.printStackTrace();
    }

    mWebView = (WebView) findViewById(R.id.activity_main_webview);
    mWebView.setWebViewClient(new WebViewClient());
    mWebView.loadUrl(url);
}

From source file:com.example.httpjson.AppEngineClient.java

public String getTokenFromServer() {
    String s = null;/*from w ww .  ja  va 2 s. co m*/
    try {

        URL uri = new URL("https://rtonames.sprintlabs.io/token");

        Map<String, List<String>> headers = new HashMap<String, List<String>>();

        String key = "Authorization";
        String authStr = "6eece178-9fcf-45b7-ab50-08b8066daabe:e494a4e72b6f7c7672b74d311cbabf2672be8c24c9496554077936097195a5ac69b6acd11eb09a1ae07a40d2e7f0348d";
        String encoding = Base64.encodeToString(authStr.getBytes(), Base64.DEFAULT);
        encoding = encoding.replace("\n", "").replace("\r", "");

        System.out.println(encoding);
        List<String> value = Arrays.asList("Basic " + encoding);

        headers.put(key, value);

        AppEngineClient aec1 = new AppEngineClient();
        Request request = aec1.new GET(uri, headers);

        AsyncTask<Request, Void, Response> obj = new asyncclass().execute(request);

        Response response = obj.get();

        obj.cancel(true);

        obj = null;

        String body = null;
        try {
            body = new String(response.body, "UTF-8");
        } catch (UnsupportedEncodingException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        JSONObject tokenjson = AppEngineClient.getJSONObject(body);

        if (tokenjson.has("token")) {
            try {
                s = tokenjson.getString("token");
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }

    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return s;
}

From source file:org.sigimera.app.android.controller.PersistanceController.java

private void storeLatestCrises(String _auth_token, int _page) {
    AsyncTask<String, Void, JSONArray> crisesHelper = new CrisesHttpHelper().execute(_auth_token, _page + "");
    JSONArray crises = null;/*from w  ww  .j a  va2 s  .c om*/
    try {
        crises = crisesHelper.get();
        if (crises != null) {
            for (int count = 0; count < crises.length(); count++) {
                try {
                    JSONObject crisis = (JSONObject) crises.get(count);
                    this.pershandler.addCrisis(crisis);
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ExecutionException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:org.sigimera.app.android.controller.PersistanceController.java

public Crisis getCrisis(String _authToken, String _crisisID) {
    Crisis crisis = this.pershandler.getCrisis(_crisisID);
    if (null == crisis) {
        AsyncTask<String, Void, JSONObject> singleCrisisTask = new SingleCrisisHttpHelper().execute(_authToken,
                _crisisID);/*from  w  w w.  ja  va  2 s  . c  o m*/
        try {
            this.pershandler.addCrisis(singleCrisisTask.get());
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ExecutionException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        crisis = this.pershandler.getCrisis(_crisisID);
    }
    return crisis;
}

From source file:org.sigimera.app.android.controller.PersistanceController.java

/**
 * /*from  w  ww .  ja  va2  s .c o  m*/
 * @param _authToken
 * @return
 */
public CrisesStats getCrisesStats(String _authToken) {
    CrisesStats stats = this.pershandler.getCrisesStats();

    if (null == stats && _authToken != null) {
        AsyncTask<String, Void, JSONObject> crisesStatsHelper = new StatisticCrisesHttpHelper()
                .execute(_authToken);
        try {
            this.pershandler.addCrisesStats(crisesStatsHelper.get());
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ExecutionException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        stats = this.pershandler.getCrisesStats();
    }

    return stats;
}

From source file:org.sigimera.app.android.controller.PersistanceController.java

/**
 * Update crises statistics/*from   w w  w .j a  va2s .co  m*/
 * 
 * @param _auth_token
 * @param _location
 */
private void updateCrisesStats(String _auth_token, Location _location) {
    Log.i("[PERSISTENT CONTROLLER]", "Check if there are new crises statistics");
    CrisesStats crisesStats = getCrisesStats(_auth_token);

    if (crisesStats != null && crisesStats.getLatestCrisisAt() != null) {
        Date date = Common.getDate(crisesStats.getLatestCrisisAt());

        AsyncTask<String, Void, JSONObject> crisesStatsHelper = new StatisticCrisesHttpHelper()
                .execute(_auth_token);
        try {
            JSONObject tmpStats = crisesStatsHelper.get();
            if (tmpStats != null) {
                String latestCrisisAt = tmpStats.getString("latest_crisis_at");
                if (latestCrisisAt != null && Common.getDate(latestCrisisAt).after(date)) {
                    Log.i("[PERSISTENT CONTROLLER]",
                            "There are new crises statistics available. Update the existing ones");
                    this.pershandler.updateCrisesStats(crisesStatsHelper.get());
                    //                   this.pershandler.addCrisesStats(crisesStatsHelper.get());   
                } else
                    Log.i("[PERSISTENT CONTROLLER]", "Crises statistics are up to date.");
            }
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ExecutionException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    } else
        Log.i("[PERSISTENT CONTROLLER]", "Crises statistics or latest crisis date are empty.");
}