All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.robolectric.shadows.httpclient.ParamsParser Maven / Gradle / Ivy

The newest version!
package org.robolectric.shadows.httpclient;

import android.net.Uri;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.http.HttpEntity;
import org.apache.http.HttpRequest;
import org.apache.http.NameValuePair;
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URLEncodedUtils;

public class ParamsParser {

  public static Map parseParams(HttpRequest request) {
    if (request instanceof HttpGet) {
      return parseParamsForGet(request);
    }
    if (request instanceof HttpEntityEnclosingRequestBase) {
      return parseParamsForRequestWithEntity((HttpEntityEnclosingRequestBase) request);
    }
    return new LinkedHashMap<>();
  }

  private static Map parseParamsForRequestWithEntity(
      HttpEntityEnclosingRequestBase request) {
    try {
      LinkedHashMap map = new LinkedHashMap<>();
      HttpEntity entity = request.getEntity();
      if (entity != null) {
        List pairs = URLEncodedUtils.parse(entity);

        for (NameValuePair pair : pairs) {
          map.put(pair.getName(), pair.getValue());
        }
      }
      return map;
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  }

  private static Map parseParamsForGet(HttpRequest request) {
    Uri uri = Uri.parse(request.getRequestLine().getUri());
    Set paramNames = uri.getQueryParameterNames();
    LinkedHashMap map = new LinkedHashMap<>();
    for (String paramName : paramNames) {
      map.put(paramName, uri.getQueryParameter(paramName));
    }
    return map;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy