|
| 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 | +} |
0 commit comments