Skip to content

Commit 656f699

Browse files
committed
OPTIONS request with sample
1 parent 41b5bf3 commit 656f699

File tree

6 files changed

+66
-1
lines changed

6 files changed

+66
-1
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.loopj.android.http.requests.DeleteRequest;
2020
import com.loopj.android.http.requests.GetRequest;
2121
import com.loopj.android.http.requests.HeadRequest;
22+
import com.loopj.android.http.requests.OptionsRequest;
2223
import com.loopj.android.http.requests.PatchRequest;
2324
import com.loopj.android.http.requests.PostRequest;
2425
import com.loopj.android.http.requests.PutRequest;
@@ -51,4 +52,8 @@ public static RequestInterface put(String URL, Header[] headers, HttpEntity putE
5152
public static RequestInterface head(String URL, Header[] headers) {
5253
return new HeadRequest(false, URL, headers, null);
5354
}
55+
56+
public static RequestInterface options(String URL, Header[] headers) {
57+
return new OptionsRequest(false, URL, headers, null);
58+
}
5459
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.loopj.android.http.requests;
2+
3+
import cz.msebera.android.httpclient.Header;
4+
import cz.msebera.android.httpclient.client.methods.HttpOptions;
5+
import cz.msebera.android.httpclient.client.methods.HttpUriRequest;
6+
7+
public class OptionsRequest extends BaseRequest {
8+
public OptionsRequest(boolean synchronous, String url, Header[] headers, Object TAG) {
9+
super(synchronous, url, headers, TAG);
10+
}
11+
12+
@Override
13+
public HttpUriRequest build() {
14+
return new HttpOptions(getURL());
15+
}
16+
}

sample/src/main/AndroidManifest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
<activity android:name=".HeadSample" />
2929
<activity android:name=".PutSample" />
3030
<activity android:name=".PatchSample" />
31+
<activity android:name=".OptionsSample" />
3132
<activity android:name=".JsonSample" />
3233
<activity android:name=".JsonStreamerSample" />
3334
<activity android:name=".FileSample" />
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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 OptionsSample extends SampleParentActivity {
12+
@Override
13+
public ResponseHandlerInterface getResponseHandler() {
14+
return defaultResponseHandler;
15+
}
16+
17+
@Override
18+
public String getDefaultURL() {
19+
return PROTOCOL + "httpbin.org";
20+
}
21+
22+
@Override
23+
public boolean isRequestHeadersAllowed() {
24+
return true;
25+
}
26+
27+
@Override
28+
public boolean isRequestBodyAllowed() {
29+
return false;
30+
}
31+
32+
@Override
33+
public int getSampleTitle() {
34+
return R.string.title_options_sample;
35+
}
36+
37+
@Override
38+
public RequestHandle executeSample(AsyncHttpClient client, String URL, Header[] headers, HttpEntity entity, ResponseHandlerInterface responseHandler) {
39+
return client.sendRequest(RequestFactory.options(URL, headers), responseHandler);
40+
}
41+
}

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
@@ -36,7 +36,8 @@ public class WaypointsActivity extends ListActivity {
3636
new SampleConfig(R.string.title_delete_sample, DeleteSample.class),
3737
new SampleConfig(R.string.title_patch_sample, PatchSample.class),
3838
new SampleConfig(R.string.title_put_sample, PutSample.class),
39-
new SampleConfig(R.string.title_head_sample, HeadSample.class)
39+
new SampleConfig(R.string.title_head_sample, HeadSample.class),
40+
new SampleConfig(R.string.title_options_sample, OptionsSample.class)
4041
};
4142

4243
@Override

sample/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
<string name="label_url_address">https://httpbin.org/headers</string>
2020

2121
<string name="title_get_sample">GET</string>
22+
<string name="title_options_sample">OPTIONS</string>
2223
<string name="title_json_sample">GET JSON and parse it</string>
2324
<string name="title_json_streamer_sample">POST JSON using streamer</string>
2425
<string name="title_post_sample">POST</string>

0 commit comments

Comments
 (0)