Example usage for org.apache.http.impl.client DefaultHttpClient DefaultHttpClient

List of usage examples for org.apache.http.impl.client DefaultHttpClient DefaultHttpClient

Introduction

In this page you can find the example usage for org.apache.http.impl.client DefaultHttpClient DefaultHttpClient.

Prototype

public DefaultHttpClient() 

Source Link

Usage

From source file:Main.java

public static Bitmap getGoogleMapThumbnail(double latitude, double longitude) {
    String URL = "http://maps.google.com/maps/api/staticmap?center=" + latitude + "," + longitude
            + "&zoom=15&size=600x600&sensor=false";

    Bitmap bmp = null;//from w ww  .j a  v a 2  s .c  o m
    HttpClient httpclient = new DefaultHttpClient();
    HttpGet request = new HttpGet(URL);

    InputStream in = null;
    try {
        in = httpclient.execute(request).getEntity().getContent();
        bmp = BitmapFactory.decodeStream(in);
        in.close();
    } catch (IllegalStateException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return bmp;
}

From source file:Main.java

public static String openPostConnection(String urlString, Map<String, String> map) {
    HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost(urlString);
    List<NameValuePair> nameValuePairs = new ArrayList<>();
    for (String key : map.keySet()) {
        nameValuePairs.add(new BasicNameValuePair(key, map.get(key)));
    }/*from  w ww  .j a v  a2s  . c  o m*/
    try {
        post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    try {
        HttpResponse httpResponse = client.execute(post);
        BufferedReader br = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent()));
        StringBuilder json = new StringBuilder("");
        String line = null;
        while ((line = br.readLine()) != null) {
            json.append(line);
        }
        return json.toString();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static String downloadPage(String url) {
    Log.v(DEBUG_TAG, url);//from w  w w. j a  va2 s . c  o  m

    String page = "";

    BufferedReader in = null;
    try {
        HttpClient client = new DefaultHttpClient();
        HttpGet request = new HttpGet(url);
        HttpParams params = request.getParams();
        HttpConnectionParams.setSoTimeout(params, 60000); // 1 minute timeout               
        HttpResponse response = client.execute(request);

        //Predifined buffer size
        /*
         * 
        in = new BufferedReader(
            new InputStreamReader(
           response.getEntity().getContent()),8*2000);
         * 
         * */

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

        StringBuffer sb = new StringBuffer("");
        String line = "";
        //String NL = System.getProperty("line.separator");
        while ((line = in.readLine()) != null) {
            sb.append(line);
        }
        in.close();
        page = sb.toString();
        Log.v(DEBUG_TAG, "Pagina descargada --> " + page);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
                Log.v(DEBUG_TAG, "Error en la descarga de la pagina");
                e.printStackTrace();
            }
        }
    }
    return page;
}

From source file:Main.java

/**
 * Makes a POST call to the server./*from  w  ww  .ja  v a2  s  .c o m*/
 * @param params 0-> Call Method = POST; 1-> URL; ... -> Params
 * @return
 */
public static String httpPost(String... params) {
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(params[1]);

    try {
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        for (int i = 1; i + 1 < params.length; i += 2) {
            nameValuePairs.add(new BasicNameValuePair(params[i], params[i + 1]));
        }
        if (!nameValuePairs.isEmpty()) {
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        }

        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);
        System.out.println(response.toString());
        return response.toString();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
        return "Client Protocol Exception";
    } catch (IOException e) {
        e.printStackTrace();
        return "POST: Failed to connect (" + params[1] + ")";
    }
}

From source file:Main.java

