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 String makeRequest(String url, String json) {
    Log.v(TAG, "URL-->" + url);
    Log.v(TAG, "input-->" + json);

    try {/*from w  w  w  . j a v a2s .c  o  m*/
        HttpPost httpPost = new HttpPost(url);
        httpPost.setEntity(new StringEntity(json));
        httpPost.setHeader("Accept", "application/json");
        httpPost.setHeader("Content-type", "application/json");
        httpPost.setHeader("sessionToken", "61");
        //text/html
        HttpResponse httpResponse = new DefaultHttpClient().execute(httpPost);

        // receive response as inputStream
        InputStream inputStream = httpResponse.getEntity().getContent();
        // convert inputstream to string
        if (inputStream != null) {
            String result = convertInputStreamToString(inputStream);
            Log.v(TAG, "output-->" + result);
            return result;
        }
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return "";
}

From source file:org.uiautomation.ios.communication.HttpClientFactory.java

public static DefaultHttpClient getClient() {
    DefaultHttpClient client = new DefaultHttpClient();
    client.setRedirectStrategy(new MyRedirectHandler());
    return client;
}

From source file:com.mmd.mssp.util.HttpUtil.java

public static String httpGet(String uri) throws IOException {
    HttpClient httpclient = new DefaultHttpClient();
    HttpGet httpget = new HttpGet(uri);
    HttpResponse response = httpclient.execute(httpget);

    logger.info(httpget.getURI().toString() + " == " + response.getStatusLine());

    HttpEntity entity = response.getEntity();
    Header[] cts = response.getHeaders("Content-Type");
    String charset = "UTF-8";
    if (cts.length > 0) {
        String ContentType = cts[0].getValue();
        if (ContentType.contains("charset")) {
            charset = ContentType.split("charset=")[1];
        }/*from   w w  w.  j  av  a  2 s .  co  m*/
    }
    if (entity != null) {
        InputStream instream = entity.getContent();
        try {
            StringBuffer buffer = new StringBuffer();
            char[] chars = new char[BUFFER_SIZE];
            while (true) {
                BufferedReader reader = new BufferedReader(new InputStreamReader(instream, charset));
                int len = reader.read(chars);
                if (len < 0) {
                    break;
                }
                buffer.append(chars, 0, len);
            }
            return buffer.toString();
        } catch (IOException ex) {
            throw ex;
        } catch (RuntimeException ex) {
            httpget.abort();
            throw ex;
        } finally {
            instream.close();
            httpclient.getConnectionManager().shutdown();
        }
    }
    return "";
}

From source file:Main.java

public static final String postData(String url, HashMap<String, String> params) {
    // Create a new HttpClient and Post Header
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(url);
    String outputString = null;/* w  ww.  j av a  2s  .  c o  m*/

    try {
        // Add your data
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(params.size());
        Iterator it = params.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry pair = (Map.Entry) it.next();
            nameValuePairs.add(new BasicNameValuePair((String) pair.getKey(), (String) pair.getValue()));
            System.out.println(pair.getKey() + " = " + pair.getValue());
            it.remove(); // avoids a ConcurrentModificationException
        }
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);

        StatusLine statusLine = response.getStatusLine();
        if (statusLine.getStatusCode() == HttpURLConnection.HTTP_OK) {
            byte[] result = EntityUtils.toByteArray(response.getEntity());
            outputString = new String(result, "UTF-8");
        }

    } catch (ClientProtocolException e) {
        Log.i(CLASS_NAME, "Client protocolException happened: " + e.getMessage());
    } catch (IOException e) {
        Log.i(CLASS_NAME, "Client IOException happened: " + e.getMessage());
    } catch (NetworkOnMainThreadException e) {
        Log.i(CLASS_NAME, "Client NetworkOnMainThreadException happened: " + e.getMessage());
        e.printStackTrace();
    } catch (Exception e) {
        Log.i(CLASS_NAME, "Unknown exeception: " + e.getMessage());
    }
    return outputString;
}

From source file:com.mehmetakiftutuncu.eshotroid.utilities.Request.java

public static String get(String url) {
    try {//ww w  .j  av a 2  s  . c  o m
        Log.info(Request.class, "Making GET request to url: " + url);

        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(url);

        HttpResponse httpResponse = httpClient.execute(httpGet);

        int statusCode = httpResponse.getStatusLine().getStatusCode();
        if (statusCode != 200) {
            Log.error(Request.class,
                    "GET request failed, invalid status code! status code: " + statusCode + ", url: " + url);
            return null;
        } else {
            HttpEntity httpEntity = httpResponse.getEntity();
            String result = EntityUtils.toString(httpEntity, "UTF-8");

            Log.info(Request.class, "GET result from url: " + url);
            Log.info(Request.class, result);

            return result;
        }
    } catch (Exception e) {
        Log.error(Request.class, "GET request failed! url: " + url, e);
        return null;
    }
}

From source file:cardgametrackercs319.DBConnectionManager.java

public static String saveTrackScores(TrackEngine engine) throws UnsupportedEncodingException, IOException {
    HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost("http://ozymaxx.net/cs319/save_tracks_scores.php");

    post.setHeader("User-Agent", USER_AGENT);
    ArrayList<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
    urlParameters.add(new BasicNameValuePair("info", engine.getJSON()));

    post.setEntity(new UrlEncodedFormEntity(urlParameters));

    HttpResponse response = client.execute(post);
    BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
    StringBuffer result = new StringBuffer();
    String resLine = "";

    while ((resLine = reader.readLine()) != null) {
        result.append(resLine);/*from   w  w w .j a  v  a  2 s .com*/
    }

    return result.toString();
}

From source file:core.RESTCalls.RESTDelete.java

public static String httpDelete(String urlStr) {

    HttpClient client = new DefaultHttpClient();

    HttpDelete delete = new HttpDelete(urlStr);

    String result = null;//from   ww  w .j a va  2s  .c o m

    try {

        HttpResponse response = client.execute(delete);

        if (response != null && response.getEntity() != null) {

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

            StringBuilder total = new StringBuilder();

            String line = null;

            while ((line = r.readLine()) != null)
                total.append(line + "\n");

            result = total.toString();
        }
    } catch (Exception ex) {

        ex.printStackTrace();
    }

    return result;
}

From source file:com.phonemetra.turbo.lockclock.weather.HttpRetriever.java

public static String retrieve(String url) {
    HttpGet request = new HttpGet(url);
    try {//from ww w.ja  v a2 s .c  o  m
        HttpResponse response = new DefaultHttpClient().execute(request);
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            return EntityUtils.toString(entity);
        }
    } catch (IOException e) {

    }
    return null;
}

From source file:core.RESTCalls.RESTGet.java

public static String httpGet(String urlStr) throws Exception {

    HttpClient client = new DefaultHttpClient();

    HttpGet get = new HttpGet(urlStr);

    HttpResponse response = client.execute(get);

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

    String content = "", line = null;

    while ((line = rd.readLine()) != null)
        content += line + "\n";

    return content;
}

From source file:de.randec.MVBMonitor.Downloader.java

static String downloadJson(String url) throws IOException {
    String data = "";
    try {/*from  ww  w  . j  ava 2  s .  co  m*/
        // defaultHttpClient
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);
        HttpResponse response = httpClient.execute(httpPost);
        data = EntityUtils.toString(response.getEntity(), "utf-8");
        //data = EntityUtils.toString(response.getEntity());
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    }
    //unescape escape codes (Umlaute)
    return StringEscapeUtils.unescapeHtml4(data);

}