Skip to content

Commit 527dae4

Browse files
committed
Added RequestParams formatting sample
1 parent d81e442 commit 527dae4

File tree

5 files changed

+189
-2
lines changed

5 files changed

+189
-2
lines changed

sample/src/main/AndroidManifest.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
<activity android:name=".CancelRequestHandleSample" />
3838
<activity android:name=".SynchronousClientSample" />
3939
<activity android:name=".IntentServiceSample" />
40-
4140
<activity android:name=".SaxSample" />
4241
<activity android:name=".FilesSample" />
4342
<activity android:name=".PersistentCookiesSample" />
@@ -51,6 +50,7 @@
5150
<activity android:name=".PrePostProcessingSample" />
5251
<activity android:name=".DigestAuthSample" />
5352
<activity android:name=".UsePoolThreadSample" />
53+
<activity android:name=".RequestParamsDebug" />
5454

5555
<service android:name=".services.ExampleIntentService" />
5656

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
package com.loopj.android.http.sample;
2+
3+
import android.os.Bundle;
4+
import android.widget.EditText;
5+
6+
import com.loopj.android.http.AsyncHttpClient;
7+
import com.loopj.android.http.RequestHandle;
8+
import com.loopj.android.http.RequestParams;
9+
import com.loopj.android.http.ResponseHandlerInterface;
10+
import com.loopj.android.http.TextHttpResponseHandler;
11+
import com.loopj.android.http.sample.util.API8Util;
12+
13+
import org.apache.http.Header;
14+
import org.apache.http.HttpEntity;
15+
16+
import java.util.ArrayList;
17+
import java.util.HashMap;
18+
import java.util.HashSet;
19+
import java.util.List;
20+
import java.util.Map;
21+
import java.util.Set;
22+
23+
public class RequestParamsDebug extends SampleParentActivity {
24+
25+
public static final String LOG_TAG = "RequestParamsDebug";
26+
private EditText customParams;
27+
private static final String DEMO_RP_CONTENT = "array=java\n" +
28+
"array=C\n" +
29+
"list=blue\n" +
30+
"list=yellow\n" +
31+
"set=music\n" +
32+
"set=art\n" +
33+
"map=first_name\n" +
34+
"map=last_name\n";
35+
36+
@Override
37+
public ResponseHandlerInterface getResponseHandler() {
38+
return new TextHttpResponseHandler() {
39+
40+
@Override
41+
public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
42+
debugStatusCode(LOG_TAG, statusCode);
43+
debugHeaders(LOG_TAG, headers);
44+
debugResponse(LOG_TAG, responseString);
45+
debugThrowable(LOG_TAG, throwable);
46+
}
47+
48+
@Override
49+
public void onSuccess(int statusCode, Header[] headers, String responseString) {
50+
debugStatusCode(LOG_TAG, statusCode);
51+
debugHeaders(LOG_TAG, headers);
52+
debugResponse(LOG_TAG, responseString);
53+
}
54+
};
55+
}
56+
57+
@Override
58+
protected void onCreate(Bundle savedInstanceState) {
59+
super.onCreate(savedInstanceState);
60+
customParams = new EditText(this);
61+
customParams.setLines(8);
62+
customParams.setText(DEMO_RP_CONTENT);
63+
customFieldsLayout.addView(customParams);
64+
}
65+
66+
@Override
67+
public String getDefaultURL() {
68+
return PROTOCOL + "httpbin.org/get";
69+
}
70+
71+
@Override
72+
public boolean isRequestHeadersAllowed() {
73+
return false;
74+
}
75+
76+
@Override
77+
public boolean isRequestBodyAllowed() {
78+
return false;
79+
}
80+
81+
@Override
82+
public int getSampleTitle() {
83+
return R.string.title_request_params_debug;
84+
}
85+
86+
@Override
87+
public RequestHandle executeSample(AsyncHttpClient client, String URL, Header[] headers, HttpEntity entity, ResponseHandlerInterface responseHandler) {
88+
return getAsyncHttpClient().get(this, getDefaultURL(), getRequestParams(), getResponseHandler());
89+
}
90+
91+
// TODO: allow parsing multiple values for each type, maybe like "type.key=value" ?
92+
private RequestParams getRequestParams() {
93+
RequestParams rp = new RequestParams();
94+
// contents of customParams custom field view
95+
String customParamsText = customParams.getText().toString();
96+
String[] pairs = customParamsText.split("\n");
97+
// temp content holders
98+
Map<String, Map<String, String>> mapOfMaps = new HashMap<>();
99+
Map<String, List<String>> mapOfLists = new HashMap<>();
100+
Map<String, String[]> mapOfArrays = new HashMap<>();
101+
Map<String, Set<String>> mapOfSets = new HashMap<>();
102+
for (String pair : pairs) {
103+
String[] kv = pair.split("=");
104+
if (kv.length != 2)
105+
continue;
106+
String key = kv[0].trim();
107+
String value = kv[1].trim();
108+
if ("array".equals(key)) {
109+
String[] values = mapOfArrays.get(key);
110+
if (values == null) {
111+
values = new String[]{value};
112+
} else {
113+
values = API8Util.copyOfRange(values, 0, values.length + 1);
114+
values[values.length - 1] = value;
115+
}
116+
mapOfArrays.put(key, values);
117+
} else if ("list".equals(key)) {
118+
List<String> values = mapOfLists.get(key);
119+
if (values == null) {
120+
values = new ArrayList<>();
121+
}
122+
values.add(value);
123+
mapOfLists.put(key, values);
124+
} else if ("set".equals(key)) {
125+
Set<String> values = mapOfSets.get(key);
126+
if (values == null) {
127+
values = new HashSet<>();
128+
}
129+
values.add(value);
130+
mapOfSets.put(key, values);
131+
} else if ("map".equals(key)) {
132+
Map<String, String> values = mapOfMaps.get(key);
133+
if (values == null) {
134+
values = new HashMap<>();
135+
}
136+
values.put(key + values.size(), value);
137+
mapOfMaps.put(key, values);
138+
}
139+
}
140+
// fill in string list
141+
for (Map.Entry<String, List<String>> entry : mapOfLists.entrySet()) {
142+
rp.put(entry.getKey(), entry.getValue());
143+
}
144+
// fill in string array
145+
for (Map.Entry<String, String[]> entry : mapOfArrays.entrySet()) {
146+
rp.put(entry.getKey(), entry.getValue());
147+
}
148+
// fill in string set
149+
for (Map.Entry<String, Set<String>> entry : mapOfSets.entrySet()) {
150+
rp.put(entry.getKey(), entry.getValue());
151+
}
152+
// fill in string map
153+
for (Map.Entry<String, Map<String, String>> entry : mapOfMaps.entrySet()) {
154+
rp.put(entry.getKey(), entry.getValue());
155+
}
156+
// debug final URL construction into UI
157+
debugResponse(LOG_TAG, rp.toString());
158+
return rp;
159+
}
160+
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
import android.widget.ArrayAdapter;
2626
import android.widget.ListView;
2727

