Skip to content

Commit 7da8ed5

Browse files
committed
Merge pull request android-async-http#753 from saurabytes/json-payload-get-delete
Sending raw HttpEntities to Get or Delete request like for JSON payloads.
2 parents a20f795 + 4f73e51 commit 7da8ed5

File tree

3 files changed

+145
-2
lines changed

3 files changed

+145
-2
lines changed

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

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,7 @@
4040
import org.apache.http.client.CredentialsProvider;
4141
import org.apache.http.client.HttpClient;
4242
import org.apache.http.client.RedirectHandler;
43-
import org.apache.http.client.methods.HttpDelete;
4443
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
45-
import org.apache.http.client.methods.HttpGet;
4644
import org.apache.http.client.methods.HttpHead;
4745
import org.apache.http.client.methods.HttpPost;
4846
import org.apache.http.client.methods.HttpPut;
@@ -901,6 +899,23 @@ public RequestHandle get(Context context, String url, Header[] headers, RequestP
901899
context);
902900
}
903901

902+
/**
903+
* Perform a HTTP GET request and track the Android Context which initiated the request.
904+
*
905+
* @param context the Android Context which initiated the request.
906+
* @param url the URL to send the request to.
907+
* @param entity a raw {@link org.apache.http.HttpEntity} to send with the request, for
908+
* example, use this to send string/json/xml payloads to a server by
909+
* passing a {@link org.apache.http.entity.StringEntity}.
910+
* @param contentType the content type of the payload you are sending, for example
911+
* application/json if sending a json payload.
912+
* @param responseHandler the response ha ndler instance that should handle the response.
913+
* @return RequestHandle of future request process
914+
*/
915+
public RequestHandle get(Context context, String url, HttpEntity entity, String contentType, ResponseHandlerInterface responseHandler) {
916+
return sendRequest(httpClient, httpContext, addEntityToRequestBase(new HttpGet(URI.create(url).normalize()), entity), contentType, responseHandler, context);
917+
}
918+
904919
// [-] HTTP GET
905920
// [+] HTTP POST
906921

@@ -1218,6 +1233,23 @@ public RequestHandle delete(Context context, String url, Header[] headers, Reque
12181233
return sendRequest(httpClient, httpContext, httpDelete, null, responseHandler, context);
12191234
}
12201235

1236+
/**
1237+
* Perform a HTTP DELETE request and track the Android Context which initiated the request.
1238+
*
1239+
* @param context the Android Context which initiated the request.
1240+
* @param url the URL to send the request to.
1241+
* @param entity a raw {@link org.apache.http.HttpEntity} to send with the request, for
1242+
* example, use this to send string/json/xml payloads to a server by
1243+
* passing a {@link org.apache.http.entity.StringEntity}.
1244+
* @param contentType the content type of the payload you are sending, for example
1245+
* application/json if sending a json payload.
1246+
* @param responseHandler the response ha ndler instance that should handle the response.
1247+
* @return RequestHandle of future request process
1248+
*/
1249+
public RequestHandle delete(Context context, String url, HttpEntity entity, String contentType, ResponseHandlerInterface responseHandler) {
1250+
return sendRequest(httpClient, httpContext, addEntityToRequestBase(new HttpDelete(URI.create(url).normalize()), entity), contentType, responseHandler, context);
1251+
}
1252+
12211253
// [-] HTTP DELETE
12221254

12231255
/**
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
Android Asynchronous Http Client
3+
Copyright (c) 2011 James Smith <[email protected]>
4+
http://loopj.com
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
*/
18+
19+
package com.loopj.android.http;
20+
21+
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
22+
23+
import java.net.URI;
24+
25+
/**
26+
* The current Android (API level 21) bundled version of the Apache Http Client does not implement
27+
* a HttpEntityEnclosingRequestBase type of HTTP DELETE method.
28+
* Until the Android version is updated this can serve in it's stead.
29+
* This implementation can and should go away when the official solution arrives.
30+
*/
31+
public final class HttpDelete extends HttpEntityEnclosingRequestBase {
32+
public final static String METHOD_NAME = "DELETE";
33+
34+
public HttpDelete() {
35+
super();
36+
}
37+
38+
public HttpDelete(final URI uri) {
39+
super();
40+
setURI(uri);
41+
}
42+
43+
/**
44+
* @throws IllegalArgumentException if the uri is invalid.
45+
*/
46+
public HttpDelete(final String uri) {
47+
super();
48+
setURI(URI.create(uri));
49+
}
50+
51+
@Override
52+
public String getMethod() {
53+
return METHOD_NAME;
54+
}
55+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
Android Asynchronous Http Client
3+
Copyright (c) 2011 James Smith <[email protected]>
4+
http://loopj.com
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
*/
18+
19+
package com.loopj.android.http;
20+
21+
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
22+
23+
import java.net.URI;
24+
25+
/**
26+
* The current Android (API level 21) bundled version of the Apache Http Client does not implement
27+
* a HttpEntityEnclosingRequestBase type of HTTP GET method.
28+
* Until the Android version is updated this can serve in it's stead.
29+
* This implementation can and should go away when the official solution arrives.
30+
*/
31+
public final class HttpGet extends HttpEntityEnclosingRequestBase {
32+
33+
public final static String METHOD_NAME = "GET";
34+
35+
public HttpGet() {
36+
super();
37+
}
38+
39+
public HttpGet(final URI uri) {
40+
super();
41+
setURI(uri);
42+
}
43+
44+
/**
45+
* @throws IllegalArgumentException if the uri is invalid.
46+
*/
47+
public HttpGet(final String uri) {
48+
super();
49+
setURI(URI.create(uri));
50+
}
51+
52+
@Override
53+
public String getMethod() {
54+
return METHOD_NAME;
55+
}
56+
}

0 commit comments

Comments
 (0)