Android Open Source - phonegap-lite-android Http Handler






From Project

Back to project page phonegap-lite-android.

License

The source code is released under:

MIT License

If you think the Android project phonegap-lite-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

/*
 * PhoneGap is available under *either* the terms of the modified BSD license *or* the
 * MIT License (2008). See http://opensource.org/licenses/alphabetical for full text.
 * /*from w ww  .  j  a v  a2  s . com*/
 * Copyright (c) 2005-2010, Nitobi Software Inc.
 * Copyright (c) 2010, IBM Corporation
 */
package com.phonegap;

import java.io.EOFException;
import java.io.FileOutputStream;
import java.io.InputStream;

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

public class HttpHandler {

  protected Boolean get(String url, String file)
  {
    HttpEntity entity = getHttpEntity(url);
    try {
      writeToDisk(entity, file);
    } catch (Exception e) { e.printStackTrace(); return false; }
    try {
      entity.consumeContent();
    } catch (Exception e) { e.printStackTrace(); return false; }
    return true;
  }
  
  private HttpEntity getHttpEntity(String url)
  /**
   * get the http entity at a given url
   */
  {
    HttpEntity entity=null;
    try {
      DefaultHttpClient httpclient = new DefaultHttpClient();
      HttpGet httpget = new HttpGet(url);
      HttpResponse response = httpclient.execute(httpget);
      entity = response.getEntity();
    } catch (Exception e) { e.printStackTrace(); return null; }
    return entity;
  }
  
  private void writeToDisk(HttpEntity entity, String file) throws EOFException
  /**
   * writes a HTTP entity to the specified filename and location on disk
   */
  {  
    int i=0;
    String FilePath="/sdcard/" + file;
    try {
      InputStream in = entity.getContent();
      byte buff[] = new byte[1024];    
      FileOutputStream out=
        new FileOutputStream(FilePath);
      do {
        int numread = in.read(buff);
        if (numread <= 0)
                     break;
        out.write(buff, 0, numread);
        i++;
      } while (true);
      out.flush();
      out.close();  
    } catch (Exception e) { e.printStackTrace(); }
  }
}




Java Source Code List

__ID__.Activity.java
com.phonegap.App.java
com.phonegap.CallbackServer.java
com.phonegap.Device.java
com.phonegap.DroidGap.java
com.phonegap.HttpHandler.java
com.phonegap.StandAlone.java
com.phonegap.TempListener.java
com.phonegap.WebViewReflect.java
com.phonegap.api.IPlugin.java
com.phonegap.api.LOG.java
com.phonegap.api.PhonegapActivity.java
com.phonegap.api.PluginManager.java
com.phonegap.api.PluginResult.java
com.phonegap.api.Plugin.java
com.phonegap.file.EncodingException.java
com.phonegap.plugin.sqlitePlugin.SQLitePlugin.java