Skip to content

Commit 2122a13

Browse files
committed
PATCH request with sample, with entity, refactored usage of LOG_TAG in sample app
1 parent 491baf5 commit 2122a13

File tree

8 files changed

+103
-17
lines changed

8 files changed

+103
-17
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import com.loopj.android.http.interfaces.RequestInterface;
1919
import com.loopj.android.http.requests.DeleteRequest;
2020
import com.loopj.android.http.requests.GetRequest;
21+
import com.loopj.android.http.requests.PatchRequest;
2122
import com.loopj.android.http.requests.PostRequest;
2223

2324
import cz.msebera.android.httpclient.Header;
@@ -37,4 +38,7 @@ public static RequestInterface delete(String URL, Header[] headers) {
3738
return new DeleteRequest(false, URL, headers, null);
3839
}
3940

41+
public static RequestInterface patch(String URL, Header[] headers, HttpEntity patchEntity) {
42+
return new PatchRequest(false, URL, headers, patchEntity, null);
43+
}
4044
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.loopj.android.http.requests;
2+
3+
import cz.msebera.android.httpclient.Header;
4+
import cz.msebera.android.httpclient.HttpEntity;
5+
import cz.msebera.android.httpclient.client.methods.HttpPatch;
6+
import cz.msebera.android.httpclient.client.methods.HttpUriRequest;
7+
8+
public class PatchRequest extends BaseRequestWithEntity {
9+
public PatchRequest(boolean synchronous, String url, Header[] headers, HttpEntity entity, Object TAG) {
10+
super(synchronous, url, headers, entity, TAG);
11+
}
12+
13+
@Override
14+
public HttpUriRequest build() {
15+
HttpPatch patch = new HttpPatch(getURL());
16+
patch.setEntity(getEntity());
17+
return patch;
18+
}
19+
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@
99
import cz.msebera.android.httpclient.HttpEntity;
1010

1111
public class DeleteSample extends SampleParentActivity {
12-
protected String LOG_TAG = "DeleteSample";
12+
@Override
13+
public String getLogTag() {
14+
return "DeleteSample";
15+
}
1316

1417
@Override
1518
public ResponseHandlerInterface getResponseHandler() {

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@
2727
import cz.msebera.android.httpclient.HttpEntity;
2828

2929
public class GetSample extends SampleParentActivity {
30-
protected String LOG_TAG = "GetSample";
30+
31+
@Override
32+
public String getLogTag() {
33+
return "GetSample";
34+
}
3135

3236
@Override
3337
public RequestHandle executeSample(AsyncHttpClient client, String URL, Header[] headers, HttpEntity entity, ResponseHandlerInterface responseHandler) {
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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 PatchSample extends SampleParentActivity {
12+
13+
@Override
14+
public String getLogTag() {
15+
return "PatchSample";
16+
}
17+
18+
@Override
19+
public ResponseHandlerInterface getResponseHandler() {
20+
return defaultResponseHandler;
21+
}
22+
23+
@Override
24+
public String getDefaultURL() {
25+
return "https://httpbin.org/patch";
26+
}
27+
28+
@Override
29+
public boolean isRequestHeadersAllowed() {
30+
return true;
31+
}
32+
33+
@Override
34+
public boolean isRequestBodyAllowed() {
35+
return true;
36+
}
37+
38+
@Override
39+
public int getSampleTitle() {
40+
return R.string.title_patch_sample;
41+
}
42+
43+
@Override
44+
public RequestHandle executeSample(AsyncHttpClient client, String URL, Header[] headers, HttpEntity entity, ResponseHandlerInterface responseHandler) {
45+
return client.sendRequest(RequestFactory.patch(URL, headers, entity), responseHandler);
46+
}
47+
}

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@
99
import cz.msebera.android.httpclient.HttpEntity;
1010

1111
public class PostSample extends SampleParentActivity {
12-
protected String LOG_TAG = "PostSample";
12+
13+
@Override
14+
public String getLogTag() {
15+
return "PostSample";
16+
}
1317

1418
@Override
1519
public ResponseHandlerInterface getResponseHandler() {

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

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,14 @@ public abstract class SampleParentActivity extends Activity implements SampleInt
6666
protected static final int LIGHTRED = Color.parseColor("#FF3300");
6767
protected static final int YELLOW = Color.parseColor("#FFFF00");
6868
protected static final int LIGHTBLUE = Color.parseColor("#99CCFF");
69-
protected String LOG_TAG = "SampleParentActivity";
7069
private static final int MENU_USE_HTTPS = 0;
7170
private static final int MENU_CLEAR_VIEW = 1;
7271
private static final int MENU_LOGGING_VERBOSITY = 2;
7372
private static final int MENU_ENABLE_LOGGING = 3;
7473
protected static String PROTOCOL = PROTOCOL_HTTPS;
7574
private final List<RequestHandle> requestHandles = new LinkedList<>();
7675
public LinearLayout customFieldsLayout;
76+
private String LOGTAG;
7777
private AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
7878
private EditText urlEditText, headersEditText, bodyEditText;
7979
protected final View.OnClickListener onClickListener = new View.OnClickListener() {
@@ -90,8 +90,6 @@ public void onClick(View v) {
9090
}
9191
};
9292
private LinearLayout responseLayout;
93-
private boolean useHttps = true;
94-
private boolean enableLogging = true;
9593
protected final ResponseHandlerInterface defaultResponseHandler = new AsyncHttpResponseHandler() {
9694

9795
@Override
@@ -101,21 +99,23 @@ public void onStart() {
10199

102100
@Override
103101
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));
102+
debugHeaders(getLogTag(), headers);
103+
debugStatusCode(getLogTag(), statusCode);
104+
debugResponse(getLogTag(), new String(responseBody));
107105
}
108106

109107
@Override
110108
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);
109+
debugHeaders(getLogTag(), headers);
110+
debugStatusCode(getLogTag(), statusCode);
111+
debugThrowable(getLogTag(), error);
114112
if (responseBody != null) {
115-
debugResponse(LOG_TAG, new String(responseBody));
113+
debugResponse(getLogTag(), new String(responseBody));
116114
}
117115
}
118116
};
117+
private boolean useHttps = true;
118+
private boolean enableLogging = true;
119119

120120
protected static String throwableToString(Throwable t) {
121121
if (t == null)
@@ -131,6 +131,10 @@ public static int getContrastColor(int color) {
131131
return y >= 128 ? Color.BLACK : Color.WHITE;
132132
}
133133

134+
public String getLogTag() {
135+
return this.LOGTAG;
136+
}
137+
134138
@Override
135139
protected void onCreate(Bundle savedInstanceState) {
136140
super.onCreate(savedInstanceState);
@@ -279,11 +283,11 @@ public List<Header> getRequestHeadersList() {
279283

280284
String headerName = line.substring(0, equalSignPos).trim();
281285
String headerValue = line.substring(1 + equalSignPos).trim();
282-
Log.d(LOG_TAG, String.format("Added header: [%s:%s]", headerName, headerValue));
286+
Log.d(getLogTag(), String.format("Added header: [%s:%s]", headerName, headerValue));
283287

284288
headers.add(new BasicHeader(headerName, headerValue));
285289
} catch (Throwable t) {
286-
Log.e(LOG_TAG, "Not a valid header line: " + line, t);
290+
Log.e(getLogTag(), "Not a valid header line: " + line, t);
287291
}
288292
}
289293
}
@@ -301,7 +305,7 @@ public HttpEntity getRequestEntity() {
301305
try {
302306
return new StringEntity(bodyText);
303307
} catch (UnsupportedEncodingException e) {
304-
Log.e(LOG_TAG, "cannot create String entity", e);
308+
Log.e(getLogTag(), "cannot create String entity", e);
305309
}
306310
}
307311
return 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
@@ -33,7 +33,8 @@ public class WaypointsActivity extends ListActivity {
3333
private static final SampleConfig[] samplesConfig = new SampleConfig[]{
3434
new SampleConfig(R.string.title_get_sample, GetSample.class),
3535
new SampleConfig(R.string.title_post_sample, PostSample.class),
36-
new SampleConfig(R.string.title_delete_sample, DeleteSample.class)
36+
new SampleConfig(R.string.title_delete_sample, DeleteSample.class),
37+
new SampleConfig(R.string.title_patch_sample, PatchSample.class)
3738
};
3839

3940
@Override

0 commit comments

Comments
 (0)