Skip to content

Commit 070372c

Browse files
committed
Sample of POST multiple files (5) as Multipart-encoded data using RequestParams
1 parent 25023d8 commit 070372c

File tree

4 files changed

+70
-2
lines changed

4 files changed

+70
-2
lines changed

sample/src/main/AndroidManifest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
<activity android:name=".SynchronousClientSample"/>
3232
<activity android:name=".IntentServiceSample"/>
3333
<activity android:name=".SaxSample"/>
34+
<activity android:name=".FilesSample"/>
3435

3536
<service android:name=".services.ExampleIntentService"/>
3637
</application>
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.loopj.android.http.sample;
2+
3+
import android.util.Log;
4+
5+
import com.loopj.android.http.AsyncHttpClient;
6+
import com.loopj.android.http.RequestHandle;
7+
import com.loopj.android.http.RequestParams;
8+
import com.loopj.android.http.ResponseHandlerInterface;
9+
10+
import org.apache.http.Header;
11+
import org.apache.http.HttpEntity;
12+
13+
import java.io.File;
14+
import java.io.FileNotFoundException;
15+
import java.io.FileOutputStream;
16+
import java.util.Random;
17+
18+
public class FilesSample extends PostSample {
19+
20+
public static final String LOG_TAG = "PostFilesSample";
21+
22+
@Override
23+
public int getSampleTitle() {
24+
return R.string.title_post_files;
25+
}
26+
27+
@Override
28+
public boolean isRequestBodyAllowed() {
29+
return false;
30+
}
31+
32+
@Override
33+
public RequestHandle executeSample(AsyncHttpClient client, String URL, Header[] headers, HttpEntity entity, ResponseHandlerInterface responseHandler) {
34+
try {
35+
RequestParams params = new RequestParams();
36+
params.put("fileOne", createTempFile("fileOne", 20));
37+
params.put("fileTwo", createTempFile("fileTwo", 30));
38+
params.put("fileThree", createTempFile("fileThree", 40));
39+
params.put("fileFour", createTempFile("fileFour", 50));
40+
params.put("fileFive", createTempFile("fileFive", 60));
41+
return client.post(this, URL, params, responseHandler);
42+
} catch (FileNotFoundException fnfException) {
43+
Log.e(LOG_TAG, "executeSample failed with FileNotFoundException", fnfException);
44+
}
45+
return null;
46+
}
47+
48+
public File createTempFile(String namePart, int byteSize) {
49+
try {
50+
File f = File.createTempFile(namePart, "_handled", getCacheDir());
51+
FileOutputStream fos = new FileOutputStream(f);
52+
Random r = new Random();
53+
byte[] buffer = new byte[byteSize];
54+
r.nextBytes(buffer);
55+
fos.write(buffer);
56+
fos.flush();
57+
fos.close();
58+
return f;
59+
} catch (Throwable t) {
60+
Log.e(LOG_TAG, "createTempFile failed", t);
61+
}
62+
return null;
63+
}
64+
}

sample/src/main/java/com/loopj/android/http/sample/WaypointsActivity.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ public class WaypointsActivity extends ListActivity {
4545
R.string.title_cancel_all,
4646
R.string.title_cancel_handle,
4747
R.string.title_synchronous,
48-
R.string.title_intent_service_sample
48+
R.string.title_intent_service_sample,
49+
R.string.title_post_files
4950
};
5051
private static final Class[] targets = {
5152
GetSample.class,
@@ -62,7 +63,8 @@ public class WaypointsActivity extends ListActivity {
6263
CancelAllRequestsSample.class,
6364
CancelRequestHandleSample.class,
6465
SynchronousClientSample.class,
65-
IntentServiceSample.class
66+
IntentServiceSample.class,
67+
FilesSample.class
6668
};
6769

6870
@Override

sample/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
<string name="title_threading_timeout">Threading timeouts</string>
1717
<string name="title_gzip_sample">GET Gzipped JSON and parse it</string>
1818
<string name="title_intent_service_sample">IntentService Synchronised Request</string>
19+
<string name="title_post_files">Post Multipart-encoded files</string>
1920
<string name="button_run">Run</string>
2021
<string name="label_headers">Headers (key=val, one per line)</string>
2122
<string name="label_req_body">Request body</string>

0 commit comments

Comments
 (0)