Search code examples
javaandroidhttpurlconnectionandroidhttpclient

Adding TwinPrime to my HttpURLConnection


TwinPrime is an SDK that supposedly enhances your app's internet connectivity by 100% with a few lines of code (www.twinprime.com).

Apparently all I need to do is place the following code in my MainActivity:

new TwinPrimeSDK(getApplicationContext(), "API_KEY");
try {
    URLConnection httpConn = TPURLConnection.openConnection("your-URL");
} catch (IOException e) {
    e.printStackTrace();
}

Then attach the following wherever there is an HttpConnection:

URLConnection httpConn = TPURLConnection.openConnection("your-URL");

My problem is I have no idea where to attach it. I've contacted their customer support but maybe somebody on SO has had experience with them?

private Bitmap getBitmap(String url) {
        File f = fileCache.getFile(url);

        Bitmap b = decodeFile(f);
        if (b != null) {
            return b;
        }

        // Download Images from the Internet
        try {
            Bitmap bitmap = null;
            URL imageUrl = new URL(url);
            HttpURLConnection conn = (HttpURLConnection) imageUrl.openConnection();
            conn.setConnectTimeout(30000);
            conn.setReadTimeout(30000);
            conn.setInstanceFollowRedirects(true);
            InputStream is = conn.getInputStream();
            OutputStream os = new FileOutputStream(f);
            FeedUtils.CopyStream(is, os);
            os.close();
            conn.disconnect();
            bitmap = decodeFile(f);
            return bitmap;
        } catch (Throwable ex) {
            ex.printStackTrace();
            if (ex instanceof OutOfMemoryError)
                memoryCache.clear();
            return null;
        }
    }

Solution

  • This is how I accomplished it.

    private Bitmap getBitmap(String url) {
            File f = fileCache.getFile(url);
    
            Bitmap b = decodeFile(f);
            if (b != null) {
                return b;
            }
    
            // Download Images from the Internet
            try {
                Bitmap bitmap = null;
                URL imageUrl = new URL(url);
                HttpURLConnection httpConn = (HttpURLConnection) TPURLConnection.openConnection(url);
    //            HttpURLConnection conn = (HttpURLConnection) imageUrl.openConnection();
                httpConn.setConnectTimeout(30000);
                httpConn.setReadTimeout(30000);
                httpConn.setInstanceFollowRedirects(true);
                InputStream is = httpConn.getInputStream();
                OutputStream os = new FileOutputStream(f);
                FeedUtils.CopyStream(is, os);
                os.close();
                httpConn.disconnect();
                bitmap = decodeFile(f);
                return bitmap;
            } catch (Throwable ex) {
                ex.printStackTrace();
                if (ex instanceof OutOfMemoryError)
                    memoryCache.clear();
                return null;
            }
        }