Java HTTP Get doGet(String url, HashMap params)

Here you can find the source of doGet(String url, HashMap params)

Description

do Get

License

Apache License

Declaration

public static String doGet(String url, HashMap<String, String> params)
            throws MalformedURLException, IOException 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;

public class Main {
    public static String doGet(String url, HashMap<String, String> params)
            throws MalformedURLException, IOException {

        String rawJSONString = "";
        String getUrl = buildUrl(url, params);

        HttpURLConnection connection = (HttpURLConnection) new URL(getUrl).openConnection();
        connection.setRequestMethod("GET");

        BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        StringBuilder sb = new StringBuilder();
        String line;// w  ww.  ja v  a 2  s.co m
        while ((line = reader.readLine()) != null) {
            sb.append(line);
        }

        rawJSONString = sb.toString();

        return rawJSONString;
    }

    private static String buildUrl(String url, HashMap<String, String> params) throws UnsupportedEncodingException {

        StringBuilder urlString = new StringBuilder(url);

        if (params.isEmpty()) {
            return urlString.toString();
        }

        urlString.append("?");

        Iterator<Entry<String, String>> it = params.entrySet().iterator();

        while (it.hasNext()) {
            Entry<String, String> entry = it.next();
            urlString.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
            urlString.append("=").append(URLEncoder.encode(entry.getValue(), "UTF-8"));
            urlString.append(it.hasNext() ? '&' : "");
        }

        return urlString.toString();
    }
}

Related

  1. doGET(HttpURLConnection conn)
  2. doGet(String theURL, Map nameValuePairs)
  3. doGet(String url)
  4. doGet(String urlStr)
  5. doGet(String urlString)
  6. doGet(URL url, String... args)
  7. doGetRequest(String urlStr)