|
| 1 | +package com.loopj.android.http; |
| 2 | + |
| 3 | +import com.loopj.android.http.interfaces.RequestParamInterface; |
| 4 | +import com.loopj.android.http.interfaces.RequestParamsInterface; |
| 5 | + |
| 6 | +import java.io.IOException; |
| 7 | +import java.io.UnsupportedEncodingException; |
| 8 | +import java.util.ArrayList; |
| 9 | +import java.util.List; |
| 10 | +import java.util.Map; |
| 11 | + |
| 12 | +import cz.msebera.android.httpclient.HttpEntity; |
| 13 | +import cz.msebera.android.httpclient.NameValuePair; |
| 14 | +import cz.msebera.android.httpclient.client.entity.EntityBuilder; |
| 15 | +import cz.msebera.android.httpclient.client.entity.UrlEncodedFormEntity; |
| 16 | +import cz.msebera.android.httpclient.message.BasicNameValuePair; |
| 17 | +import cz.msebera.android.httpclient.util.EntityUtils; |
| 18 | + |
| 19 | +public class HttpEntityFactory { |
| 20 | + |
| 21 | + /** |
| 22 | + * convert and mix @RequestParams and @HttpEntity into one @HttpEntity |
| 23 | + * @param requestParams |
| 24 | + * @param httpEntity |
| 25 | + * @return |
| 26 | + */ |
| 27 | + public static HttpEntity getHttpEntity(RequestParams requestParams, HttpEntity httpEntity) { |
| 28 | + List<NameValuePair> pairs = new ArrayList<>(); |
| 29 | + for (Map.Entry<String, RequestParamInterface> param : requestParams.getParams()) { |
| 30 | + pairs.add(new BasicNameValuePair(param.getKey(), param.getValue().getValue())); |
| 31 | + } |
| 32 | + |
| 33 | + String sEntity = ""; |
| 34 | + try { |
| 35 | + sEntity = EntityUtils.toString(httpEntity); |
| 36 | + sEntity = sEntity.concat(EntityUtils.toString(new UrlEncodedFormEntity(pairs))); |
| 37 | + } catch (IOException e) { |
| 38 | + e.printStackTrace(); |
| 39 | + } |
| 40 | + |
| 41 | + try { |
| 42 | + return EntityBuilder.create().setText(sEntity).build(); |
| 43 | + } catch (Exception e) { |
| 44 | + e.printStackTrace(); |
| 45 | + return null; |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * get @UrlEncodedFormEntity from @RequestParams object |
| 51 | + * @param params |
| 52 | + * @return |
| 53 | + */ |
| 54 | + public static HttpEntity getFormEntity(RequestParamsInterface params) { |
| 55 | + List<NameValuePair> pairs = new ArrayList<NameValuePair>(); |
| 56 | + for (Map.Entry<String, RequestParamInterface> param : params.getParams()) { |
| 57 | + pairs.add(new BasicNameValuePair(param.getKey(), param.getValue().getValue())); |
| 58 | + } |
| 59 | + try { |
| 60 | + return new UrlEncodedFormEntity(pairs); |
| 61 | + } catch (UnsupportedEncodingException e) { |
| 62 | + e.printStackTrace(); |
| 63 | + } |
| 64 | + return null; |
| 65 | + } |
| 66 | +} |
0 commit comments