Search in sources :

Example 1 with BasicNameValuePair

use of cz.msebera.android.httpclient.message.BasicNameValuePair in project android-async-http by loopj.

the class RequestParams method getParamsList.

private List<BasicNameValuePair> getParamsList(String key, Object value) {
    List<BasicNameValuePair> params = new LinkedList<BasicNameValuePair>();
    if (value instanceof Map) {
        Map map = (Map) value;
        List list = new ArrayList<Object>(map.keySet());
        // Ensure consistent ordering in query string
        if (list.size() > 0 && list.get(0) instanceof Comparable) {
            Collections.sort(list);
        }
        for (Object nestedKey : list) {
            if (nestedKey instanceof String) {
                Object nestedValue = map.get(nestedKey);
                if (nestedValue != null) {
                    params.addAll(getParamsList(key == null ? (String) nestedKey : String.format(Locale.US, "%s[%s]", key, nestedKey), nestedValue));
                }
            }
        }
    } else if (value instanceof List) {
        List list = (List) value;
        int listSize = list.size();
        for (int nestedValueIndex = 0; nestedValueIndex < listSize; nestedValueIndex++) {
            params.addAll(getParamsList(String.format(Locale.US, "%s[%d]", key, nestedValueIndex), list.get(nestedValueIndex)));
        }
    } else if (value instanceof Object[]) {
        Object[] array = (Object[]) value;
        int arrayLength = array.length;
        for (int nestedValueIndex = 0; nestedValueIndex < arrayLength; nestedValueIndex++) {
            params.addAll(getParamsList(String.format(Locale.US, "%s[%d]", key, nestedValueIndex), array[nestedValueIndex]));
        }
    } else if (value instanceof Set) {
        Set set = (Set) value;
        for (Object nestedValue : set) {
            params.addAll(getParamsList(key, nestedValue));
        }
    } else {
        params.add(new BasicNameValuePair(key, value.toString()));
    }
    return params;
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) BasicNameValuePair(cz.msebera.android.httpclient.message.BasicNameValuePair) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Map(java.util.Map) LinkedList(java.util.LinkedList)

Example 2 with BasicNameValuePair

use of cz.msebera.android.httpclient.message.BasicNameValuePair in project android-async-http by loopj.

the class RequestParams method createMultipartEntity.

private HttpEntity createMultipartEntity(ResponseHandlerInterface progressHandler) throws IOException {
    SimpleMultipartEntity entity = new SimpleMultipartEntity(progressHandler);
    entity.setIsRepeatable(isRepeatable);
    // Add string params
    for (ConcurrentHashMap.Entry<String, String> entry : urlParams.entrySet()) {
        entity.addPartWithCharset(entry.getKey(), entry.getValue(), contentEncoding);
    }
    // Add non-string params
    List<BasicNameValuePair> params = getParamsList(null, urlParamsWithObjects);
    for (BasicNameValuePair kv : params) {
        entity.addPartWithCharset(kv.getName(), kv.getValue(), contentEncoding);
    }
    // Add stream params
    for (ConcurrentHashMap.Entry<String, StreamWrapper> entry : streamParams.entrySet()) {
        StreamWrapper stream = entry.getValue();
        if (stream.inputStream != null) {
            entity.addPart(entry.getKey(), stream.name, stream.inputStream, stream.contentType);
        }
    }
    // Add file params
    for (ConcurrentHashMap.Entry<String, FileWrapper> entry : fileParams.entrySet()) {
        FileWrapper fileWrapper = entry.getValue();
        entity.addPart(entry.getKey(), fileWrapper.file, fileWrapper.contentType, fileWrapper.customFileName);
    }
    // Add file collection
    for (ConcurrentHashMap.Entry<String, List<FileWrapper>> entry : fileArrayParams.entrySet()) {
        List<FileWrapper> fileWrapper = entry.getValue();
        for (FileWrapper fw : fileWrapper) {
            entity.addPart(entry.getKey(), fw.file, fw.contentType, fw.customFileName);
        }
    }
    return entity;
}
Also used : BasicNameValuePair(cz.msebera.android.httpclient.message.BasicNameValuePair) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 3 with BasicNameValuePair

use of cz.msebera.android.httpclient.message.BasicNameValuePair in project AndroidStudy by tinggengyan.

