Skip to content

Commit 7cc6b8d

Browse files
author
Sabi
committed
sending raw HttpEntities to Get of Delete request like for JSON payloads/queries
1 parent f660670 commit 7cc6b8d

File tree

3 files changed

+144
-2
lines changed

3 files changed

+144
-2
lines changed

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

Lines changed: 35 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;
@@ -69,6 +67,7 @@
6967
import org.apache.http.protocol.ExecutionContext;
7068
import org.apache.http.protocol.HttpContext;
7169
import org.apache.http.protocol.SyncBasicHttpContext;
70+
import org.json.JSONObject;
7271

7372
import java.io.IOException;
7473
import java.io.InputStream;
@@ -901,6 +900,23 @@ public RequestHandle get(Context context, String url, Header[] headers, RequestP
901900
context);
902901
}
903902

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

@@ -1206,6 +1222,23 @@ public RequestHandle delete(Context context, String url, Header[] headers, Reque
12061222
return sendRequest(httpClient, httpContext, httpDelete, null, responseHandler, context);
12071223
}
12081224

1225+
/**
1226+
* Perform a HTTP DELETE request and track the Android Context which initiated the request.
1227+
*
1228+
* @param context the Android Context which initiated the request.
1229+
* @param url the URL to send the request to.
1230+
* @param entity a raw {@link org.apache.http.HttpEntity} to send with the request, for
1231+
* example, use this to send string/json/xml payloads to a server by
1232+
* passing a {@link org.apache.http.entity.StringEntity}.
1233+
* @param contentType the content type of the payload you are sending, for example
1234+
* application/json if sending a json payload.
1235+
* @param responseHandler the response ha ndler instance that should handle the response.
1236+
* @return RequestHandle of future request process
1237+
*/
1238+
public RequestHandle delete(Context context, String url, HttpEntity entity, String contentType, ResponseHandlerInterface responseHandler) {
1239+
return sendRequest(httpClient, httpContext, addEntityToRequestBase(new HttpDelete(URI.create(url).normalize()), entity), contentType, responseHandler, context);
1240+
}
1241+
12091242
// [-] HTTP DELETE
12101243

12111244
/**
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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+
* the HTTP PATCH method. Until the Android version is updated this can serve in it's stead.
28+
* This implementation can and should go away when the official solution arrives.
29+
*/
30+
public final class HttpDelete extends HttpEntityEnclosingRequestBase {
31+
public final static String METHOD_NAME = "DELETE";
32+
33+
public HttpDelete() {
34+
super();
35+
}
36+
37+
public HttpDelete(final URI uri) {
38+
super();
39+
setURI(uri);
40+
}
41+
42+
/**
43+
* @throws IllegalArgumentException if the uri is invalid.
44+
*/
45+
public HttpDelete(final String uri) {
46+
super();
47+
setURI(URI.create(uri));
48+
}
49+
50+
@Override
51+
public String getMethod() {
52+
return METHOD_NAME;
53+
}
54+
}
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+
* the HTTP PATCH method. Until the Android version is updated this can serve in it's stead.
28+
* This implementation can and should go away when the official solution arrives.
29+
*/
30+
public final class HttpGet extends HttpEntityEnclosingRequestBase {
31+
32+
public final static String METHOD_NAME = "GET";
33+
34+
public HttpGet() {
35+
super();
36+
}
37+
38+
public HttpGet(final URI uri) {
39+
super();
40+
setURI(uri);
41+
}
42+
43+
/**
44+
* @throws IllegalArgumentException if the uri is invalid.
45+
*/
46+
public HttpGet(final String uri) {
47+
super();
48+
setURI(URI.create(uri));
49+
}
50+
51+
@Override
52+
public String getMethod() {
53+
return METHOD_NAME;
54+
}
55+
}

0 commit comments

Comments
 (0)