Skip to content

Commit cc9eb89

Browse files
committed
Added sample for behavior on igoring Content-Type when HttpEntity is set
1 parent 8f396c8 commit cc9eb89

File tree

4 files changed

+101
-26
lines changed

4 files changed

+101
-26
lines changed

sample/src/main/AndroidManifest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
<activity android:name=".RangeResponseSample"/>
4141
<activity android:name=".Http401AuthSample"/>
4242
<activity android:name=".AsyncBackgroundThreadSample"/>
43+
<activity android:name=".ContentTypeForHttpEntitySample"/>
4344

4445
<service android:name=".services.ExampleIntentService"/>
4546
</application>
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package com.loopj.android.http.sample;
2+
3+
import android.util.Log;
4+
5+
import com.loopj.android.http.AsyncHttpClient;
6+
import com.loopj.android.http.RequestHandle;
7+
import com.loopj.android.http.RequestParams;
8+
import com.loopj.android.http.ResponseHandlerInterface;
9+
import com.loopj.android.http.TextHttpResponseHandler;
10+
11+
import org.apache.http.Header;
12+
import org.apache.http.HttpEntity;
13+
14+
import java.io.File;
15+
import java.io.IOException;
16+
17+
public class ContentTypeForHttpEntitySample extends SampleParentActivity {
18+
private static final String LOG_TAG = "ContentTypeForHttpEntitySample";
19+
20+
@Override
21+
public ResponseHandlerInterface getResponseHandler() {
22+
return new TextHttpResponseHandler() {
23+
@Override
24+
public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
25+
debugHeaders(LOG_TAG, headers);
26+
debugStatusCode(LOG_TAG, statusCode);
27+
debugResponse(LOG_TAG, responseString);
28+
debugThrowable(LOG_TAG, throwable);
29+
}
30+
31+
@Override
32+
public void onSuccess(int statusCode, Header[] headers, String responseString) {
33+
debugHeaders(LOG_TAG, headers);
34+
debugStatusCode(LOG_TAG, statusCode);
35+
debugResponse(LOG_TAG, responseString);
36+
}
37+
};
38+
}
39+
40+
@Override
41+
public String getDefaultURL() {
42+
return "http://httpbin.org/post";
43+
}
44+
45+
@Override
46+
public boolean isRequestHeadersAllowed() {
47+
return true;
48+
}
49+
50+
@Override
51+
public boolean isRequestBodyAllowed() {
52+
return false;
53+
}
54+
55+
@Override
56+
public int getSampleTitle() {
57+
return R.string.title_content_type_http_entity;
58+
}
59+
60+
@Override
61+
public RequestHandle executeSample(AsyncHttpClient client, String URL, Header[] headers, HttpEntity entity, ResponseHandlerInterface responseHandler) {
62+
RequestParams rParams = new RequestParams();
63+
rParams.put("sample_key", "Sample String");
64+
try {
65+
File sample_file = File.createTempFile("temp_", "_handled", getCacheDir());
66+
rParams.put("sample_file", sample_file);
67+
} catch (IOException e) {
68+
Log.e(LOG_TAG, "Cannot add sample file", e);
69+
}
70+
return client.post(this, URL, headers, rParams, "multipart/form-data", responseHandler);
71+
}
72+
}

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

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -30,30 +30,31 @@
3030

