Search in sources :

Example 1 with AndroidHttpClient

use of android.net.http.AndroidHttpClient in project qksms by moezbhatti.

the class HttpUtils method createHttpClient.

private static AndroidHttpClient createHttpClient(Context context) {
    String userAgent = MmsConfig.getUserAgent();
    AndroidHttpClient client = AndroidHttpClient.newInstance(userAgent, context);
    HttpParams params = client.getParams();
    HttpProtocolParams.setContentCharset(params, "UTF-8");
    // set the socket timeout
    int soTimeout = MmsConfig.getHttpSocketTimeout();
    if (LOCAL_LOGV)
        Log.d(TAG, "[HttpUtils] createHttpClient w/ socket timeout " + soTimeout + " ms, " + ", UA=" + userAgent);
    HttpConnectionParams.setSoTimeout(params, soTimeout);
    return client;
}
Also used : HttpParams(org.apache.http.params.HttpParams) AndroidHttpClient(android.net.http.AndroidHttpClient)

Example 2 with AndroidHttpClient

use of android.net.http.AndroidHttpClient in project robolectric by robolectric.

the class AndroidHttpClientTest method testNewInstanceWithContext.

@Test
public void testNewInstanceWithContext() throws Exception {
    AndroidHttpClient client = AndroidHttpClient.newInstance("foo", RuntimeEnvironment.application);
    assertThat(client).isNotNull();
}
Also used : AndroidHttpClient(android.net.http.AndroidHttpClient) Test(org.junit.Test)

Example 3 with AndroidHttpClient

use of android.net.http.AndroidHttpClient in project glitch-hq-android by tinyspeck.

the class ImageDownloader method downloadBitmap.

Bitmap downloadBitmap(String url) {
    final int IO_BUFFER_SIZE = 4 * 1024;
    try {
        // AndroidHttpClient is not allowed to be used from the main thread
        final HttpClient client = (mode == Mode.NO_ASYNC_TASK) ? new DefaultHttpClient() : AndroidHttpClient.newInstance("Android");
        final HttpGet getRequest = new HttpGet(url);
        try {
            HttpResponse response = client.execute(getRequest);
            final int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode != HttpStatus.SC_OK) {
                Log.w("ImageDownloader", "Error " + statusCode + " while retrieving bitmap from " + url);
                return null;
            }
            final HttpEntity entity = response.getEntity();
            if (entity != null) {
                InputStream inputStream = null;
                try {
                    inputStream = entity.getContent();
                    // Bug on slow connections, fixed in future release.
                    return BitmapFactory.decodeStream(new FlushedInputStream(inputStream));
                } finally {
                    if (inputStream != null) {
                        inputStream.close();
                    }
                    entity.consumeContent();
                }
            }
        } catch (IOException e) {
            getRequest.abort();
            Log.w(LOG_TAG, "I/O error while retrieving bitmap from " + url, e);
        } catch (IllegalStateException e) {
            getRequest.abort();
            Log.w(LOG_TAG, "Incorrect URL: " + url);
        } catch (Exception e) {
            getRequest.abort();
            Log.w(LOG_TAG, "Error while retrieving bitmap from " + url, e);
        } finally {
            if ((client instanceof AndroidHttpClient)) {
                ((AndroidHttpClient) client).close();
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
Also used : HttpEntity(org.apache.http.HttpEntity) FilterInputStream(java.io.FilterInputStream) InputStream(java.io.InputStream) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) AndroidHttpClient(android.net.http.AndroidHttpClient) HttpClient(org.apache.http.client.HttpClient) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) AndroidHttpClient(android.net.http.AndroidHttpClient) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) IOException(java.io.IOException)

Example 4 with AndroidHttpClient

use of android.net.http.AndroidHttpClient in project android_frameworks_base by ParanoidAndroid.

the class GpsXtraDownloader method doDownload.

