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 HttpResponse getData(String url) {
    HttpResponse response = null;/*from   w ww. j  av a 2s  . c o m*/
    try {
        HttpClient client = new DefaultHttpClient();
        HttpGet request = new HttpGet();
        request.setURI(new URI(url));
        response = client.execute(request);
    } catch (URISyntaxException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return response;
}

From source file:org.radiognu.radiognu.utils.utilsradiognu.java

public static final String getResponseServiceAPI(String... uri) {
    HttpClient httpclient = new DefaultHttpClient();
    HttpResponse response;/*  w w w .ja va2 s  .c  o m*/
    String responseString = null;
    try {
        // make a HTTP request
        response = httpclient.execute(new HttpGet(uri[0]));
        StatusLine statusLine = response.getStatusLine();
        if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
            // request successful - read the response and close the connection
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            response.getEntity().writeTo(out);
            out.close();
            responseString = out.toString();
        } else {
            // request failed - close the connection
            response.getEntity().getContent().close();
            throw new IOException(statusLine.getReasonPhrase());
        }
    } catch (Exception e) {
    }
    return responseString;
}

From source file:Main.java

public static String getInputStreamFromUrl(String url) {
    InputStream content = null;/*from  w w  w  .  jav  a  2 s  .  c o  m*/
    String res = "";
    try {
        HttpGet httpGet = new HttpGet(url);
        HttpClient httpclient = new DefaultHttpClient();
        // Execute HTTP Get Request
        HttpResponse response = httpclient.execute(httpGet);
        content = response.getEntity().getContent();
        res = InputStreamtoString(content);
    } catch (Exception e) {
        e.printStackTrace();
    }

    return res;
}

From source file:Main.java

public static String GET(String url) {
    InputStream inputStream = null;
    String result = "";
    try {/*from   www.  j  av  a 2s. co  m*/

        // create HttpClient
        HttpClient httpclient = new DefaultHttpClient();

        // make GET request to the given URL
        HttpResponse httpResponse = httpclient.execute(new HttpGet(url));

        // receive response as inputStream
        inputStream = httpResponse.getEntity().getContent();

        // convert inputstream to string
        if (inputStream != null)
            result = convertInputStreamToString(inputStream);
        else
            result = "Did not work!";

    } catch (Exception e) {
        Log.d("InputStream", e.getLocalizedMessage());
    }

    return result;
}

From source file:Main.java

/**
 * Code adapted from http://stackoverflow.com/a/12854981
 * @return Device external IP address or ERROR statement.
 *///  www.  j  av a 2 s.c  o m
public static String getExternalIP() {
    try {
        HttpClient httpclient = new DefaultHttpClient();
        HttpGet httpget = new HttpGet("http://ip2country.sourceforge.net/ip2c.php?format=JSON");
        // HttpGet httpget = new HttpGet("http://whatismyip.com.au/");
        // HttpGet httpget = new HttpGet("http://www.whatismyip.org/");
        HttpResponse response = httpclient.execute(httpget);
        HttpEntity entity = response.getEntity();
        if (entity != null && entity.getContentLength() > 0) {
            JSONObject json_data = new JSONObject(EntityUtils.toString(entity));
            return json_data.getString("ip");
        } else {
            Log.e(LOG_TAG, "Response error: " + response.getStatusLine().toString());
        }
    } catch (Exception e) {
        Log.e(LOG_TAG, e.toString());
    }
    return "ERROR";
}

From source file:Main.java

public static String getJson(String url) throws ClientProtocolException, IOException, InterruptedException {
    StringBuilder builder = new StringBuilder();
    HttpClient client = new DefaultHttpClient();
    HttpGet httpGet = new HttpGet(url);
    HttpResponse response = client.execute(httpGet);
    StatusLine statusLine = response.getStatusLine();
    int statusCode = statusLine.getStatusCode();
    if (statusCode == 200) {
        HttpEntity entity = response.getEntity();
        InputStream content = entity.getContent();
        BufferedReader reader = new BufferedReader(new InputStreamReader(content));
        String line;//  ww  w  .  j  a  v a 2s.  co m
        while ((line = reader.readLine()) != null)
            builder.append(line);
    }
    return builder.toString();
}

From source file:Main.java

public static JSONObject callService(String url) {
    InputStream inputStream = null;
    JSONObject result = null;//from w  ww  .  j  a  v  a2 s  .  c om
    try {
        // create HttpClient
        HttpClient httpclient = new DefaultHttpClient();
        // make GET request to the given URL
        HttpResponse httpResponse = httpclient.execute(new HttpGet(url));
        // receive response as inputStream
        inputStream = httpResponse.getEntity().getContent();
        // convert inputstream to string
        if (inputStream != null) {
            String response = convertInputStreamToString(inputStream);
            //result = new JSONObject(response);
        }
    } catch (Exception e) {
        Log.d("InputStream", e.getLocalizedMessage());
    }
    return result;
}

From source file:Main.java

public static String getJSONFromUrlWithPostRequest(String url, List<NameValuePair> params) {

    try {//from   w  w w.  j a va 2  s  .  co m
        HttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);

        httpPost.setEntity(new UrlEncodedFormEntity(params));

        HttpResponse httpResponse = httpClient.execute(httpPost);
        String tempResponse = EntityUtils.toString(httpResponse.getEntity());

        return tempResponse;

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return "";
}

From source file:Main.java

/**
 * fetch JSONArray from given url, must be called on worker thread
 * @param url/*w w w  .j  a  v a2 s  . c  om*/
 * @return
 * @throws JSONException
 * @throws IOException
 */
public static JSONArray getRemoteJsonArray(String url) throws JSONException, IOException {
    HttpClient client = new DefaultHttpClient();
    HttpGet get = new HttpGet(url);
    HttpResponse response = client.execute(get);
    BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

    StringBuilder builder = new StringBuilder();
    for (String s = reader.readLine(); s != null; s = reader.readLine()) {
        builder.append(s);
    }
    return new JSONArray(builder.toString());
}

From source file:Main.java

protected static synchronized InputStream getAWebPage(URL url) throws ClientProtocolException, IOException {
    HttpClient httpClient = new DefaultHttpClient();
    HttpContext localContext = new BasicHttpContext();
    HttpGet httpGet = new HttpGet(url.toString());
    try {//from  w  w  w. jav  a2 s .  c o m
        //do the request
        HttpResponse response = httpClient.execute(httpGet, localContext);
        StatusLine status = response.getStatusLine();
        if (status.getStatusCode() != HTTP_STATUS_OK) {
            throw new IOException("Invalid response from the IKSU server! " + status.toString());
        }
        //InputStream ist = response.getEntity().getContent();

        return response.getEntity().getContent();
    } catch (Exception e) {
        e.printStackTrace();
        throw new ClientProtocolException("Protocol Exception! " + e.toString());
    }
}