blob: 815c33af654f866f1483755bd068d6d0f015d7b3 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
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<File, Integer, String> {
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();
}
}
|