com.victorkifer.AndroidTemplates.api.Downloader.java Source code

Java tutorial

Introduction

Here is the source code for com.victorkifer.AndroidTemplates.api.Downloader.java

Source

package com.victorkifer.AndroidTemplates.api;

import com.victorkifer.AndroidTemplates.R;
import com.victorkifer.AndroidTemplates.utils.AppUtils;
import com.victorkifer.AndroidTemplates.utils.Logger;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.DefaultHttpClient;

import java.io.*;
import java.util.List;

/**
 * Author: Victor Kifer (droiddevua[at]gmail[dot]com)
 * License: MIT (see LICENSE file)
 * Year: 2014
 */
public class Downloader {
    private static final String TAG = Downloader.class.getSimpleName();

    public final static int GET = 1;
    public final static int POST = 2;

    private static DefaultHttpClient httpClient = new DefaultHttpClient();

    public static String textDownload(String url, int method, List<NameValuePair> params) throws Exception {
        StringBuilder builder = new StringBuilder();
        try {
            HttpResponse httpResponse = null;

            switch (method) {
            case GET:
                httpResponse = makeHttpGetRequest(url, params);
                break;
            case POST:
                httpResponse = makeHttpPostRequest(url, params);
            }

            StatusLine statusLine = httpResponse.getStatusLine();
            int statusCode = statusLine.getStatusCode();
            if (statusCode == 200) {
                HttpEntity entity = httpResponse.getEntity();
                InputStream content = entity.getContent();
                BufferedReader reader = new BufferedReader(new InputStreamReader(content, "utf-8"));
                String line;
                while ((line = reader.readLine()) != null) {
                    builder.append(line);
                }
            }
        } catch (UnsupportedEncodingException e) {
            Logger.e(TAG, e.getMessage());
            throw new Exception(AppUtils.getContext().getString(R.string.err_failed_download));
        } catch (ClientProtocolException e) {
            Logger.e(TAG, e.getMessage());
            throw new Exception(AppUtils.getContext().getString(R.string.err_failed_download));
        } catch (IOException e) {
            Logger.e(TAG, e.getMessage());
            throw new Exception(AppUtils.getContext().getString(R.string.err_failed_download));
        }

        Logger.i(TAG, builder.toString());

        return builder.toString();
    }

    private static HttpResponse makeHttpGetRequest(String url, List<NameValuePair> params) throws IOException {
        if (params != null) {
            String paramString = URLEncodedUtils.format(params, "utf-8");
            url += "?" + paramString;
        }

        Logger.e(TAG, url);

        HttpGet httpGet = new HttpGet(url);
        return httpClient.execute(httpGet);
    }

    private static HttpResponse makeHttpPostRequest(String url, List<NameValuePair> params) throws IOException {
        HttpPost httpPost = new HttpPost(url);

        if (params != null) {
            httpPost.setEntity(new UrlEncodedFormEntity(params));
        }

        Logger.e(TAG, url);

        return httpClient.execute(httpPost);
    }

}