protected static byte[] doDownload(String url, boolean isProxySet, String proxyHost, int proxyPort) {
    if (DEBUG)
        Log.d(TAG, "Downloading XTRA data from " + url);
    AndroidHttpClient client = null;
    try {
        client = AndroidHttpClient.newInstance("Android");
        HttpUriRequest req = new HttpGet(url);
        if (isProxySet) {
            HttpHost proxy = new HttpHost(proxyHost, proxyPort);
            ConnRouteParams.setDefaultProxy(req.getParams(), proxy);
        }
        req.addHeader("Accept", "*/*, application/vnd.wap.mms-message, application/vnd.wap.sic");
        req.addHeader("x-wap-profile", "http://www.openmobilealliance.org/tech/profiles/UAPROF/ccppschema-20021212#");
        HttpResponse response = client.execute(req);
        StatusLine status = response.getStatusLine();
        if (status.getStatusCode() != 200) {
            // HTTP 200 is success.
            if (DEBUG)
                Log.d(TAG, "HTTP error: " + status.getReasonPhrase());
            return null;
        }
        HttpEntity entity = response.getEntity();
        byte[] body = null;
        if (entity != null) {
            try {
                if (entity.getContentLength() > 0) {
                    body = new byte[(int) entity.getContentLength()];
                    DataInputStream dis = new DataInputStream(entity.getContent());
                    try {
                        dis.readFully(body);
                    } finally {
                        try {
                            dis.close();
                        } catch (IOException e) {
                            Log.e(TAG, "Unexpected IOException.", e);
                        }
                    }
                }
            } finally {
                if (entity != null) {
                    entity.consumeContent();
                }
            }
        }
        return body;
    } catch (Exception e) {
        if (DEBUG)
            Log.d(TAG, "error " + e);
    } finally {
        if (client != null) {
            client.close();
        }
    }
    return null;
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) StatusLine(org.apache.http.StatusLine) HttpEntity(org.apache.http.HttpEntity) HttpHost(org.apache.http.HttpHost) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) AndroidHttpClient(android.net.http.AndroidHttpClient) DataInputStream(java.io.DataInputStream) IOException(java.io.IOException)

Example 5 with AndroidHttpClient

use of android.net.http.AndroidHttpClient in project android_frameworks_base by ParanoidAndroid.

the class SslLoad method run.

public void run() {
    boolean error = false;
    while (true) {
        synchronized (this) {
            while (!running) {
                try {
                    this.wait();
                } catch (InterruptedException e) {
                /* ignored */
                }
            }
        }
        AndroidHttpClient client = AndroidHttpClient.newInstance("Mozilla/5.001 (windows; U; NT4.0; en-us) Gecko/25250101");
        try {
            // Cert. is for "www.google.com", not "google.com".
            String url = error ? "https://google.com/" : "https://www.google.com";
            client.execute(new HttpGet(url), new ResponseHandler<Void>() {

                public Void handleResponse(HttpResponse response) {
                    /* ignore */
                    return null;
                }
            });
            Log.i(TAG, "Request succeeded.");
        } catch (IOException e) {
            Log.w(TAG, "Request failed.", e);
        }
        client.close();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
        /* ignored */
        }
        error = !error;
    }
}
Also used : HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) AndroidHttpClient(android.net.http.AndroidHttpClient)

Aggregations

AndroidHttpClient (android.net.http.AndroidHttpClient)8 HttpResponse (org.apache.http.HttpResponse)5 HttpGet (org.apache.http.client.methods.HttpGet)5 IOException (java.io.IOException)4 HttpEntity (org.apache.http.HttpEntity)3 Test (org.junit.Test)3 DataInputStream (java.io.DataInputStream)2 HttpHost (org.apache.http.HttpHost)2 StatusLine (org.apache.http.StatusLine)2 HttpParams (org.apache.http.params.HttpParams)2 TelephonyManager (android.telephony.TelephonyManager)1 FilterInputStream (java.io.FilterInputStream)1 InputStream (java.io.InputStream)1 SocketException (java.net.SocketException)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 HttpRequest (org.apache.http.HttpRequest)1 HttpClient (org.apache.http.client.HttpClient)1 HttpPost (org.apache.http.client.methods.HttpPost)1 HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)1