the class HttpCookie method run.

@Override
public void run() {
    list.add(new BasicNameValuePair("name", "steve"));
    list.add(new BasicNameValuePair("age", "12"));
    try {
        post.setEntity(new UrlEncodedFormEntity(list));
        HttpResponse response = client.execute(post);
        if (response.getStatusLine().getStatusCode() == 200) {
            AbstractHttpClient absclient = (AbstractHttpClient) client;
            List<Cookie> cookies = absclient.getCookieStore().getCookies();
            for (Cookie cookie : cookies) {
                System.out.println("name==" + cookie.getName());
                System.out.println("value==" + cookie.getValue());
                Message message = new Message();
                message.obj = cookie;
                mHandler.sendMessage(message);
                return;
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : AbstractHttpClient(cz.msebera.android.httpclient.impl.client.AbstractHttpClient) Cookie(cz.msebera.android.httpclient.cookie.Cookie) Message(android.os.Message) BasicNameValuePair(cz.msebera.android.httpclient.message.BasicNameValuePair) HttpResponse(cz.msebera.android.httpclient.HttpResponse) UrlEncodedFormEntity(cz.msebera.android.httpclient.client.entity.UrlEncodedFormEntity)

Example 4 with BasicNameValuePair

use of cz.msebera.android.httpclient.message.BasicNameValuePair in project android-async-http by loopj.

the class RequestParams method getParamsList.

protected List<BasicNameValuePair> getParamsList() {
    List<BasicNameValuePair> lparams = new LinkedList<BasicNameValuePair>();
    for (ConcurrentHashMap.Entry<String, String> entry : urlParams.entrySet()) {
        lparams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
    }
    lparams.addAll(getParamsList(null, urlParamsWithObjects));
    return lparams;
}
Also used : BasicNameValuePair(cz.msebera.android.httpclient.message.BasicNameValuePair) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) LinkedList(java.util.LinkedList)

Example 5 with BasicNameValuePair

use of cz.msebera.android.httpclient.message.BasicNameValuePair in project android-async-http by loopj.

the class RequestParams method toString.

@Override
public String toString() {
    StringBuilder result = new StringBuilder();
    for (ConcurrentHashMap.Entry<String, String> entry : urlParams.entrySet()) {
        if (result.length() > 0)
            result.append("&");
        result.append(entry.getKey());
        result.append("=");
        result.append(entry.getValue());
    }
    for (ConcurrentHashMap.Entry<String, StreamWrapper> entry : streamParams.entrySet()) {
        if (result.length() > 0)
            result.append("&");
        result.append(entry.getKey());
        result.append("=");
        result.append("STREAM");
    }
    for (ConcurrentHashMap.Entry<String, FileWrapper> entry : fileParams.entrySet()) {
        if (result.length() > 0)
            result.append("&");
        result.append(entry.getKey());
        result.append("=");
        result.append("FILE");
    }
    for (ConcurrentHashMap.Entry<String, List<FileWrapper>> entry : fileArrayParams.entrySet()) {
        if (result.length() > 0)
            result.append("&");
        result.append(entry.getKey());
        result.append("=");
        result.append("FILES(SIZE=").append(entry.getValue().size()).append(")");
    }
    List<BasicNameValuePair> params = getParamsList(null, urlParamsWithObjects);
    for (BasicNameValuePair kv : params) {
        if (result.length() > 0)
            result.append("&");
        result.append(kv.getName());
        result.append("=");
        result.append(kv.getValue());
    }
    return result.toString();
}
Also used : BasicNameValuePair(cz.msebera.android.httpclient.message.BasicNameValuePair) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Aggregations

BasicNameValuePair (cz.msebera.android.httpclient.message.BasicNameValuePair)5 LinkedList (java.util.LinkedList)4 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 Message (android.os.Message)1 HttpResponse (cz.msebera.android.httpclient.HttpResponse)1 UrlEncodedFormEntity (cz.msebera.android.httpclient.client.entity.UrlEncodedFormEntity)1 Cookie (cz.msebera.android.httpclient.cookie.Cookie)1 AbstractHttpClient (cz.msebera.android.httpclient.impl.client.AbstractHttpClient)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Set (java.util.Set)1