diff options
Diffstat (limited to 'main/src')
| -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 |
