Skip to content

Commit 29ba1f0

Browse files
committed
naive implmentation of HTTP method PATCH
1 parent 85ffc37 commit 29ba1f0

File tree

6 files changed

+176
-1
lines changed

6 files changed

+176
-1
lines changed

library/src/main/java/com/loopj/android/http/AsyncHttpClient.java

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1065,7 +1065,77 @@ public RequestHandle put(Context context, String url, Header[] headers, HttpEnti
10651065
if (headers != null) request.setHeaders(headers);
10661066
return sendRequest(httpClient, httpContext, request, contentType, responseHandler, context);
10671067
}
1068+
1069+
/**
1070+
* Perform a HTTP PATCH request, without any parameters.
1071+
*
1072+
* @param url the URL to send the request to.
1073+
* @param responseHandler the response handler instance that should handle the response.
1074+
* @return RequestHandle of future request process
1075+
*/
1076+
public RequestHandle patch(String url, ResponseHandlerInterface responseHandler) {
1077+
return patch(null, url, null, responseHandler);
1078+
}
10681079

1080+
/**
1081+
* Perform a HTTP PATCH request with parameters.
1082+
*
1083+
* @param url the URL to send the request to.
1084+
* @param params additional PUT parameters or files to send with the request.
1085+
* @param responseHandler the response handler instance that should handle the response.
1086+
* @return RequestHandle of future request process
1087+
*/
1088+
public RequestHandle patch(String url, RequestParams params, ResponseHandlerInterface responseHandler) {
1089+
return patch(null, url, params, responseHandler);
1090+
}
1091+
1092+
/**
1093+
* Perform a HTTP PATCH request and track the Android Context which initiated the request.
1094+
*
1095+
* @param context the Android Context which initiated the request.
1096+
* @param url the URL to send the request to.
1097+
* @param params additional PUT parameters or files to send with the request.
1098+
* @param responseHandler the response handler instance that should handle the response.
1099+
* @return RequestHandle of future request process
1100+
*/
1101+
public RequestHandle patch(Context context, String url, RequestParams params, ResponseHandlerInterface responseHandler) {
1102+
return patch(context, url, paramsToEntity(params, responseHandler), null, responseHandler);
1103+
}
1104+
1105+
/**
1106+
* Perform a HTTP PATCH request and track the Android Context which initiated the request.
1107+
*
1108+
* @param context the Android Context which initiated the request.
1109+
* @param url the URL to send the request to.
1110+
* @param params additional PUT parameters or files to send with the request.
1111+
* @param responseHandler the response handler instance that should handle the response.
1112+
* @return RequestHandle of future request process
1113+
*/
1114+
public RequestHandle patch(Context context, String url, HttpEntity entity, String contentType, ResponseHandlerInterface responseHandler) {
1115+
return sendRequest(httpClient, httpContext, addEntityToRequestBase(new HttpPatch(URI.create(url).normalize()), entity), contentType, responseHandler, context);
1116+
}
1117+
1118+
/**
1119+
* Perform a HTTP PATCH request and track the Android Context which initiated the request. And set
1120+
* one-time headers for the request
1121+
*
1122+
* @param context the Android Context which initiated the request.
1123+
* @param url the URL to send the request to.
1124+
* @param headers set one-time headers for this request
1125+
* @param entity a raw {@link HttpEntity} to send with the request, for example, use
1126+
* this to send string/json/xml payloads to a server by passing a {@link
1127+
* org.apache.http.entity.StringEntity}.
1128+
* @param contentType the content type of the payload you are sending, for example
1129+
* application/json if sending a json payload.
1130+
* @param responseHandler the response handler instance that should handle the response.
1131+
* @return RequestHandle of future request process
1132+
*/
1133+
public RequestHandle patch(Context context, String url, Header[] headers, HttpEntity entity, String contentType, ResponseHandlerInterface responseHandler) {
1134+
HttpEntityEnclosingRequestBase request = addEntityToRequestBase(new HttpPatch(URI.create(url).normalize()), entity);
1135+
if (headers != null) request.setHeaders(headers);
1136+
return sendRequest(httpClient, httpContext, request, contentType, responseHandler, context);
1137+
}
1138+
10691139
// [-] HTTP PUT
10701140
// [+] HTTP DELETE
10711141

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.loopj.android.http;
2+
3+
import java.net.URI;
4+
5+
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
6+
7+
public final class HttpPatch extends HttpEntityEnclosingRequestBase {
8+
9+
public final static String METHOD_NAME = "PATCH";
10+
11+
public HttpPatch() {
12+
super();
13+
}
14+
15+
public HttpPatch(final URI uri) {
16+
super();
17+
setURI(uri);
18+
}
19+
20+
/**
21+
* @throws IllegalArgumentException if the uri is invalid.
22+
*/
23+
public HttpPatch(final String uri) {
24+
super();
25+
setURI(URI.create(uri));
26+
}
27+
28+
@Override
29+
public String getMethod() {
30+
return METHOD_NAME;
31+
}
32+
33+
}