3131
public class WaypointsActivity extends ListActivity {
3232

33-
private static final SampleConfig[] samplesConfig = new SampleConfig[] {
34-
new SampleConfig(R.string.title_get_sample, GetSample.class),
35-
new SampleConfig(R.string.title_post_sample, PostSample.class),
36-
new SampleConfig(R.string.title_delete_sample, DeleteSample.class),
37-
new SampleConfig(R.string.title_put_sample, PutSample.class),
38-
new SampleConfig(R.string.title_json_sample, JsonSample.class),
39-
new SampleConfig(R.string.title_json_streamer_sample, JsonStreamerSample.class),
40-
new SampleConfig(R.string.title_sax_example, SaxSample.class),
41-
new SampleConfig(R.string.title_file_sample, FileSample.class),
42-
new SampleConfig(R.string.title_binary_sample, BinarySample.class),
43-
new SampleConfig(R.string.title_gzip_sample, GzipSample.class),
44-
new SampleConfig(R.string.title_redirect_302, Redirect302Sample.class),
45-
new SampleConfig(R.string.title_threading_timeout, ThreadingTimeoutSample.class),
46-
new SampleConfig(R.string.title_cancel_all, CancelAllRequestsSample.class),
47-
new SampleConfig(R.string.title_cancel_handle, CancelRequestHandleSample.class),
48-
new SampleConfig(R.string.title_synchronous, SynchronousClientSample.class),
49-
new SampleConfig(R.string.title_intent_service_sample, IntentServiceSample.class),
50-
new SampleConfig(R.string.title_post_files, FilesSample.class),
51-
new SampleConfig(R.string.title_persistent_cookies, PersistentCookiesSample.class),
52-
new SampleConfig(R.string.title_custom_ca, CustomCASample.class),
53-
new SampleConfig(R.string.title_retry_handler, RetryRequestSample.class),
54-
new SampleConfig(R.string.title_range_sample, RangeResponseSample.class),
55-
new SampleConfig(R.string.title_401_unauth, Http401AuthSample.class),
56-
new SampleConfig(R.string.title_pre_post_processing, PrePostProcessingSample.class)
33+
private static final SampleConfig[] samplesConfig = new SampleConfig[]{
34+
new SampleConfig(R.string.title_get_sample, GetSample.class),
35+
new SampleConfig(R.string.title_post_sample, PostSample.class),
36+
new SampleConfig(R.string.title_delete_sample, DeleteSample.class),
37+
new SampleConfig(R.string.title_put_sample, PutSample.class),
38+
new SampleConfig(R.string.title_json_sample, JsonSample.class),
39+
new SampleConfig(R.string.title_json_streamer_sample, JsonStreamerSample.class),
40+
new SampleConfig(R.string.title_sax_example, SaxSample.class),
41+
new SampleConfig(R.string.title_file_sample, FileSample.class),
42+
new SampleConfig(R.string.title_binary_sample, BinarySample.class),
43+
new SampleConfig(R.string.title_gzip_sample, GzipSample.class),
44+
new SampleConfig(R.string.title_redirect_302, Redirect302Sample.class),
45+
new SampleConfig(R.string.title_threading_timeout, ThreadingTimeoutSample.class),
46+
new SampleConfig(R.string.title_cancel_all, CancelAllRequestsSample.class),
47+
new SampleConfig(R.string.title_cancel_handle, CancelRequestHandleSample.class),
48+
new SampleConfig(R.string.title_synchronous, SynchronousClientSample.class),
49+
new SampleConfig(R.string.title_intent_service_sample, IntentServiceSample.class),
50+
new SampleConfig(R.string.title_post_files, FilesSample.class),
51+
new SampleConfig(R.string.title_persistent_cookies, PersistentCookiesSample.class),
52+
new SampleConfig(R.string.title_custom_ca, CustomCASample.class),
53+
new SampleConfig(R.string.title_retry_handler, RetryRequestSample.class),
54+
new SampleConfig(R.string.title_range_sample, RangeResponseSample.class),
55+
new SampleConfig(R.string.title_401_unauth, Http401AuthSample.class),
56+
new SampleConfig(R.string.title_pre_post_processing, PrePostProcessingSample.class),
57+
new SampleConfig(R.string.title_content_type_http_entity, ContentTypeForHttpEntitySample.class)
5758
};
5859

5960
@Override
@@ -82,8 +83,8 @@ private static class SampleConfig {
8283
final Class targetClass;
8384

8485
SampleConfig(int titleId, Class targetClass) {
85-
this.titleId = titleId;
86-
this.targetClass = targetClass;
86+
this.titleId = titleId;
87+
this.targetClass = targetClass;
8788
}
8889

8990
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,5 @@
3737
<string name="title_401_unauth">401 basic authentication</string>
3838
<string name="title_pre_post_processing">Pre-/Post-processing</string>
3939
<string name="title_async_background_thread">Async on background thread</string>
40+
<string name="title_content_type_http_entity">Content-Type with HttpEntity</string>
4041
</resources>

0 commit comments

Comments
 (0)