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:com.cyanogenmod.lockclock.weather.HttpRetriever.java

public static String retrieve(String url) {
    HttpGet request = new HttpGet(url);
    try {// w  w  w .  j  a  v a  2  s  .  c o m
        HttpResponse response = new DefaultHttpClient().execute(request);
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            return EntityUtils.toString(entity);
        }
    } catch (IOException e) {
        Log.e(TAG, "Couldn't retrieve data", e);
    }
    return null;
}

From source file:com.niceapps.app.HttpClient.java

public static JSONObject SendHttpPost(String URL, JSONObject jsonObjSend) {

    try {/* w ww . java  2s  .  c om*/
        DefaultHttpClient httpclient = new DefaultHttpClient();
        HttpPost httpPostRequest = new HttpPost(URL);

        StringEntity se;
        se = new StringEntity(jsonObjSend.toString());

        // Set HTTP parameters
        httpPostRequest.setEntity(se);
        httpPostRequest.setHeader("Accept", "application/json");
        httpPostRequest.setHeader("Content-Type", "application/json");

        HttpResponse response = (HttpResponse) httpclient.execute(httpPostRequest);

        if (response.getStatusLine().getStatusCode() == 200) {
            HttpEntity entity = response.getEntity();
            JSONObject jsonObjRecv = new JSONObject(EntityUtils.toString(entity));

            return jsonObjRecv;
        } else {
            return null;
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:com.leprechaun.solveig.http.RequestExecutor.java

public static ResponseEntity simpleExecutor(String host, String port, String query) {

    ResponseEntity responseEntity = new ResponseEntity();

    HttpClient client = new DefaultHttpClient();

    String url = "http://" + host + ":" + port + "/query?q=" + query;

    System.out.println(url);/*from  w w  w .  j av a 2 s . c o  m*/

    HttpGet get = new HttpGet(url);

    try {
        HttpResponse response = client.execute(get);
        HttpEntity entity = response.getEntity();
        String responseString = EntityUtils.toString(entity, "UTF-8");
        System.out.println(responseString);

        responseEntity.setCode(response.getStatusLine().toString());
        responseEntity.setBody(responseString);
    } catch (IOException e) {
        System.out.println(e.getMessage());

        responseEntity.setCode("ERROR");
        responseEntity.setBody(e.getMessage());
    }

    return responseEntity;
}

From source file:cloud4all.Utils.NetworkAnode.java

public static void sendRequest(String anode) {
    AsyncTask<String, Void, Void> at = new AsyncTask<String, Void, Void>() {
        @Override//  w  w w. j  a  va 2  s.  c o  m
        protected Void doInBackground(String... url) {
            try {
                HttpClient client = new DefaultHttpClient();
                HttpGet command = new HttpGet(url[0]);
                HttpResponse response = client.execute(command);

                InputStreamReader isr = new InputStreamReader(response.getEntity().getContent());
                BufferedReader in = new BufferedReader(isr);
                String res = in.readLine();
                Log.d("GPIIUserListeners", "GPII has returned: " + res);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
    };
    at.execute(anode);
}

From source file:Main.java

public static String postReqAsJson(String uri, String requestJson) throws ClientProtocolException, IOException {
    Log.i(TAG, "Send data to :" + uri + " ========== and the data str:" + requestJson);
    HttpPost post = new HttpPost(uri);
    List<NameValuePair> parameters = new ArrayList<NameValuePair>();
    parameters.add(new BasicNameValuePair("attendanceClientJSON", requestJson));
    post.setEntity(new UrlEncodedFormEntity(parameters, "UTF-8"));
    HttpClient client = new DefaultHttpClient();
    HttpResponse response = client.execute(post);
    if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
        String retStr = EntityUtils.toString(response.getEntity());
        Log.i(TAG, "=================response str:" + retStr);
        return retStr;
    }/*from ww  w  .jav a 2  s .c o  m*/

    return response.getStatusLine().getStatusCode() + "ERROR";
}

From source file:se.anyro.tagtider.utils.Http.java

public static DefaultHttpClient getClient() {
    if (client == null) {
        client = new DefaultHttpClient();
        Credentials creds = new UsernamePasswordCredentials("tagtider", "codemocracy");
        AuthScope scope = new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM);
        client.getCredentialsProvider().setCredentials(scope, creds);
    }/*w  ww  .j  a va 2 s  .  co m*/
    return client;
}

From source file:Main.java

/**
 * Configures the httpClient to connect to the URL provided.
 */// w  ww .java2 s.co m
public static HttpClient getHttpClient() {
    HttpClient httpClient = new DefaultHttpClient();
    final HttpParams params = httpClient.getParams();
    HttpConnectionParams.setConnectionTimeout(params, HTTP_REQUEST_TIMEOUT_MS);
    HttpConnectionParams.setSoTimeout(params, HTTP_REQUEST_TIMEOUT_MS);
    ConnManagerParams.setTimeout(params, HTTP_REQUEST_TIMEOUT_MS);
    return httpClient;
}

From source file:org.android.CPForAndroidPlusPlus.HttpClient.java

public static String makeRequest(String path) throws Exception {
    final String TAG = "HttpClient"; //Used in logging etc.
    String str = "";

    try {/* w ww .  jav a2s .c  om*/
        DefaultHttpClient httpclient = new DefaultHttpClient();
        //HttpParams params = new BasicHttpParams();
        //params.setParameter(CoreConnectionPNames.SO_TIMEOUT, 1000L);
        //httpclient.setParams(params);
        HttpGet httpost = new HttpGet(path);

        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        str = (String) httpclient.execute(httpost, responseHandler);
    } catch (Exception e) {
        Log.e(TAG, e.getMessage());
    }
    return str;
}

From source file:com.cbtec.eliademyutils.EliademyUtils.java

public static String serviceCall(String data, String webService, String token, String serviceClient) {

    String retval = null;/*w  w  w .ja  v a2  s  .  co m*/
    Log.d("EliademyUtils", "Service: " + data + " Service: " + webService);

    try {
        DefaultHttpClient httpclient = new DefaultHttpClient();
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

        if (data.toString().length() > 0) {
            JSONObject jsObj = new JSONObject(data.toString());
            nameValuePairs.addAll(nameValueJson(jsObj, ""));
        }
        nameValuePairs.add(new BasicNameValuePair("wstoken", token));
        nameValuePairs.add(new BasicNameValuePair("wsfunction", webService));
        nameValuePairs.add(new BasicNameValuePair("moodlewsrestformat", "json"));

        HttpPost httppost = new HttpPost(serviceClient + "/webservice/rest/server.php?");

        Log.d("EliademyUtils", nameValuePairs.toString());
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            StringBuilder jsonStr = new StringBuilder();
            InputStream iStream = entity.getContent();
            BufferedReader bufferReader = new BufferedReader(new InputStreamReader(iStream));
            String jpart;
            while ((jpart = bufferReader.readLine()) != null) {
                jsonStr.append(jpart);
            }
            iStream.close();
            return jsonStr.toString();
        }
    } catch (Exception e) {
        Log.e("EliademyUtils", "exception", e);
        return retval;
    }
    return retval;
}

From source file:Main.java

/**
 * Op Http post request , "404error" response if failed
 * /*from w ww.j av  a2s  . c  om*/
 * @param url
 * @param map
 *            Values to request
 * @return
 */

static public String doHttpPost(String url, HashMap<String, String> map) {

    HttpClient client = new DefaultHttpClient();
    HttpConnectionParams.setConnectionTimeout(client.getParams(), TIMEOUT);
    HttpConnectionParams.setSoTimeout(client.getParams(), TIMEOUT);
    ConnManagerParams.setTimeout(client.getParams(), TIMEOUT);
    HttpPost post = new HttpPost(url);
    post.setHeaders(headers);
    String result = "ERROR";
    ArrayList<BasicNameValuePair> pairList = new ArrayList<BasicNameValuePair>();
    if (map != null) {
        for (Map.Entry<String, String> entry : map.entrySet()) {
            BasicNameValuePair pair = new BasicNameValuePair(entry.getKey(), entry.getValue());
            pairList.add(pair);
        }

    }
    try {
        HttpEntity entity = new UrlEncodedFormEntity(pairList, "UTF-8");
        post.setEntity(entity);
        HttpResponse response = client.execute(post);

        if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {

            result = EntityUtils.toString(response.getEntity(), "UTF-8");

        } else {
            result = EntityUtils.toString(response.getEntity(), "UTF-8")
                    + response.getStatusLine().getStatusCode() + "ERROR";
        }

    } catch (ConnectTimeoutException e) {
        result = "TIMEOUTERROR";
    }

    catch (Exception e) {
        result = "OTHERERROR";
        e.printStackTrace();

    }
    return result;
}