From eb7c5e7d7b332fb488658bb84066b0b3c516896e Mon Sep 17 00:00:00 2001 From: Joe Robinson Date: Mon, 4 Feb 2013 20:51:05 +0000 Subject: "Added http upload functionality --- AndroidManifest.xml | 2 +- gen/com/lc8n/blauploader/R.java | 129 +----------- libs/httpclient-4.2.3.jar | Bin 0 -> 433070 bytes libs/httpcore-4.2.2.jar | Bin 0 -> 223571 bytes libs/httpmime-4.2.3.jar | Bin 0 -> 26477 bytes src/com/lc8n/blauploader/FileShare.java | 266 +++++++++++++------------ src/com/lc8n/blauploader/FileUpload.java | 5 +- src/com/lc8n/util/FileUploader.java | 106 ++++++++++ src/com/lc8n/util/ProgressMultipartEntity.java | 71 +++++++ 9 files changed, 325 insertions(+), 254 deletions(-) create mode 100644 libs/httpclient-4.2.3.jar create mode 100644 libs/httpcore-4.2.2.jar create mode 100644 libs/httpmime-4.2.3.jar create mode 100644 src/com/lc8n/util/FileUploader.java create mode 100644 src/com/lc8n/util/ProgressMultipartEntity.java diff --git a/AndroidManifest.xml b/AndroidManifest.xml index fe7b93e..2941e0e 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -43,7 +43,7 @@ - + Must be a color value, in the form of "#rgb", "#argb", -"#rrggbb", or "#aarrggbb". -

