Skip to content

Commit c0476ab

Browse files
committed
POST request, with sample and factory helper method
1 parent 556941a commit c0476ab

File tree

5 files changed

+83
-7
lines changed

5 files changed

+83
-7
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,19 @@
1919

2020
import com.loopj.android.http.interfaces.RequestInterface;
2121
import com.loopj.android.http.requests.GetRequest;
22+
import com.loopj.android.http.requests.PostRequest;
2223

2324
import cz.msebera.android.httpclient.Header;
25+
import cz.msebera.android.httpclient.HttpEntity;
2426

2527
public final class RequestFactory {
2628

2729
public static RequestInterface get(Context context, String URL, Header[] headers) {
2830
return new GetRequest(false, context, URL, headers, null);
2931
}
3032

33+
public static RequestInterface post(Context context, String URL, Header[] headers, HttpEntity postEntity) {
34+
return new PostRequest(false, context, URL, headers, postEntity, null);
35+
}
36+
3137
}

library/src/main/java/com/loopj/android/http/requests/PostRequest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,16 @@
1515
*/
1616
package com.loopj.android.http.requests;
1717

18+
import android.content.Context;
19+
1820
import cz.msebera.android.httpclient.Header;
1921
import cz.msebera.android.httpclient.HttpEntity;
2022
import cz.msebera.android.httpclient.client.methods.HttpPost;
2123
import cz.msebera.android.httpclient.client.methods.HttpUriRequest;
2224

2325
public class PostRequest extends BaseRequestWithEntity {
2426

25-
public PostRequest(boolean synchronous, String url, Header[] headers, HttpEntity entity, Object TAG) {
27+
public PostRequest(boolean synchronous, Context context, String url, Header[] headers, HttpEntity entity, Object TAG) {
2628
super(synchronous, url, headers, entity, TAG);
2729
}
2830

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,19 +69,19 @@ public void onStart() {
6969
}
7070

7171
@Override
72-
public void onSuccess(int statusCode, Header[] headers, byte[] response) {
72+
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
7373
debugHeaders(LOG_TAG, headers);
7474
debugStatusCode(LOG_TAG, statusCode);
75-
debugResponse(LOG_TAG, new String(response));
75+
debugResponse(LOG_TAG, new String(responseBody));
7676
}
7777

7878
@Override
79-
public void onFailure(int statusCode, Header[] headers, byte[] errorResponse, Throwable e) {
79+
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
8080
debugHeaders(LOG_TAG, headers);
8181
debugStatusCode(LOG_TAG, statusCode);
82-
debugThrowable(LOG_TAG, e);
83-
if (errorResponse != null) {
84-
debugResponse(LOG_TAG, new String(errorResponse));
82+
debugThrowable(LOG_TAG, error);
83+
if (responseBody != null) {
84+
debugResponse(LOG_TAG, new String(responseBody));
8585
}
8686
}
8787

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

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
@@ -32,6 +32,7 @@ public class WaypointsActivity extends ListActivity {
3232

3333
private static final SampleConfig[] samplesConfig = new SampleConfig[]{
3434
new SampleConfig(R.string.title_get_sample, GetSample.class),
35+
new SampleConfig(R.string.title_post_sample, PostSample.class)
3536
};
3637

3738
@Override

0 commit comments

Comments
 (0)