Android Http Get getHTTP(String... params)

Here you can find the source of getHTTP(String... params)

Description

Performs an HTTP GET Request.

Parameter

Parameter Description
params 0-> URL 1 -> email 2 -> pw

Return

401 - If the login fails. The content if all goes well

Declaration

public static String getHTTP(String... params) 

Method Source Code

//package com.java2s;

import org.apache.http.HttpResponse;

import org.apache.http.client.HttpClient;

import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import org.apache.http.client.methods.HttpUriRequest;

import org.apache.http.util.EntityUtils;
import org.apache.http.HttpStatus;

import android.util.Base64;
import android.util.Log;

public class Main {
    /**/*from   www .ja  v  a  2 s. co m*/
     * Performs an HTTP GET Request.
     * @param params 0-> URL
     *             1 -> email
     *             2 -> pw
     * @return  401 - If the login fails.
     *          The content if all goes well
     */
    public static String getHTTP(String... params) {
        HttpClient httpclient = new DefaultHttpClient();
        HttpUriRequest request = new HttpGet(params[0]); // Or HttpPost(), depends on your needs  

        // The authorization part
        Log.d("getHTTP():param[1]", params[1]);
        Log.d("getHTTP():param[2]", params[2]);
        String credentials = params[1] + ":" + params[2];
        String base64EncodedCredentials = Base64.encodeToString(
                credentials.getBytes(), Base64.NO_WRAP);
        request.addHeader("Authorization", "Basic "
                + base64EncodedCredentials);

        try {
            HttpResponse response = httpclient.execute(request);
            // Check the response status for login.
            if (!isLoggedIn(response.getStatusLine().getStatusCode())) {
                return "" + HttpStatus.SC_UNAUTHORIZED;
            }

            return EntityUtils.toString(response.getEntity());
        } catch (Exception e) {
            Log.d("getHTTP():Exception", e.toString());
        }
        return null;
    }

    public static boolean isLoggedIn(int status) {
        if (status == HttpStatus.SC_UNAUTHORIZED) {
            return false;
        }

        return true;
    }
}

Related

  1. request(String url)
  2. requestGet(String url)
  3. executeHttpGet(String url)
  4. executeHttpPost(String url, ArrayList postParameters)
  5. handleURLEncodedResponse(HttpResponse response)
  6. handleXMLResponse(HttpResponse response)
  7. getStringFromConnection( HttpURLConnection connection)
  8. httpGetRequestParseParams( String paramString)