Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 596b8cb

Browse files
author
Elias Naur
committed
support custom HTTP method in addition to the standard (POST)
1 parent c1db4c1 commit 596b8cb

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

src/com/alexbbb/uploadservice/UploadRequest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
public class UploadRequest {
1717

1818
private UploadNotificationConfig notificationConfig;
19+
private String method = "POST";
1920
private final Context context;
2021
private final String uploadId;
2122
private final String url;
@@ -89,6 +90,14 @@ public void addArrayParameter(final String paramName, final List<String> list) {
8990
}
9091
}
9192

93+
public void setMethod(String method) {
94+
this.method = method;
95+
}
96+
97+
protected String getMethod() {
98+
return method;
99+
}
100+
92101
protected String getUploadId() {
93102
return uploadId;
94103
}

src/com/alexbbb/uploadservice/UploadService.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public class UploadService extends IntentService {
4343
protected static final String PARAM_NOTIFICATION_CONFIG = "notificationConfig";
4444
protected static final String PARAM_ID = "id";
4545
protected static final String PARAM_URL = "url";
46+
protected static final String PARAM_METHOD = "method";
4647
protected static final String PARAM_FILES = "files";
4748
protected static final String PARAM_REQUEST_HEADERS = "requestHeaders";
4849
protected static final String PARAM_REQUEST_PARAMETERS = "requestParameters";
@@ -86,6 +87,7 @@ public static void startUpload(final UploadRequest task)
8687
intent.putExtra(PARAM_NOTIFICATION_CONFIG, task.getNotificationConfig());
8788
intent.putExtra(PARAM_ID, task.getUploadId());
8889
intent.putExtra(PARAM_URL, task.getServerUrl());
90+
intent.putExtra(PARAM_METHOD, task.getMethod());
8991
intent.putParcelableArrayListExtra(PARAM_FILES, task.getFilesToUpload());
9092
intent.putParcelableArrayListExtra(PARAM_REQUEST_HEADERS, task.getHeaders());
9193
intent.putParcelableArrayListExtra(PARAM_REQUEST_PARAMETERS, task.getParameters());
@@ -116,6 +118,7 @@ protected void onHandleIntent(Intent intent) {
116118
notificationConfig = intent.getParcelableExtra(PARAM_NOTIFICATION_CONFIG);
117119
final String uploadId = intent.getStringExtra(PARAM_ID);
118120
final String url = intent.getStringExtra(PARAM_URL);
121+
final String method = intent.getStringExtra(PARAM_METHOD);
119122
final ArrayList<FileToUpload> files = intent.getParcelableArrayListExtra(PARAM_FILES);
120123
final ArrayList<NameValue> headers = intent.getParcelableArrayListExtra(PARAM_REQUEST_HEADERS);
121124
final ArrayList<NameValue> parameters = intent.getParcelableArrayListExtra(PARAM_REQUEST_PARAMETERS);
@@ -124,7 +127,7 @@ protected void onHandleIntent(Intent intent) {
124127
wakeLock.acquire();
125128
try {
126129
createNotification();
127-
handleFileUpload(uploadId, url, files, headers, parameters);
130+
handleFileUpload(uploadId, url, method, files, headers, parameters);
128131
} catch (Exception exception) {
129132
broadcastError(uploadId, exception);
130133
} finally {
@@ -136,6 +139,7 @@ protected void onHandleIntent(Intent intent) {
136139

137140
private void handleFileUpload(final String uploadId,
138141
final String url,
142+
final String method,
139143
final ArrayList<FileToUpload> filesToUpload,
140144
final ArrayList<NameValue> requestHeaders,
141145
final ArrayList<NameValue> requestParameters)
@@ -148,7 +152,7 @@ private void handleFileUpload(final String uploadId,
148152
OutputStream requestStream = null;
149153

150154
try {
151-
conn = getMultipartHttpURLConnection(url, boundary);
155+
conn = getMultipartHttpURLConnection(url, method, boundary);
152156

153157
setRequestHeaders(conn, requestHeaders);
154158

@@ -205,14 +209,15 @@ private byte[] getTrailerBytes(final String boundary)
205209
}
206210

207211
private HttpURLConnection getMultipartHttpURLConnection(final String url,
212+
final String method,
208213
final String boundary)
209214
throws IOException {
210215
final HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
211216

212217
conn.setDoInput(true);
213218
conn.setDoOutput(true);
214219
conn.setUseCaches(false);
215-
conn.setRequestMethod("POST");
220+
conn.setRequestMethod(method);
216221
conn.setRequestProperty("Connection", "Keep-Alive");
217222
conn.setRequestProperty("ENCTYPE", "multipart/form-data");
218223
conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);

0 commit comments

Comments
 (0)