Skip to content

Commit

Permalink
Adding SimpleDownload example
Browse files Browse the repository at this point in the history
  • Loading branch information
Enrique Diaz committed Jul 7, 2011
1 parent 34a66be commit 230c65c
Show file tree
Hide file tree
Showing 132 changed files with 5,372 additions and 58 deletions.
745 changes: 745 additions & 0 deletions .metadata/.log

Large diffs are not rendered by default.

@@ -0,0 +1,83 @@
package org.androidtitlan.itam.simpledownload;

import java.io.File;
import java.io.FileOutputStream;

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

import android.app.Activity;
import android.app.IntentService;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.os.Message;
import android.os.Messenger;
import android.util.Log;

public class SimpleDownload extends IntentService {
public static final String EXTRA_MESSENGER="org.androidtitlan.itam.EXTRA_MESSENGER";

private HttpClient mHttpClient = null;

public SimpleDownload() {
super("SimpleDownload");
}

@Override
public void onCreate() {
super.onCreate();
mHttpClient = new DefaultHttpClient();
}
@Override
public void onDestroy() {
super.onDestroy();
mHttpClient.getConnectionManager().shutdown();
}

@Override
protected void onHandleIntent(Intent i) {
HttpGet getMethod= new HttpGet(i.getData().toString());
int result = Activity.RESULT_CANCELED;

try{
ResponseHandler<byte[]> mResponseHandler = new ByteArrayResponseHandler();
byte[] responseBody = mHttpClient.execute(getMethod, mResponseHandler);
File output=new File(Environment.getExternalStorageDirectory(),
i.getData().getLastPathSegment());

if(output.exists()){
output.delete();
}

FileOutputStream fos = FileOutputStream(output.getPath());
fos.write(responseBody);
fos.close();
result = Activity.RESULT_OK;

catch (Exception e2) {
Log.e(getClass().getName(), "Exception in download", e2);
}
Bundle extras = i.getExtras();

if(extras!=null){
Messenger messenger = (Messenger)extras.get(EXTRA_MESSENGER);
Message msg = Message.obtain();

msg.arg1 = result;
try{
messenger.send(msg);
}
catch(android.os.RemoteException e1){
Log.w(getClass().getName(), "Exception sending Message",e1);

}
}
}





@@ -0,0 +1,67 @@
package org.androidtitlan.itam.simpledownload;

import java.io.File;
import java.io.FileOutputStream;

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

import android.app.Activity;
import android.app.IntentService;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;

public class SimpleDownload extends IntentService {
public static final String EXTRA_MESSENGER="org.androidtitlan.itam.EXTRA_MESSENGER";

private HttpClient mHttpClient = null;

public SimpleDownload() {
super("SimpleDownload");
}

@Override
public void onCreate() {
super.onCreate();
mHttpClient = new DefaultHttpClient();
}
@Override
public void onDestroy() {
super.onDestroy();
mHttpClient.getConnectionManager().shutdown();
}

@Override
protected void onHandleIntent(Intent i) {
HttpGet getMethod= new HttpGet(i.getData().toString());
int result = Activity.RESULT_CANCELED;

try{
ResponseHandler<byte[]> mResponseHandler = new ByteArrayResponseHandler();
byte[] responseBody = mHttpClient.execute(getMethod, mResponseHandler);
File output=new File(Environment.getExternalStorageDirectory(),
i.getData().getLastPathSegment());

if(output.exists()){
output.delete();
}

FileOutputStream fos = FileOutputStream(output.getPath());
fos.write(responseBody);
fos.close();
result = Activity.RESULT_OK;

catch (Exception e2) {
Log.e(getClass().getName(), "Exception in download", e2);
}
Bundle extras = i.getExtras();




}
}
@@ -0,0 +1,58 @@
package org.androidtitlan.itam.simpledownload;

import java.io.File;
import java.io.FileOutputStream;

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

import android.app.Activity;
import android.app.IntentService;
import android.content.Intent;
import android.os.Environment;

public class SimpleDownload extends IntentService {
public static final String EXTRA_MESSENGER="org.androidtitlan.itam.EXTRA_MESSENGER";

private HttpClient mHttpClient = null;

public SimpleDownload() {
super("SimpleDownload");
}

@Override
public void onCreate() {
super.onCreate();
mHttpClient = new DefaultHttpClient();
}
@Override
public void onDestroy() {
super.onDestroy();
mHttpClient.getConnectionManager().shutdown();
}

@Override
protected void onHandleIntent(Intent i) {
HttpGet getMethod= new HttpGet(i.getData().toString());
int result = Activity.RESULT_CANCELED;

try{
ResponseHandler<byte[]> mResponseHandler = new ByteArrayResponseHandler();
byte[] responseBody = mHttpClient.execute(getMethod, mResponseHandler);
File output=new File(Environment.getExternalStorageDirectory(),
i.getData().getLastPathSegment());

if(output.exists()){
output.delete();
}

FileOutputStream fos = FileOutputStream(output.getPath());
fos.write(responseBody);
fos.close();
}
}


}
@@ -0,0 +1,51 @@
package org.androidtitlan.itam.simpledownload;

import java.io.File;

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

import android.app.Activity;
import android.app.IntentService;
import android.content.Intent;
import android.os.Environment;

public class SimpleDownload extends IntentService {
public static final String EXTRA_MESSENGER="org.androidtitlan.itam.EXTRA_MESSENGER";

private HttpClient mHttpClient = null;
public SimpleDownload(String name) {

super(name);
}
@Override
public void onCreate() {
super.onCreate();
mHttpClient = new DefaultHttpClient();
}
@Override
public void onDestroy() {
super.onDestroy();
mHttpClient.getConnectionManager().shutdown();
}

@Override
protected void onHandleIntent(Intent intent) {
HttpGet getMethod= new HttpGet(i.getData().toString());
int result =Activity.RESULT_CANCELED;

try{
ResponseHandler<byte[]> responseHandler = new ByteArrayResponseHandler();
byte[] responseBody = mHttpClient.execute(getMethod, responseHandler);
File output=new File(Environment.getExternalStorageDirectory(),i.getData().getLastPathSegment());

if(output.exists()){
output.delete();
}
}
}


}
@@ -0,0 +1,84 @@
package org.androidtitlan.itam.simpledownload;

import java.io.File;
import java.io.FileOutputStream;

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

import android.app.Activity;
import android.app.IntentService;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.os.Message;
import android.os.Messenger;
import android.util.Log;

public class SimpleDownload extends IntentService {
public static final String EXTRA_MESSENGER="org.androidtitlan.itam.EXTRA_MESSENGER";

private HttpClient mHttpClient = null;

public SimpleDownload() {
super("SimpleDownload");
}

@Override
public void onCreate() {
super.onCreate();
mHttpClient = new DefaultHttpClient();
}
@Override
public void onDestroy() {
super.onDestroy();
mHttpClient.getConnectionManager().shutdown();
}

@Override
protected void onHandleIntent(Intent i) {
HttpGet getMethod= new HttpGet(i.getData().toString());
int result = Activity.RESULT_CANCELED;

try{
ResponseHandler<byte[]> mResponseHandler = new ByteArrayResponseHandler();
byte[] responseBody = mHttpClient.execute(getMethod, mResponseHandler);
File output=new File(Environment.getExternalStorageDirectory(),
i.getData().getLastPathSegment());

if(output.exists()){
output.delete();
}

FileOutputStream fos = FileOutputStream(output.getPath());
fos.write(responseBody);
fos.close();
result = Activity.RESULT_OK;
}

catch (Exception e2) {
Log.e(getClass().getName(), "Exception in download", e2);
}
Bundle extras = i.getExtras();

if(extras!=null){
Messenger messenger = (Messenger)extras.get(EXTRA_MESSENGER);
Message msg = Message.obtain();

msg.arg1 = result;
try{
messenger.send(msg);
}
catch(android.os.RemoteException e1){
Log.w(getClass().getName(), "Exception sending Message",e1);

}
}
}





0 comments on commit 230c65c

Please sign in to comment.