Skip to content

Commit 491baf5

Browse files
committed
DELETE request without entity, removed context constructor param from Requests and usage
1 parent c0476ab commit 491baf5

File tree

9 files changed

+105
-80
lines changed

9 files changed

+105
-80
lines changed

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,8 @@
1515
*/
1616
package com.loopj.android.http;
1717

18-
import android.content.Context;
19-
2018
import com.loopj.android.http.interfaces.RequestInterface;
19+
import com.loopj.android.http.requests.DeleteRequest;
2120
import com.loopj.android.http.requests.GetRequest;
2221
import com.loopj.android.http.requests.PostRequest;
2322

@@ -26,12 +25,16 @@
2625

2726
public final class RequestFactory {
2827

29-
public static RequestInterface get(Context context, String URL, Header[] headers) {
30-
return new GetRequest(false, context, URL, headers, null);
28+
public static RequestInterface get(String URL, Header[] headers) {
29+
return new GetRequest(false, URL, headers, null);
30+
}
31+
32+
public static RequestInterface post(String URL, Header[] headers, HttpEntity postEntity) {
33+
return new PostRequest(false, URL, headers, postEntity, null);
3134
}
3235

33-
public static RequestInterface post(Context context, String URL, Header[] headers, HttpEntity postEntity) {
34-
return new PostRequest(false, context, URL, headers, postEntity, null);
36+
public static RequestInterface delete(String URL, Header[] headers) {
37+
return new DeleteRequest(false, URL, headers, null);
3538
}
3639

3740
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.loopj.android.http.requests;
2+
3+
import cz.msebera.android.httpclient.Header;
4+
import cz.msebera.android.httpclient.client.methods.HttpDelete;
5+
import cz.msebera.android.httpclient.client.methods.HttpUriRequest;
6+
7+
public class DeleteRequest extends BaseRequest {
8+
9+
public DeleteRequest(boolean synchronous, String url, Header[] headers, Object TAG) {
10+
super(synchronous, url, headers, TAG);
11+
}
12+
13+
@Override
14+
public HttpUriRequest build() {
15+
return new HttpDelete(getURL());
16+
}
17+
}

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

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

18-
import android.content.Context;
19-
2018
import cz.msebera.android.httpclient.Header;
2119
import cz.msebera.android.httpclient.client.methods.HttpGet;
2220
import cz.msebera.android.httpclient.client.methods.HttpUriRequest;
2321

2422
public final class GetRequest extends BaseRequest {
2523

26-
public GetRequest(boolean synchronous, Context context, String URL, Header[] headers, Object TAG) {
24+
public GetRequest(boolean synchronous, String URL, Header[] headers, Object TAG) {
2725
super(synchronous, URL, headers, TAG);
2826
}
2927

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

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

18-
import android.content.Context;
19-
2018
import cz.msebera.android.httpclient.Header;
2119
import cz.msebera.android.httpclient.HttpEntity;
2220
import cz.msebera.android.httpclient.client.methods.HttpPost;
2321
import cz.msebera.android.httpclient.client.methods.HttpUriRequest;
2422

2523
public class PostRequest extends BaseRequestWithEntity {
2624

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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.interfaces.ResponseHandlerInterface;
6+
import com.loopj.android.http.utils.RequestHandle;
7+
8+
import cz.msebera.android.httpclient.Header;
9+
import cz.msebera.android.httpclient.HttpEntity;
10+
11+
public class DeleteSample extends SampleParentActivity {
12+
protected String LOG_TAG = "DeleteSample";
13+
14+
@Override
15+
public ResponseHandlerInterface getResponseHandler() {
16+
return defaultResponseHandler;
17+
}
18+
19+
@Override
20+
public String getDefaultURL() {
21+
return "https://httpbin.org/delete";
22+
}
23+
24+
@Override
25+
public boolean isRequestHeadersAllowed() {
26+
return true;
27+
}
28+
29+
@Override
30+
public boolean isRequestBodyAllowed() {
31+
return true;
32+
}
33+
34+
@Override
35+
public int getSampleTitle() {
36+
return R.string.title_delete_sample;
37+
}
38+
39+
@Override
40+
public RequestHandle executeSample(AsyncHttpClient client, String URL, Header[] headers, HttpEntity entity, ResponseHandlerInterface responseHandler) {
41+
return client.sendRequest(RequestFactory.delete(URL, headers), responseHandler);
42+
}
43+
}

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

Lines changed: 3 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,20 @@
1818

1919
package com.loopj.android.http.sample;
2020

21-
import android.widget.Toast;
22-
2321
import com.loopj.android.http.AsyncHttpClient;
2422
import com.loopj.android.http.RequestFactory;
25-
import com.loopj.android.http.handlers.AsyncHttpResponseHandler;
2623
import com.loopj.android.http.interfaces.ResponseHandlerInterface;
2724
import com.loopj.android.http.utils.RequestHandle;
2825

29-
import java.util.Locale;
30-
3126
import cz.msebera.android.httpclient.Header;
3227
import cz.msebera.android.httpclient.HttpEntity;
3328

3429
public class GetSample extends SampleParentActivity {
35-
private static final String LOG_TAG = "GetSample";
30+
protected String LOG_TAG = "GetSample";
3631

3732
@Override
3833
public RequestHandle executeSample(AsyncHttpClient client, String URL, Header[] headers, HttpEntity entity, ResponseHandlerInterface responseHandler) {
39-
return client.sendRequest(RequestFactory.get(this, URL, headers), responseHandler);
34+
return client.sendRequest(RequestFactory.get(URL, headers), responseHandler);
4035
}
4136

4237
@Override
@@ -61,37 +56,6 @@ public String getDefaultURL() {
6156

6257
@Override
6358
public ResponseHandlerInterface getResponseHandler() {
64-
return new AsyncHttpResponseHandler() {
65-
66-
@Override
67-
public void onStart() {
68-
clearOutputs();
69-
}
70-
71-
@Override
72-
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
73-
debugHeaders(LOG_TAG, headers);
74-
debugStatusCode(LOG_TAG, statusCode);
75-
debugResponse(LOG_TAG, new String(responseBody));
76-
}
77-
78-
@Override
79-
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
80-
debugHeaders(LOG_TAG, headers);
81-
debugStatusCode(LOG_TAG, statusCode);
82-
debugThrowable(LOG_TAG, error);
83-
if (responseBody != null) {
84-
debugResponse(LOG_TAG, new String(responseBody));
85-
}
86-
}
87-
88-
@Override
89-
public void onRetry(int retryNo) {
90-
Toast.makeText(GetSample.this,
91-
String.format(Locale.getDefault(), "Request is retried, retry no. %d", retryNo),
92-
Toast.LENGTH_SHORT)
93-
.show();
94-
}
95-
};
59+
return defaultResponseHandler;
9660
}
9761
}

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

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,18 @@
22

33
import com.loopj.android.http.AsyncHttpClient;
44
import com.loopj.android.http.RequestFactory;
5-
import com.loopj.android.http.handlers.AsyncHttpResponseHandler;
65
import com.loopj.android.http.interfaces.ResponseHandlerInterface;
76
import com.loopj.android.http.utils.RequestHandle;
87

98
import cz.msebera.android.httpclient.Header;
109
import cz.msebera.android.httpclient.HttpEntity;
1110

1211
public class PostSample extends SampleParentActivity {
13-
private static final String LOG_TAG = "PostSample";
12+
protected String LOG_TAG = "PostSample";
1413

1514
@Override
1615
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-
};
16+
return defaultResponseHandler;
4117
}
4218

4319
@Override
@@ -62,6 +38,6 @@ public int getSampleTitle() {
6238

6339
@Override
6440
public RequestHandle executeSample(AsyncHttpClient client, String URL, Header[] headers, HttpEntity entity, ResponseHandlerInterface responseHandler) {
65-
return client.sendRequest(RequestFactory.post(this, URL, headers, entity), responseHandler);
41+
return client.sendRequest(RequestFactory.post(URL, headers, entity), responseHandler);
6642
}
6743
}

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636

3737
import com.loopj.android.http.AsyncHttpClient;
3838
import com.loopj.android.http.AsyncHttpRequest;
39+
import com.loopj.android.http.handlers.AsyncHttpResponseHandler;
3940
import com.loopj.android.http.interfaces.ResponseHandlerInterface;
4041
import com.loopj.android.http.utils.RequestHandle;
4142

@@ -65,7 +66,7 @@ public abstract class SampleParentActivity extends Activity implements SampleInt
6566
protected static final int LIGHTRED = Color.parseColor("#FF3300");
6667
protected static final int YELLOW = Color.parseColor("#FFFF00");
6768
protected static final int LIGHTBLUE = Color.parseColor("#99CCFF");
68-
private static final String LOG_TAG = "SampleParentActivity";
69+
protected String LOG_TAG = "SampleParentActivity";
6970
private static final int MENU_USE_HTTPS = 0;
7071
private static final int MENU_CLEAR_VIEW = 1;
7172
private static final int MENU_LOGGING_VERBOSITY = 2;
@@ -91,6 +92,30 @@ public void onClick(View v) {
9192
private LinearLayout responseLayout;
9293
private boolean useHttps = true;
9394
private boolean enableLogging = true;
95+
protected final ResponseHandlerInterface defaultResponseHandler = new AsyncHttpResponseHandler() {
96+
97+
@Override
98+
public void onStart() {
99+
clearOutputs();
100+
}
101+
102+
@Override
103+
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
104+
debugHeaders(LOG_TAG, headers);
105+
debugStatusCode(LOG_TAG, statusCode);
106+
debugResponse(LOG_TAG, new String(responseBody));
107+
}
108+
109+
@Override
110+
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
111+
debugHeaders(LOG_TAG, headers);
112+
debugStatusCode(LOG_TAG, statusCode);
113+
debugThrowable(LOG_TAG, error);
114+
if (responseBody != null) {
115+
debugResponse(LOG_TAG, new String(responseBody));
116+
}
117+
}
118+
};
94119

95120
protected static String throwableToString(Throwable t) {
96121
if (t == null)

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ 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)
35+
new SampleConfig(R.string.title_post_sample, PostSample.class),
36+
new SampleConfig(R.string.title_delete_sample, DeleteSample.class)
3637
};
3738

3839
@Override

0 commit comments

Comments
 (0)