public static byte[] doPostSubmit(String url, List<NameValuePair> params) {
    HttpClient httpClient = new DefaultHttpClient();
    HttpPost httpPost = new HttpPost();
    try {//from   w w w . jav a  2s.  c om
        httpPost.setEntity(new UrlEncodedFormEntity(params, "utf-8"));
        HttpResponse response = httpClient.execute(httpPost);
        if (response.getStatusLine().getStatusCode() == 200) {
            HttpEntity entity = response.getEntity();
            return EntityUtils.toByteArray(entity);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static String getStringFromUrl(List<NameValuePair> nameValuePairs)
        throws ClientProtocolException, IOException {
    String url = "http://www.fsurugby.org/serve/request.php";
    DefaultHttpClient client = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(url);
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    HttpResponse response = client.execute(httppost);
    HttpEntity entity = response.getEntity();
    BufferedHttpEntity buffer = new BufferedHttpEntity(entity);
    InputStream is = buffer.getContent();
    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    StringBuilder sb = new StringBuilder();
    String line;/*from  w  ww.  j  a v a2s . c o m*/
    while ((line = reader.readLine()) != null) {
        sb.append(line);
    }

    return sb.toString();
}

From source file:com.Kuesty.services.JSONRequest.java

public static void execute(String uri, JSONRequestTaskHandler rsh) {

    HttpClient httpclient = new DefaultHttpClient();
    HttpResponse response;// ww  w. j  ava  2s  . c o  m
    String responseString = null;

    try {
        response = httpclient.execute(new HttpGet(uri));
        StatusLine statusLine = response.getStatusLine();
        if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            response.getEntity().writeTo(out);
            out.close();
            responseString = out.toString();

        } else {
            //Closes the connection.
            response.getEntity().getContent().close();
            throw new IOException(statusLine.getReasonPhrase());
        }
    } catch (ClientProtocolException e) {
        rsh.onError(e.getMessage());
    } catch (IOException e) {
        rsh.onError(e.getMessage());
    }
    try {
        JSONObject response1 = new JSONObject(responseString);
        rsh.onSuccess(response1);

    } catch (JSONException e) {
        rsh.onError(e.getMessage());
    }
}

From source file:Main.java

/**
 * @author Cheb//from w w  w  .  ja v a 2s.c o  m
 * @param URL - URL to server
 * @param context - context
 * @param mPreferences SharedPreferences
 * downloading JSON from URL
 */
public static String downloadJSON(String URL, Context context, SharedPreferences mPreferences) {
    StringBuilder sb = new StringBuilder();
    DefaultHttpClient mHttpClient = new DefaultHttpClient();
    HttpGet dhttpget = new HttpGet(URL);
    HttpResponse dresponse = null;
    try {
        dresponse = mHttpClient.execute(dhttpget);
    } catch (IOException e) {
        e.printStackTrace();
    }
    int status = dresponse.getStatusLine().getStatusCode();
    if (status == 200) {
        char[] buffer = new char[1];
        try {
            InputStream content = dresponse.getEntity().getContent();
            InputStreamReader isr = new InputStreamReader(content);
            while (isr.read(buffer) != -1) {
                sb.append(buffer);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        //saving JSON 
        mPreferences = context.getSharedPreferences(TAG_FEED, 0);
        mPreferences.edit().putString(TAG_FEED, sb.toString()).commit();
    } else {
        Log.i("Error", "Connection error : " + Integer.toString(status));
    }
    return sb.toString();
}

From source file:com.sender.request.SenderUtil.java

public static void post() throws Exception {
    HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost("http://localhost:8080/ServicioWebUtil/ServicioUtil?wsdl");
    String xml = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:util=\"http://util.servicio.com/\">\n"
            + "   <soapenv:Header/>\n" + "   <soapenv:Body>\n" + "      <util:saludar/>\n"
            + "   </soapenv:Body>\n" + "</soapenv:Envelope>";
    HttpEntity entity = new ByteArrayEntity(xml.getBytes("UTF-8"));
    post.setEntity(entity);//from  w  ww . j  av  a2 s  .  c om
    HttpResponse response = client.execute(post);
    String result = EntityUtils.toString(response.getEntity());

    System.out.println("Resultado: " + result);
}

From source file:com.weavers.duqhan.util.CurrencyConverter.java

public static Double convert(String currencyFrom, String currencyTo) throws IOException {
    HttpClient httpclient = new DefaultHttpClient();
    //        HttpGet httpGet = new HttpGet("https://finance.yahoo.com/webservice/v1/symbols/allcurrencies/quote?format=json");
    HttpGet httpGet = new HttpGet("https://cdn.shopify.com/s/javascripts/currencies.js");
    ResponseHandler<String> responseHandler = new BasicResponseHandler();
    String responseBody = httpclient.execute(httpGet, responseHandler);
    httpclient.getConnectionManager().shutdown();

    String rates = responseBody.split("rates:")[1].split("convert")[0];
    rates = rates.substring(0, rates.length() - 1);
    ObjectMapper mapper = new ObjectMapper();
    CurrencyRates jSONReader = null;/* w  w  w  .j  a v  a 2 s  .  co  m*/
    jSONReader = mapper.readValue(rates, CurrencyRates.class);
    //        System.out.println("ssssssssssss == " + jSONReader.getINR());
    double ratio = 0.0;
    if (currencyTo.equals("USD")) {
        ratio = jSONReader.getINR() / jSONReader.getUSD();
    } else {
        ratio = jSONReader.getUSD() / jSONReader.getINR();
    }
    return ratio;
}