Example usage for org.apache.http.client.utils URLEncodedUtils format

List of usage examples for org.apache.http.client.utils URLEncodedUtils format

Introduction

In this page you can find the example usage for org.apache.http.client.utils URLEncodedUtils format.

Prototype

public static String format(final Iterable<? extends NameValuePair> parameters, final Charset charset) 

Source Link

Document

Returns a String that is suitable for use as an application/x-www-form-urlencoded list of parameters in an HTTP PUT or HTTP POST.

Usage

From source file:com.victorkifer.AndroidTemplates.api.Downloader.java

private static HttpResponse makeHttpGetRequest(String url, List<NameValuePair> params) throws IOException {
    if (params != null) {
        String paramString = URLEncodedUtils.format(params, "utf-8");
        url += "?" + paramString;
    }/*from   w  w w.  j ava2s . co  m*/

    Logger.e(TAG, url);

    HttpGet httpGet = new HttpGet(url);
    return httpClient.execute(httpGet);
}

From source file:org.mobicents.servlet.restcomm.util.HttpUtils.java

public static String toString(final Header[] headers) {
    final List<NameValuePair> parameters = new ArrayList<NameValuePair>();
    for (final Header header : headers) {
        parameters.add(new BasicNameValuePair(header.getName(), header.getValue()));
    }//from w w  w .  j  a  v  a2  s.  c o  m
    return URLEncodedUtils.format(parameters, "UTF-8");
}

From source file:com.gmobi.poponews.util.HttpHelper.java

public static String getParamString(Map<String, String> params) {
    List<BasicNameValuePair> lparams = new LinkedList<BasicNameValuePair>();
    for (String key : params.keySet()) {
        lparams.add(new BasicNameValuePair(key, params.get(key)));
    }//from w w w .j a v  a  2s .co  m
    return URLEncodedUtils.format(lparams, "UTF-8");
}

From source file:com.thoughtworks.go.http.mocks.HttpRequestBuilder.java

public HttpRequestBuilder withPath(String path) {
    try {/*from   w  w  w  . j  a  v  a2 s.  c om*/
        URIBuilder uri = new URIBuilder(path);
        request.setServerName("test.host");
        request.setContextPath(CONTEXT_PATH);
        request.setParameters(splitQuery(uri));
        request.setRequestURI(CONTEXT_PATH + uri.getPath());
        request.setServletPath(uri.getPath());
        if (!uri.getQueryParams().isEmpty()) {
            request.setQueryString(URLEncodedUtils.format(uri.getQueryParams(), UTF_8));
        }
        return this;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:org.maikelwever.droidpile.backend.TestApiConnecter.java

@Override
public String postData(ArrayList<NameValuePair> payload, String... data) throws IOException {
    String suffix = "";
    for (String arg : data) {
        suffix += "/" + arg;
    }//from   ww  w.j  ava  2s . c  om
    String urlParameters = URLEncodedUtils.format(payload, "UTF-8");
    String key = suffix + urlParameters;

    if (testPostData.containsKey(key)) {
        return testPostData.get(key);
    } else {
        Log.d("droidpile", "Error: data not in testdata. Url: " + key);
        return null;
    }
}

From source file:movie.rating.retriever.JSONResponse.java

public JSONObject makeHttpRequest(String url, String method, List<NameValuePair> params) throws Exception {

    // Making HTTP request

    // check for request method
    if (method == "POST") {
        // request method is POST
        // defaultHttpClient
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);
        httpPost.setEntity(new UrlEncodedFormEntity(params));

        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        is = httpEntity.getContent();//  w w w.j av  a2s.c  o  m

    } else if (method == "GET") {
        // request method is GET
        DefaultHttpClient httpClient = new DefaultHttpClient();
        if (params != null) {
            String paramString = URLEncodedUtils.format(params, "utf-8");
            url += "?" + paramString;
        }
        HttpGet httpGet = new HttpGet(url);

        HttpResponse httpResponse = httpClient.execute(httpGet);
        HttpEntity httpEntity = httpResponse.getEntity();
        is = httpEntity.getContent();
    }

    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 20);
        StringBuilder sb = new StringBuilder();
        String line = null;

        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();
        json = sb.toString();

    } catch (Exception e) {
        System.out.println("Error converting result " + e.toString());
    }

    // try parse the string to a JSON object

    //System.out.println("JSON String"+ json);
    try {
        jObj = new JSONObject(json);

    } catch (Exception e) {
        System.out.println("Error parsing data " + e.toString());
    }

    // return JSON String
    return jObj;

}

