Search in sources :

Example 1 with HttpClient

use of org.pixmob.httpclient.HttpClient in project httpclient by pixmob.

the class ContentTypeTask method doRun.

@Override
protected void doRun() throws Exception {
    final HttpClient hc = createClient();
    hc.get("http://www.wired.com/").to(new CheckContentHandler("text/html", "UTF-8")).execute();
    hc.head("http://www.google.com/intl/en_com/images/srpr/logo3w.png").to(new CheckContentHandler("image/png", null)).execute();
}
Also used : HttpClient(org.pixmob.httpclient.HttpClient)

Example 2 with HttpClient

use of org.pixmob.httpclient.HttpClient in project httpclient by pixmob.

the class DownloadFileTask method doRun.

@Override
protected void doRun() throws Exception {
    final File imgFile = new File(getContext().getCacheDir(), "google_logo.png");
    imgFile.delete();
    final HttpClient hc = createClient();
    hc.get("http://www.google.fr/images/srpr/logo3w.png").to(imgFile).execute();
    if (!imgFile.exists() || imgFile.length() == 0) {
        throw new TaskExecutionFailedException("File download failed");
    }
}
Also used : TaskExecutionFailedException(org.pixmob.httpclient.demo.TaskExecutionFailedException) HttpClient(org.pixmob.httpclient.HttpClient) File(java.io.File)

Example 3 with HttpClient

use of org.pixmob.httpclient.HttpClient in project httpclient by pixmob.

the class GoogleAppEngineAuthTask method doRun.

@Override
protected void doRun() throws Exception {
    final AccountManager am = (AccountManager) getContext().getSystemService(Context.ACCOUNT_SERVICE);
    final Account[] accounts = am.getAccountsByType(GoogleAppEngineAuthenticator.GOOGLE_ACCOUNT_TYPE);
    if (accounts == null || accounts.length == 0) {
        throw new IllegalStateException("No Google account found");
    }
    final String gaeHost = "pushpushandroid.appspot.com";
    final GoogleAppEngineAuthenticator auth = new GoogleAppEngineAuthenticator(getContext(), accounts[0], gaeHost);
    final HttpClient hc = createClient();
    hc.head("https://" + gaeHost).with(auth).execute();
}
Also used : Account(android.accounts.Account) GoogleAppEngineAuthenticator(org.pixmob.httpclient.GoogleAppEngineAuthenticator) HttpClient(org.pixmob.httpclient.HttpClient) AccountManager(android.accounts.AccountManager)

Example 4 with HttpClient

use of org.pixmob.httpclient.HttpClient in project httpclient by pixmob.

the class PostFormTask method doRun.

@Override
protected void doRun() throws Exception {
    final HttpClient hc = createClient();
    hc.post("http://groovyconsole.appspot.com/executor.groovy").param("script", "printf 'Hello Android!'").to(new HttpResponseHandler() {

        @Override
        public void onResponse(HttpResponse response) throws Exception {
            final StringBuilder rawJson = new StringBuilder(64);
            response.read(rawJson);
            final JSONObject json = new JSONObject(rawJson.toString());
            assertEquals("Hello Android!", json.getString("outputText"));
        }
    }).execute();
    final HttpResponse resp = hc.post("http://groovyconsole.appspot.com/executor.groovy").param("script", "printf 'Hello Android! (inline)'").execute();
    final StringBuilder rawJson = new StringBuilder(64);
    resp.read(rawJson);
    final JSONObject json = new JSONObject(rawJson.toString());
    assertEquals("Hello Android! (inline)", json.getString("outputText"));
}
Also used : JSONObject(org.json.JSONObject) HttpClient(org.pixmob.httpclient.HttpClient) HttpResponse(org.pixmob.httpclient.HttpResponse) HttpResponseHandler(org.pixmob.httpclient.HttpResponseHandler)

Example 5 with HttpClient

use of org.pixmob.httpclient.HttpClient in project httpclient by pixmob.

the class RedirectTask method doRun.

@Override
protected void doRun() throws Exception {
    final HttpClient hc = createClient();
    final HttpResponse response = hc.get("http://google.com").expect(HttpURLConnection.HTTP_MOVED_PERM).execute();
    assertEquals("text/html", response.getContentType());
    assertEquals("UTF-8", response.getContentCharset());
}
Also used : HttpClient(org.pixmob.httpclient.HttpClient) HttpResponse(org.pixmob.httpclient.HttpResponse)

Aggregations

HttpClient (org.pixmob.httpclient.HttpClient)7 HttpResponse (org.pixmob.httpclient.HttpResponse)2 Account (android.accounts.Account)1 AccountManager (android.accounts.AccountManager)1 File (java.io.File)1 JSONObject (org.json.JSONObject)1 GoogleAppEngineAuthenticator (org.pixmob.httpclient.GoogleAppEngineAuthenticator)1 HttpResponseHandler (org.pixmob.httpclient.HttpResponseHandler)1 TaskExecutionFailedException (org.pixmob.httpclient.demo.TaskExecutionFailedException)1