sample/src/main/AndroidManifest.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
<activity android:name=".PostSample"/>
2222
<activity android:name=".DeleteSample"/>
2323
<activity android:name=".PutSample"/>
24+
<activity android:name=".PatchSample"/>
2425
<activity android:name=".JsonSample"/>
2526
<activity android:name=".JsonStreamerSample"/>
2627
<activity android:name=".FileSample"/>
@@ -32,6 +33,7 @@
3233
<activity android:name=".CancelRequestHandleSample"/>
3334
<activity android:name=".SynchronousClientSample"/>
3435
<activity android:name=".IntentServiceSample"/>
36+
3537
<activity android:name=".SaxSample"/>
3638
<activity android:name=".FilesSample"/>
3739
<activity android:name=".PersistentCookiesSample"/>
@@ -44,8 +46,8 @@
4446
<activity android:name=".ResumeDownloadSample"/>
4547
<activity android:name=".PrePostProcessingSample"/>
4648
<activity android:name=".DigestAuthSample"/>
47-
4849
<service android:name=".services.ExampleIntentService"/>
50+
4951
</application>
5052

5153
</manifest>
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.loopj.android.http.sample;
2+
3+
import org.apache.http.Header;
4+
import org.apache.http.HttpEntity;
5+
6+
import com.loopj.android.http.AsyncHttpClient;
7+
import com.loopj.android.http.AsyncHttpResponseHandler;
8+
import com.loopj.android.http.RequestHandle;
9+
import com.loopj.android.http.ResponseHandlerInterface;
10+
11+
public class PatchSample extends SampleParentActivity {
12+
13+
private static final String LOG_TAG = "PatchSample";
14+
15+
@Override
16+
public RequestHandle executeSample(AsyncHttpClient client, String URL, Header[] headers, HttpEntity entity, ResponseHandlerInterface responseHandler) {
17+
return client.patch(this, URL, entity, null, responseHandler);
18+
}
19+
20+
@Override
21+
public int getSampleTitle() {
22+
return R.string.title_patch_sample;
23+
}
24+
25+
@Override
26+
public boolean isRequestBodyAllowed() {
27+
return false;
28+
}
29+
30+
@Override
31+
public boolean isRequestHeadersAllowed() {
32+
return false;
33+
}
34+
35+
@Override
36+
public String getDefaultURL() {
37+
return PROTOCOL + "httpbin.org/patch";
38+
}
39+
40+
@Override
41+
public ResponseHandlerInterface getResponseHandler() {
42+
return new AsyncHttpResponseHandler() {
43+
@Override
44+
public void onStart() {
45+
clearOutputs();
46+
}
47+
48+
@Override
49+
public void onSuccess(int statusCode, Header[] headers, byte[] response) {
50+
debugHeaders(LOG_TAG, headers);
51+
debugStatusCode(LOG_TAG, statusCode);
52+
debugResponse(LOG_TAG, new String(response));
53+
}
54+
55+
@Override
56+
public void onFailure(int statusCode, Header[] headers, byte[] errorResponse, Throwable e) {
57+
debugHeaders(LOG_TAG, headers);
58+
debugStatusCode(LOG_TAG, statusCode);
59+
debugThrowable(LOG_TAG, e);
60+
if (errorResponse != null) {
61+
debugResponse(LOG_TAG, new String(errorResponse));
62+
}
63+
}
64+
};
65+
}
66+
67+
68+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public class WaypointsActivity extends ListActivity {
3535
new SampleConfig(R.string.title_post_sample, PostSample.class),
3636
new SampleConfig(R.string.title_delete_sample, DeleteSample.class),
3737
new SampleConfig(R.string.title_put_sample, PutSample.class),
38+
new SampleConfig(R.string.title_patch_sample, PatchSample.class),
3839
new SampleConfig(R.string.title_json_sample, JsonSample.class),
3940
new SampleConfig(R.string.title_json_streamer_sample, JsonStreamerSample.class),
4041
new SampleConfig(R.string.title_sax_example, SaxSample.class),

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
<string name="title_json_streamer_sample">POST JSON using streamer</string>
2222
<string name="title_post_sample">POST</string>
2323
<string name="title_put_sample">PUT</string>
24+
<string name="title_patch_sample">PATCH</string>
2425
<string name="title_delete_sample">DELETE</string>
2526
<string name="title_file_sample">GET to File</string>
2627
<string name="title_binary_sample">GET binary data</string>

0 commit comments

Comments
 (0)