diff options
| author | Samuel Tardieu <sam@rfc1149.net> | 2012-05-11 10:11:36 +0200 |
|---|---|---|
| committer | Samuel Tardieu <sam@rfc1149.net> | 2012-05-11 11:02:32 +0200 |
| commit | 7076e9ff9bc740416c6402cea23d36bfb3dceb14 (patch) | |
| tree | 54ed365b6d316b916b6e71e751756bb4b3a1b382 /main/src/cgeo/geocaching/network | |
| parent | e73ff29690963c87f3113477d46203d40a6db001 (diff) | |
| download | cgeo-7076e9ff9bc740416c6402cea23d36bfb3dceb14.zip cgeo-7076e9ff9bc740416c6402cea23d36bfb3dceb14.tar.gz cgeo-7076e9ff9bc740416c6402cea23d36bfb3dceb14.tar.bz2 | |
Add file upload capabilities
This is necessary for #583.
Diffstat (limited to 'main/src/cgeo/geocaching/network')
| -rw-r--r-- | main/src/cgeo/geocaching/network/Network.java | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/main/src/cgeo/geocaching/network/Network.java b/main/src/cgeo/geocaching/network/Network.java index 55f6377..2c84c9c 100644 --- a/main/src/cgeo/geocaching/network/Network.java +++ b/main/src/cgeo/geocaching/network/Network.java @@ -20,6 +20,9 @@ import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpRequestBase; import org.apache.http.entity.HttpEntityWrapper; +import org.apache.http.entity.mime.MultipartEntity; +import org.apache.http.entity.mime.content.FileBody; +import org.apache.http.entity.mime.content.StringBody; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.params.BasicHttpParams; import org.apache.http.params.CoreConnectionPNames; @@ -130,6 +133,36 @@ public abstract class Network { } /** + * Multipart POST HTTP request + * + * @param uri the URI to request + * @param params the parameters to add to the POST request + * @param fileFieldName the name of the file field name + * @param fileContentType the content-type of the file + * @param file the file to include in the request + * @return the HTTP response, or null in case of an encoding error param + */ + public static HttpResponse postRequest(final String uri, final Parameters params, + final String fileFieldName, final String fileContentType, final File file) { + final MultipartEntity entity = new MultipartEntity(); + for (final NameValuePair param : params) { + try { + entity.addPart(param.getName(), new StringBody(param.getValue())); + } catch (final UnsupportedEncodingException e) { + Log.e("Network.postRequest: unsupported encoding for parameter " + param.getName(), e); + return null; + } + } + entity.addPart(fileFieldName, new FileBody(file, fileContentType)); + + final HttpPost request = new HttpPost(uri); + request.setEntity(entity); + + addHeaders(request, null, null); + return doRepeatedRequests(request); + } + + /** * Make an HTTP request * * @param method |