From source file:ch.iterate.openstack.swift.model.Region.java

public URI getStorageUrl(String container, String object, List<NameValuePair> parameters) {
    return URI.create(String.format("%s?%s", this.getStorageUrl(container, object),
            URLEncodedUtils.format(parameters, "UTF-8")));
}

From source file:restApi.ApiCaller.java

@Override
protected Void doInBackground() throws IOException {

    List<NameValuePair> params = new LinkedList<NameValuePair>();

    params.add(new BasicNameValuePair("q", q));
    params.add(new BasicNameValuePair("format", format));
    params.add(new BasicNameValuePair("diagnostics", diagnostics));
    params.add(new BasicNameValuePair("env", env));

    String paramString = URLEncodedUtils.format(params, "utf-8");

    builtUrl = baseUrl + paramString;//from w w w.j ava2s .  c o  m

    HttpClient client = HttpClients.createDefault();

    HttpGet request = new HttpGet(builtUrl);

    // add request header
    request.addHeader("User-Agent", USER_AGENT);

    HttpResponse response = client.execute(request);

    BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

    StringBuffer result = new StringBuffer();
    String line = "";
    while ((line = rd.readLine()) != null) {
        result.append(line);
    }

    jsonStr = result.toString();

    return null;

}

From source file:com.strato.hidrive.api.connection.httpgateway.request.PostRequest.java

@SuppressWarnings("unchecked")
protected HttpRequestBase createHttpRequest(String requestUri, HttpRequestParamsVisitor<?> visitor)
        throws UnsupportedEncodingException {
    HttpPost httpPost = new HttpPost(requestUri);
    //There used StringEntity and some string manipulations instead of UrlEncodedFormEntity due to problem in url encoding ("+" instead "%20")
    //details: http://stackoverflow.com/questions/7915029/how-to-encode-space-as-20-in-urlencodedformentity-while-executing-apache-httppo
    String entityValue = URLEncodedUtils.format((List<NameValuePair>) visitor.getHttpRequestParams(),
            HTTP.UTF_8);/*from   ww  w. java2 s  . c o m*/
    entityValue = entityValue.replaceAll("\\+", "%20");
    StringEntity entity = new StringEntity(entityValue, HTTP.UTF_8);
    entity.setContentType(URLEncodedUtils.CONTENT_TYPE);
    httpPost.setEntity(entity);
    //original code:
    //httpPost.setEntity(new UrlEncodedFormEntity((List<? extends NameValuePair>) visitor.getHttpRequestParams(), HTTP.UTF_8));
    return httpPost;
}

From source file:cr.ac.siua.tec.services.impl.RTServiceImpl.java

/**
 * Returns all of the ticket's data in a hashmap. If the ticket doesn't exist returns null.
 *//*from  w  w w .j a  v  a 2  s  .c  o m*/
public HashMap<String, String> getTicket(String ticketId) {
    HashMap<String, String> ticketContent = null;
    String url = this.baseUrl + "/ticket/" + ticketId + "/show";

    try (CloseableHttpClient client = HttpClientBuilder.create().build()) {
        List<NameValuePair> urlParameters = new ArrayList<>();
        urlParameters.add(new BasicNameValuePair("user", this.rtUser));
        urlParameters.add(new BasicNameValuePair("pass", this.rtPw));
        StringBuilder requestUrl = new StringBuilder(url);
        String queryString = URLEncodedUtils.format(urlParameters, "utf-8");
        requestUrl.append("?");
        requestUrl.append(queryString);

        HttpGet get = new HttpGet(requestUrl.toString());
        HttpResponse response = client.execute(get);
        String responseString = getResponseString(response);
        ticketContent = ticketStringToHashMap(responseString);
        return ticketContent;
    } catch (IOException e) {
        System.out.println("Exception while trying to get RT Ticket using GET.");
        e.printStackTrace();
        return null;
    }
}