Http Get : Http Connection « Network « Android






Http Get

    
/*
 *  Copyright 2010 Facebook, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


//package net.sarangnamu.android;


import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLDecoder;
import java.net.URLEncoder;

import android.content.Context;
import android.os.Bundle;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;

final class Util {


  public static String openURL(String url, String method, Bundle params)
    throws MalformedURLException, IOException {

    final String boundary = "sarangnamu.net_boundry-------------------";
    final String endLine = "\r\n";

    OutputStream os;

    if (method.equals("GET")) {
      url = url + "?" + percentEncode(params);
    }

    HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
    conn.setRequestProperty("User-Agent",
                System.getProperties().getProperty("http.agent") + " sarangnamu.net");

    if (!method.equals("GET")) {
      Bundle dataParams = new Bundle();

      for (String key : params.keySet()) {
        if (params.getByteArray(key) != null) {
          dataParams.putByteArray(key, params.getByteArray(key));
        }
      }

      if (!params.containsKey("method")) {
        params.putString("method", method);
      }

      conn.setRequestMethod("POST");
      conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
      conn.setDoOutput(true);
      conn.setDoInput(true);
      conn.setRequestProperty("Connection", "Keep-Alive");
      conn.connect();

      os = new BufferedOutputStream(conn.getOutputStream());
      os.write(("---" + boundary + endLine).getBytes());
      os.write(percentEncodeBody(params, boundary).getBytes());
      os.write((endLine + "---" + boundary + endLine).getBytes());

      if (!dataParams.isEmpty()) {
        for (String key : dataParams.keySet()) {
          os.write(("Content-Disposition: form-data; filename=\"" + key + "\"" + endLine).getBytes());
                    os.write(("Content-Type: content/unknown" + endLine + endLine).getBytes());
                    os.write(dataParams.getByteArray(key));
                    os.write((endLine + "--" + boundary + endLine).getBytes());
        }
      }

      os.flush();
    }

    String response = "";
        try {
            response = read(conn.getInputStream());
        } catch (FileNotFoundException e) {
            // Error Stream contains JSON that we can parse to a FB error
            response = read(conn.getErrorStream());
        }
        return response;
  }

  private static String read(InputStream in) throws IOException {
        StringBuilder sb = new StringBuilder();
        BufferedReader r = new BufferedReader(new InputStreamReader(in), 1000);
        for (String line = r.readLine(); line != null; line = r.readLine()) {
            sb.append(line);
        }
        in.close();
        return sb.toString();
    }

  /*
   * percent encode
   */

  public static String percentEncodeBody(Bundle parameters, String boundary) {
    if (parameters == null)
      return "";

      StringBuilder sb = new StringBuilder();

          for (String key : parameters.keySet()) {
              if (parameters.getByteArray(key) != null) {
                  continue;
              }

              sb.append("Content-Disposition: form-data; name=\"" + key +
                    "\"\r\n\r\n" + parameters.getString(key));
              sb.append("\r\n" + "--" + boundary + "\r\n");
          }

          return sb.toString();
  }

  public static String percentEncode(Bundle parameters) {
    if (parameters == null) {
        return "";
    }

      StringBuilder sb = new StringBuilder();
      boolean first = true;
      for (String key : parameters.keySet()) {
        if (first) first = false; else sb.append("&");
            sb.append(URLEncoder.encode(key) + "=" +
                        URLEncoder.encode(parameters.getString(key)));
          }
          return sb.toString();
  }

  public static Bundle percentDecode(String s) {
    Bundle params = new Bundle();
    if (s != null) {
        String array[] = s.split("&");
        for (String parameter : array) {
          String v[] = parameter.split("=");
              params.putString(URLDecoder.decode(v[0]),
                      URLDecoder.decode(v[1]));
        }
      }
    return params;
  }


    /*
     * cookie
     */

    public static void clearCookies(Context context) {
        // Edge case: an illegal state exception is thrown if an instance of
        // CookieSyncManager has not be created.  CookieSyncManager is normally
        // created by a WebKit view, but this might happen if you start the
        // app, restore saved state, and click logout before running a UI
        // dialog in a WebView -- in which case the app crashes
        @SuppressWarnings("unused")
        CookieSyncManager cookieSyncMngr =
            CookieSyncManager.createInstance(context);
        CookieManager cookieManager = CookieManager.getInstance();
        cookieManager.removeAllCookie();
    }

}

   
    
    
    
  








Related examples in the same category

1.Http Connection
2.Using HttpGet to get the web response
3.Create Http connection
4.Http connection with
5.HttpGet and DefaultHttpClient
6.Http Post
7.Simple HTTP Request
8.Http Request Class
9.Http Get and Http Post
10.Get Text from HttpResponse
11.Http Request
12.Http Downloader
13.Is Http Url Available
14.Http Retriever
15.Receive Response from HttpURLConnection
16.Print http headers. Useful for debugging.
17.Return the base URL from the given URL. Example: http://foo.org/abc.html -> http://foo.org/
18.Send message with HttpPost
19.Get Http Stream
20.Generic Object Http Loader
21.Http Get and DefaultHttpClient
22.Gets http output from URL
23.Util for Http Get
24.do Http Get Request and return the status code
25.implements HttpClient
26.Get File From HTTP
27.Make all redirects to go to http in stead of https
28.New Http Client Manager
29.Http Client Manager
30.Multipart Post
31.Get Server Data
32.Yahoo News Crawler
33.Send Packet
34.Read a web page
35.parse Request Header
36.Data Send Utils
37.This class is in charge of synchronizing the events (with due dates) with Google Calendar
38.Update Favicon
39.Converts key-value pair to appropriate signature for Facebook.