Android Open Source - fooplayer-android Http Client Helper






From Project

Back to project page fooplayer-android.

License

The source code is released under:

GNU General Public License

If you think the Android project fooplayer-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.mauriciogiordano.fooplayer.network;
//from w  w  w.  j  ava 2  s  . co  m
import android.content.Context;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
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.URIUtils;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;

public class HttpClientHelper
{
    public static Integer PORT   = 80;
    public static String METHOD  = "http";

    private URI uri = null;
    private String url;
    private String path;

    private static DefaultHttpClient httpClient = null;

    private List<NameValuePair> dataPost = new ArrayList<NameValuePair>();
    private List<NameValuePair> dataGet  = new ArrayList<NameValuePair>();

    public HttpClientHelper(String url, String path, Context context)
    {
        this.url = url;
        this.path = path;

        if(httpClient == null)
        {
            httpClient = new DefaultHttpClient();
        }
    }

    public void setSSL()
    {
        PORT = 443;
        METHOD = "https";
    }

    public void addParamForGet(String key, String value)
    {
        dataGet.add(new BasicNameValuePair(key, value));
    }

    public void addParamForPost(String key, String value)
    {
        dataPost.add(new BasicNameValuePair(key, value));
    }

    public HttpResponse executePost()
    {
        HttpResponse response = null;

        try {
            uri = URIUtils.createURI(METHOD, url, PORT, path,
                    dataGet == null ? null : URLEncodedUtils.format(dataGet, "UTF-8"), null);

            HttpPost httpPost = new HttpPost(uri);

            httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded");

            httpPost.setEntity(new UrlEncodedFormEntity(dataPost, HTTP.UTF_8));

            response = httpClient.execute(httpPost);

        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (URISyntaxException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        return response;
    }

    public HttpResponse executeGet()
    {
        try {
            uri = URIUtils.createURI(METHOD, url, PORT, path,
                    dataGet == null ? null : URLEncodedUtils.format(dataGet, "UTF-8"), null);
        } catch (URISyntaxException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        HttpGet httpget = new HttpGet(uri);

        HttpResponse response = null;

        try {
            response = httpClient.execute(httpget);
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return response;
    }

    public DefaultHttpClient getHttpClient() { return httpClient; }

    public String getURI()
    {
        return uri.toString();
    }

    public List<NameValuePair> getParams()
    {
        return dataGet;
    }

    public List<NameValuePair> postParams()
    {
        return dataPost;
    }
}




Java Source Code List

com.mauriciogiordano.fooplayer.AlbumsActivity.java
com.mauriciogiordano.fooplayer.ApplicationTest.java
com.mauriciogiordano.fooplayer.FooActivity.java
com.mauriciogiordano.fooplayer.TracksActivity.java
com.mauriciogiordano.fooplayer.adapter.AlbumsAdapter.java
com.mauriciogiordano.fooplayer.adapter.ArtistsAdapter.java
com.mauriciogiordano.fooplayer.adapter.TracksAdapter.java
com.mauriciogiordano.fooplayer.config.Constants.java
com.mauriciogiordano.fooplayer.database.Album.java
com.mauriciogiordano.fooplayer.database.Artist.java
com.mauriciogiordano.fooplayer.database.Bean.java
com.mauriciogiordano.fooplayer.database.DatabaseHelper.java
com.mauriciogiordano.fooplayer.database.Track.java
com.mauriciogiordano.fooplayer.fragment.AlbumsFragment.java
com.mauriciogiordano.fooplayer.fragment.ArtistsFragment.java
com.mauriciogiordano.fooplayer.fragment.TracksFragment.java
com.mauriciogiordano.fooplayer.network.Delegate.java
com.mauriciogiordano.fooplayer.network.Endpoints.java
com.mauriciogiordano.fooplayer.network.HttpClientHelper.java
com.mauriciogiordano.fooplayer.network.ImageHelper.java
com.mauriciogiordano.fooplayer.network.Network.java
com.mauriciogiordano.fooplayer.network.YouTubeUtility.java
com.mauriciogiordano.fooplayer.view.TextView.java