28+
import com.loopj.android.http.RequestParams;
29+
2830
import java.util.ArrayList;
2931
import java.util.List;
3032

@@ -58,7 +60,8 @@ public class WaypointsActivity extends ListActivity {
5860
new SampleConfig(R.string.title_content_type_http_entity, ContentTypeForHttpEntitySample.class),
5961
new SampleConfig(R.string.title_resume_download, ResumeDownloadSample.class),
6062
new SampleConfig(R.string.title_digest_auth, DigestAuthSample.class),
61-
new SampleConfig(R.string.title_use_pool_thread, UsePoolThreadSample.class)
63+
new SampleConfig(R.string.title_use_pool_thread, UsePoolThreadSample.class),
64+
new SampleConfig(R.string.title_request_params_debug, RequestParamsDebug.class)
6265
};
6366

6467
@Override
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.loopj.android.http.sample.util;
2+
3+
import java.lang.reflect.Array;
4+
5+
public class API8Util {
6+
7+
@SuppressWarnings("unchecked")
8+
public static <T> T[] copyOfRange(T[] original, int start, int end) {
9+
int originalLength = original.length; // For exception priority compatibility.
10+
if (start > end) {
11+
throw new IllegalArgumentException();
12+
}
13+
if (start < 0 || start > originalLength) {
14+
throw new ArrayIndexOutOfBoundsException();
15+
}
16+
int resultLength = end - start;
17+
int copyLength = Math.min(resultLength, originalLength - start);
18+
T[] result = (T[]) Array.newInstance(original.getClass().getComponentType(), resultLength);
19+
System.arraycopy(original, start, result, 0, copyLength);
20+
return result;
21+
}
22+
23+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,5 @@
4545
<string name="title_resume_download">Resuming Download</string>
4646
<string name="title_digest_auth">Digest Authentication</string>
4747
<string name="title_use_pool_thread">Use Pool Thread in Response</string>
48+
<string name="title_request_params_debug">Request Params debug</string>
4849
</resources>

0 commit comments

Comments
 (0)