This may also be a reference to a resource (in the form -"@[package:]type:name") or -theme attribute (in the form -"?[package:][type:]name") -containing a value of this type. - */ - public static final int exampleColor=0x7f010002; - /**

Must be a dimension value, which is a floating point number appended with a unit such as "14.5sp". -Available units are: px (pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size), -in (inches), mm (millimeters). -

This may also be a reference to a resource (in the form -"@[package:]type:name") or -theme attribute (in the form -"?[package:][type:]name") -containing a value of this type. - */ - public static final int exampleDimension=0x7f010001; - /**

May be a reference to another resource, in the form "@[+][package:]type:name" -or to a theme attribute in the form "?[package:][type:]name". -

May be a color value, in the form of "#rgb", "#argb", -"#rrggbb", or "#aarrggbb". - */ - public static final int exampleDrawable=0x7f010003; - /**

Must be a string value, using '\\;' to escape characters such as '\\n' or '\\uxxxx' for a unicode character. -

This may also be a reference to a resource (in the form -"@[package:]type:name") or -theme attribute (in the form -"?[package:][type:]name") -containing a value of this type. - */ - public static final int exampleString=0x7f010000; } public static final class drawable { public static final int binary=0x7f020000; @@ -96,16 +63,14 @@ containing a value of this type. public static final int uploadlocation=0x7f07001d; } public static final class layout { - public static final int activity_main=0x7f030000; - public static final int browser=0x7f030001; - public static final int filerow=0x7f030002; - public static final int home=0x7f030003; - public static final int sample_file_list_view=0x7f030004; - public static final int share=0x7f030005; - public static final int share2=0x7f030006; - public static final int soundrecorder=0x7f030007; - public static final int upload=0x7f030008; - public static final int uploadlocation=0x7f030009; + public static final int browser=0x7f030000; + public static final int filerow=0x7f030001; + public static final int home=0x7f030002; + public static final int share=0x7f030003; + public static final int share2=0x7f030004; + public static final int soundrecorder=0x7f030005; + public static final int upload=0x7f030006; + public static final int uploadlocation=0x7f030007; } public static final class menu { public static final int activity_main=0x7f060000; @@ -126,82 +91,4 @@ containing a value of this type. */ public static final int LightTheme=0x7f050000; } - public static final class styleable { - /** Attributes that can be used with a FileListView. -

Includes the following attributes:

- - - - - - - - -
AttributeDescription
{@link #FileListView_exampleColor com.lc8n.blauploader:exampleColor}
{@link #FileListView_exampleDimension com.lc8n.blauploader:exampleDimension}
{@link #FileListView_exampleDrawable com.lc8n.blauploader:exampleDrawable}
{@link #FileListView_exampleString com.lc8n.blauploader:exampleString}
- @see #FileListView_exampleColor - @see #FileListView_exampleDimension - @see #FileListView_exampleDrawable - @see #FileListView_exampleString - */ - public static final int[] FileListView = { - 0x7f010000, 0x7f010001, 0x7f010002, 0x7f010003 - }; - /** -

This symbol is the offset where the {@link com.lc8n.blauploader.R.attr#exampleColor} - attribute's value can be found in the {@link #FileListView} array. - - -

Must be a color value, in the form of "#rgb", "#argb", -"#rrggbb", or "#aarrggbb". -

This may also be a reference to a resource (in the form -"@[package:]type:name") or -theme attribute (in the form -"?[package:][type:]name") -containing a value of this type. - @attr name android:exampleColor - */ - public static final int FileListView_exampleColor = 2; - /** -

This symbol is the offset where the {@link com.lc8n.blauploader.R.attr#exampleDimension} - attribute's value can be found in the {@link #FileListView} array. - - -

Must be a dimension value, which is a floating point number appended with a unit such as "14.5sp". -Available units are: px (pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size), -in (inches), mm (millimeters). -

This may also be a reference to a resource (in the form -"@[package:]type:name") or -theme attribute (in the form -"?[package:][type:]name") -containing a value of this type. - @attr name android:exampleDimension - */ - public static final int FileListView_exampleDimension = 1; - /** -

This symbol is the offset where the {@link com.lc8n.blauploader.R.attr#exampleDrawable} - attribute's value can be found in the {@link #FileListView} array. - - -

May be a reference to another resource, in the form "@[+][package:]type:name" -or to a theme attribute in the form "?[package:][type:]name". -

May be a color value, in the form of "#rgb", "#argb", -"#rrggbb", or "#aarrggbb". - @attr name android:exampleDrawable - */ - public static final int FileListView_exampleDrawable = 3; - /** -

This symbol is the offset where the {@link com.lc8n.blauploader.R.attr#exampleString} - attribute's value can be found in the {@link #FileListView} array. - - -

Must be a string value, using '\\;' to escape characters such as '\\n' or '\\uxxxx' for a unicode character. -

This may also be a reference to a resource (in the form -"@[package:]type:name") or -theme attribute (in the form -"?[package:][type:]name") -containing a value of this type. - @attr name android:exampleString - */ - public static final int FileListView_exampleString = 0; - }; } diff --git a/libs/httpclient-4.2.3.jar b/libs/httpclient-4.2.3.jar new file mode 100644 index 0000000..e379af3 Binary files /dev/null and b/libs/httpclient-4.2.3.jar differ diff --git a/libs/httpcore-4.2.2.jar b/libs/httpcore-4.2.2.jar new file mode 100644 index 0000000..a64cd2f Binary files /dev/null and b/libs/httpcore-4.2.2.jar differ diff --git a/libs/httpmime-4.2.3.jar b/libs/httpmime-4.2.3.jar new file mode 100644 index 0000000..c91c219 Binary files /dev/null and b/libs/httpmime-4.2.3.jar differ diff --git a/src/com/lc8n/blauploader/FileShare.java b/src/com/lc8n/blauploader/FileShare.java index f03e37f..007cfdb 100644 --- a/src/com/lc8n/blauploader/FileShare.java +++ b/src/com/lc8n/blauploader/FileShare.java @@ -5,7 +5,6 @@ import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; -import java.io.InputStream; import java.io.OutputStream; import android.app.Activity; @@ -39,14 +38,15 @@ import android.widget.Button; import android.widget.CheckBox; import android.widget.EditText; import android.widget.ImageView; -import android.widget.Toast; + +import com.lc8n.util.FileUploader; public class FileShare extends Activity { private EditText text; - private float fileSize; + private long fileSize; private Handler pbHandle = null; - private ProgressDialog progressDialog; - private ProgressDialog preparing; + //private ProgressDialog progressDialog; + //private ProgressDialog preparing; private String fileName; private Thread thread; private AlertDialog alert; @@ -54,7 +54,7 @@ public class FileShare extends Activity { private File blaFile; private ImageView thumbView; private int imgRotate = 0; - + private Context context = this; public void onCreate(Bundle savedInstanceState) { @@ -87,130 +87,130 @@ public class FileShare extends Activity { }); //Set up progress dialog which will show upload progress - progressDialog = new ProgressDialog(this); - - progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); - progressDialog.setMessage("Blauploading..."); - progressDialog.setCancelable(true); - progressDialog.setProgress(0); - - - progressDialog.setButton(AlertDialog.BUTTON_NEGATIVE, getText(R.string.cancel), new DialogInterface.OnClickListener() { - - public void onClick(DialogInterface dialog, int which) { - // Cancel the upload! - progressDialog.dismiss(); - thread.interrupt(); - - - } - }); +// progressDialog = new ProgressDialog(this); +// +// progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); +// progressDialog.setMessage("Blauploading..."); +// progressDialog.setCancelable(true); +// progressDialog.setProgress(0); +// +// +// progressDialog.setButton(AlertDialog.BUTTON_NEGATIVE, getText(R.string.cancel), new DialogInterface.OnClickListener() { +// +// public void onClick(DialogInterface dialog, int which) { +// // Cancel the upload! +// progressDialog.dismiss(); +// thread.interrupt(); +// +// +// } +// }); //Set up preparing dialog which will be shown when connecting to FTP - preparing = new ProgressDialog(this); - preparing.setProgressStyle(ProgressDialog.STYLE_SPINNER); - preparing.setMessage("Preparing to Blaupload..."); - preparing.setCancelable(true); - preparing.setButton(AlertDialog.BUTTON_NEGATIVE, getText(R.string.cancel), new DialogInterface.OnClickListener() { - - public void onClick(DialogInterface dialog, int which) { - // Cancel the upload! - progressDialog.dismiss(); - thread.interrupt(); - - } - }); - +// preparing = new ProgressDialog(this); +// preparing.setProgressStyle(maxProgressDialog.STYLE_SPINNER); +// preparing.setMessage("Preparing to Blaupload..."); +// preparing.setCancelable(true); +// preparing.setButton(AlertDialog.BUTTON_NEGATIVE, getText(R.string.cancel), new DialogInterface.OnClickListener() { +// +// public void onClick(DialogInterface dialog, int which) { +// // Cancel the upload! +// progressDialog.dismiss(); +// thread.interrupt(); +// +// } +// }); +// //Handles responses from the FTP thread - pbHandle = new Handler(){ - - @Override - public void handleMessage(Message msg) { - - //Get the current progress (file size completed in bytes) - long progress = msg.getData().getLong("progress_update"); - float percent = (progress/fileSize)*100.0f; - Integer intProgress = Math.round(percent); - - int kB = Math.round(progress / 1024); - - //TODO: Make this not terrible - //Checks for filesize - 10kB because that's the last guaranteed response - //But there might be more, which would result in multiple SMSs - //Need a nicer way of cleaning up when it's done - if(intProgress==100 || progress>(fileSize-10240)) { - SmsManager sms = SmsManager.getDefault(); - sms.sendTextMessage("07927278978", null, "New image blauploaded: http://www.blaupload.co.uk/"+fileName,null, null); - blaFile.delete(); - progressDialog.dismiss(); - } - else { - //Really not sure why this is in an else - //Close the prepare dialog and start the progress dialog - preparing.dismiss(); - progressDialog.show(); - progressDialog.setProgress(kB); - } - - int code = msg.getData().getInt("response"); - - //Handle FTP response codes - switch (code) { - case 0: - break; - case 600: - alert.setMessage("File Already Exists! Please Rename"); - alert.show(); - progressDialog.dismiss(); - thread.interrupt(); - break; - case 226: - //This is the response code for complete, but we usually don't receive it - //TODO: Make sure we receive it - //SmsManager sms = SmsManager.getDefault(); - //sms.sendTextMessage("07927278978", null, "New image blauploaded: http://www.blaupload.co.uk/"+fileName,null, null); - progressDialog.dismiss(); - break; - case 800: - alert.setMessage("Unable To Connect To Server!"); - alert.show(); - progressDialog.dismiss(); - break; - case 801: - alert.setMessage("Unable To Log In!"); - alert.show(); - progressDialog.dismiss(); - break; - case 804: - alert.setMessage("Unable To Get File List!"); - alert.show(); - progressDialog.dismiss(); - break; - case 805: - alert.setMessage("Unable To Get Upload File!"); - alert.show(); - progressDialog.dismiss(); - break; - case 806: - alert.setMessage("FTP Server Did Not Respond!"); - alert.show(); - progressDialog.dismiss(); - break; - case 807: - alert.setMessage("Unable To Log Out!"); - alert.show(); - progressDialog.dismiss(); - break; - default: - alert.setMessage("Response Code: "+code); - alert.show(); - progressDialog.dismiss(); - break; - - } - } - }; +// pbHandle = new Handler(){ +// +// @Override +// public void handleMessage(Message msg) { +// +// //Get the current progress (file size completed in bytes) +// long progress = msg.getData().getLong("progress_update"); +// float percent = (progress/fileSize)*100.0f; +// Integer intProgress = Math.round(percent); +// +// int kB = Math.round(progress / 1024); +// +// //TODO: Make this not terrible +// //Checks for filesize - 10kB because that's the last guaranteed response +// //But there might be more, which would result in multiple SMSs +// //Need a nicer way of cleaning up when it's done +// if(intProgress==100 || progress>(fileSize-10240)) { +// SmsManager sms = SmsManager.getDefault(); +// sms.sendTextMessage("07927278978", null, "New image blauploaded: http://www.blaupload.co.uk/"+fileName,null, null); +// blaFile.delete(); +// progressDialog.dismiss(); +// } +// else { +// //Really not sure why this is in an else +// //Close the prepare dialog and start the progress dialog +// preparing.dismiss(); +// progressDialog.show(); +// progressDialog.setProgress(kB); +// } +// +// int code = msg.getData().getInt("response"); +// +// //Handle FTP response codes +// switch (code) { +// case 0: +// break; +// case 600: +// alert.setMessage("File Already Exists! Please Rename"); +// alert.show(); +// progressDialog.dismiss(); +// thread.interrupt(); +// break; +// case 226: +// //This is the response code for complete, but we usually don't receive it +// //TODO: Make sure we receive it +// //SmsManager sms = SmsManager.getDefault(); +// //sms.sendTextMessage("07927278978", null, "New image blauploaded: http://www.blaupload.co.uk/"+fileName,null, null); +// progressDialog.dismiss(); +// break; +// case 800: +// alert.setMessage("Unable To Connect To Server!"); +// alert.show(); +// progressDialog.dismiss(); +// break; +// case 801: +// alert.setMessage("Unable To Log In!"); +// alert.show(); +// progressDialog.dismiss(); +// break; +// case 804: +// alert.setMessage("Unable To Get File List!"); +// alert.show(); +// progressDialog.dismiss(); +// break; +// case 805: +// alert.setMessage("Unable To Get Upload File!"); +// alert.show(); +// progressDialog.dismiss(); +// break; +// case 806: +// alert.setMessage("FTP Server Did Not Respond!"); +// alert.show(); +// progressDialog.dismissmax(); +// break; +// case 807: +// alert.setMessage("Unable To Log Out!"); +// alert.show(); +// progressDialog.dismiss(); +// break; +// default: +// alert.setMessage("Response Code: "+code); +// alert.show(); +// progressDialog.dismiss(); +// break; +// +// } +// } +// }; //Handler for "Upload" button //This is where the magic happens @@ -221,7 +221,7 @@ public class FileShare extends Activity { //Show the preparing dialog while this is all happening //TODO: There is lag between pressing upload and this happening. Why? - preparing.show(); + //preparing.show(); //Check box for resizing/reducing quality of image CheckBox checkBox = (CheckBox)findViewById(R.id.resize); @@ -260,10 +260,14 @@ public class FileShare extends Activity { fileSize = blaFile.length(); //Start the upload - FileUpload uploader = new FileUpload(blaFile, pbHandle); - thread = new Thread(uploader); - thread.start(); - progressDialog.setMax(Math.round(fileSize/1024)); +// FileUpload uploader = new FileUpload(blaFile, pbHandle); + FileUploader uploader2 = new FileUploader(context); + uploader2.prepare((int)fileSize/1024); + uploader2.execute(blaFile); + +// thread = new Thread(uploader); +// thread.start(); +// progressDialog.setMax(Math.round(fileSize/1024)); } diff --git a/src/com/lc8n/blauploader/FileUpload.java b/src/com/lc8n/blauploader/FileUpload.java index 1d41f8c..2eaea4a 100644 --- a/src/com/lc8n/blauploader/FileUpload.java +++ b/src/com/lc8n/blauploader/FileUpload.java @@ -59,10 +59,13 @@ public class FileUpload implements Runnable{ } + public void httpUpload (File file, Handler pbHandle) { + + } + public void uploadFile(File file, Handler pbHandle) { - int code = 0; FileInputStream fis = null; try { diff --git a/src/com/lc8n/util/FileUploader.java b/src/com/lc8n/util/FileUploader.java new file mode 100644 index 0000000..815c33a --- /dev/null +++ b/src/com/lc8n/util/FileUploader.java @@ -0,0 +1,106 @@ +/** + * + */ +package com.lc8n.util; + +import java.io.File; + +import org.apache.http.HttpResponse; +import org.apache.http.client.HttpClient; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.entity.mime.content.FileBody; +import org.apache.http.impl.client.DefaultHttpClient; +import org.apache.http.protocol.BasicHttpContext; +import org.apache.http.protocol.HttpContext; +import org.apache.http.util.EntityUtils; + +import android.app.ProgressDialog; +import android.content.Context; +import android.os.AsyncTask; +import android.util.Log; + +import com.lc8n.util.ProgressMultipartEntity; +import com.lc8n.util.ProgressMultipartEntity.ProgressListener; + + +/** + * @author joe + * + */ +public class FileUploader extends AsyncTask { + + ProgressDialog pd; + int totalSize; + protected Context mContext; + + public FileUploader(Context context) { + + mContext = context; + } + @Override + protected void onPreExecute() + { + + } + + public void prepare(int max) { + pd = new ProgressDialog(mContext); + pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); + pd.setMessage("Uploading File..."); + pd.setCancelable(false); + pd.setMax(max); + pd.show(); + } + + @Override + protected String doInBackground(File... file) { + + HttpClient httpClient = new DefaultHttpClient(); + HttpContext httpContext = new BasicHttpContext(); + HttpPost httpPost = new HttpPost("http://www.blaupload.co.uk/upload_file.php"); + Log.d("File", file[0].getName()); + + try + { + ProgressMultipartEntity multipartContent = new ProgressMultipartEntity(new ProgressListener() + { + + public void transferred(long num) + { + Log.d("Progress", String.valueOf(num)); + publishProgress((int)(num/1024)); + } + }); + + // We use FileBody to transfer an image + multipartContent.addPart("file", new FileBody(file[0])); + + totalSize = (int)(multipartContent.getContentLength()/1024); + Log.d("Size", String.valueOf(totalSize)); + // Send it + httpPost.setEntity(multipartContent); + HttpResponse response = httpClient.execute(httpPost, httpContext); + String serverResponse = EntityUtils.toString(response.getEntity()); + + return serverResponse; + } + + catch (Exception e) + { + System.out.println(e); + } + return null; + } + + @Override + protected void onProgressUpdate(Integer... progress) + { + pd.setProgress((int) (progress[0])); + } + + @Override + protected void onPostExecute(String a) + { + pd.dismiss(); + } +} diff --git a/src/com/lc8n/util/ProgressMultipartEntity.java b/src/com/lc8n/util/ProgressMultipartEntity.java new file mode 100644 index 0000000..fefe053 --- /dev/null +++ b/src/com/lc8n/util/ProgressMultipartEntity.java @@ -0,0 +1,71 @@ +package com.lc8n.util; + +import java.io.FilterOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.nio.charset.Charset; +import org.apache.http.entity.mime.HttpMultipartMode; +import org.apache.http.entity.mime.MultipartEntity; + +public class ProgressMultipartEntity extends MultipartEntity +{ + + private final ProgressListener listener; + + public ProgressMultipartEntity(final ProgressListener listener) + { + super(); + this.listener = listener; + } + + public ProgressMultipartEntity(final HttpMultipartMode mode, final ProgressListener listener) + { + super(mode); + this.listener = listener; + } + + public ProgressMultipartEntity(HttpMultipartMode mode, final String boundary, final Charset charset, final ProgressListener listener) + { + super(mode, boundary, charset); + this.listener = listener; + } + + @Override + public void writeTo(final OutputStream outstream) throws IOException + { + super.writeTo(new CountingOutputStream(outstream, this.listener)); + } + + public static interface ProgressListener + { + void transferred(long num); + } + + public static class CountingOutputStream extends FilterOutputStream + { + + private final ProgressListener listener; + private long transferred; + + public CountingOutputStream(final OutputStream out, final ProgressListener listener) + { + super(out); + this.listener = listener; + this.transferred = 0; + } + + public void write(byte[] b, int off, int len) throws IOException + { + out.write(b, off, len); + this.transferred += len; + this.listener.transferred(this.transferred); + } + + public void write(int b) throws IOException + { + out.write(b); + this.transferred++; + this.listener.transferred(this.transferred); + } + } +} \ No newline at end of file -- cgit v1.2.3