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

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

Introduction

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

Prototype

public CloseableHttpResponse execute(final HttpUriRequest request) throws IOException, ClientProtocolException 

Source Link

Usage

From source file:models.ApiConnector.java

public JSONArray GetAllTeas() {
    String url = "http://chuangmi.my-place.us/getAllTeas.php";

    /*//from   ww  w  .  j ava  2 s  .c om
     * Get HttpResponse object from the url
     * Get HttpEntity from Http Response object
     */

    HttpEntity httpEntity = null;

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

        HttpResponse httpResponse = httpClient.execute(httpGet);

        httpEntity = httpResponse.getEntity();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    //convert HttpEntity into JSON array
    JSONArray jsonArray = null;

    if (httpEntity != null) {
        try {
            String entityResponse = EntityUtils.toString(httpEntity);
            Log.e("Entity Response : ", entityResponse);
            jsonArray = new JSONArray(entityResponse);
        } catch (JSONException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    return jsonArray;
}

From source file:models.ApiConnectorPromotions.java

public JSONArray GetAllTeas() {
    String url = "http://chuangmi.my-place.us/getAllPromotionInfo.php";

    /*// w  ww  . j a v a2 s.  c  om
     * Get HttpResponse object from the url
     * Get HttpEntity from Http Response object
     */

    HttpEntity httpEntity = null;

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

        HttpResponse httpResponse = httpClient.execute(httpGet);

        httpEntity = httpResponse.getEntity();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    //convert HttpEntity into JSON array
    JSONArray jsonArray = null;

    if (httpEntity != null) {
        try {
            String entityResponse = EntityUtils.toString(httpEntity);
            Log.e("Entity Response : ", entityResponse);
            jsonArray = new JSONArray(entityResponse);
        } catch (JSONException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    return jsonArray;
}

From source file:org.jboss.as.test.integration.web.security.servlet.methods.DenyUncoveredHttpMethodsTestCase.java

private HttpResponse getHttpResponse(HttpUriRequest request) throws IOException {
    DefaultHttpClient httpClient = new DefaultHttpClient();
    HttpResponse response = httpClient.execute(request);
    return response;
}

From source file:se.vgregion.pubsub.inttest.Subscriber.java

public void subscribe(SubscriptionMode mode, URI hub, URI topic) throws URISyntaxException, IOException {
    HttpPost post = new HttpPost(hub);

    List<NameValuePair> parameters = new ArrayList<NameValuePair>();
    parameters/*  www . j a  va2s  .c om*/
            .add(new BasicNameValuePair("hub.callback", buildTestUrl(localServerName, server, "/").toString()));
    if (mode == SubscriptionMode.SUBSCRIBE) {
        parameters.add(new BasicNameValuePair("hub.mode", "subscribe"));
    } else {
        parameters.add(new BasicNameValuePair("hub.mode", "unsubscribe"));
    }
    parameters.add(new BasicNameValuePair("hub.topic", topic.toString()));
    parameters.add(new BasicNameValuePair("hub.verify", "sync"));

    post.setEntity(new UrlEncodedFormEntity(parameters));
    DefaultHttpClient client = new DefaultHttpClient();
    HttpResponse response = client.execute(post);

    Assert.assertEquals(204, response.getStatusLine().getStatusCode());
}

From source file:ca.ualberta.cs.c301f12t01.serverStorage.Server.java

public void get(String args) {
    DefaultHttpClient httpclient = new DefaultHttpClient();
    HttpGet httpGet = new HttpGet(serverName + args);
    try {//from  w w w .j a  v  a 2s  .c  o  m
        HttpResponse response1 = httpclient.execute(httpGet);
        // System.out.println(response1.getStatusLine());
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:com.pti.mates.ServerUtilities.java

/**
 * Register this account/device pair within the server.
 *//*from ww w .  ja v a2  s .  c  o m*/
public static void register(String fbid, String regId, Context ctx) {
    Utils utils = new Utils(ctx);
    DefaultHttpClient client = new MyHttpClient(ctx);
    HttpPost post = new HttpPost("https://54.194.14.115:443/register");
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
    nameValuePairs.add(new BasicNameValuePair("matesid", fbid));
    Log.d("REGISTERBACK", "matesId = " + fbid);
    nameValuePairs.add(new BasicNameValuePair("gcmid", regId));

    Log.d("regIdBACK", regId);
    try {
        post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    } catch (UnsupportedEncodingException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    // Execute the Post call and obtain the response
    HttpResponse postResponse;
    try {
        Log.d("REGISTERBACK", "ENTRO TRY");
        postResponse = client.execute(post);
        HttpEntity responseEntity = postResponse.getEntity();
        InputStream is = responseEntity.getContent();
        s = utils.convertStreamToString(is);
        Log.d("S REGISTER", s);

    } catch (ClientProtocolException e) {
        Log.e("ERROR", e.toString());
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
        Log.e("ERROR", e.toString());
        s = "ERROR: " + e.toString() + " :(";
    }
    Log.d("CHIVATO registe", "FIN THREAD");

    //doRegister = new DoRegister(fbid,regId,ctx);
    //doRegister.execute();
    Intent intent = new Intent(ctx, LogOk.class);
    ctx.startActivity(intent);

}

From source file:de.hhn.android.licenseplatedecoder.activities.OSMActivity.java

public JSONArray searchLocation(String query) {
    JSONArray results = new JSONArray();

    try {//  w ww.  ja va2 s. c  o m
        query = URLEncoder.encode(query, "UTF-8");
    } catch (UnsupportedEncodingException e) {
        return results;
    }
    String url = "http://nominatim.openstreetmap.org/search?";
    url += "q=" + query;
    url += "&format=json";

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

    try {
        HttpResponse response = httpClient.execute(httpGet);
        String content = EntityUtils.toString(response.getEntity(), "utf-8");
        results = new JSONArray(content);

    } catch (Exception e) {
        Log.e("searchLocation", "Error executing url: " + url + "; " + e.getMessage());
    }

    return results;
}

From source file:com.fibon.maven.confluence.ExportPageConfluenceMojo.java

private void downloadFile(HttpGet request) throws MojoFailureException {
    InputStream in = null;/*from w ww. j a  v  a  2s  .  c o  m*/
    FileOutputStream out = null;
    DefaultHttpClient httpClient = new DefaultHttpClient();
    try {
        HttpResponse response = httpClient.execute(request);
        if (response == null || response.getEntity() == null) {
            getLog().warn("Nothing to save - empty response");
        } else {
            in = response.getEntity().getContent();
            out = new FileOutputStream(outputFile);
            IOUtils.copy(in, out);
        }
    } catch (Exception e) {
        throw fail("Unable to download page", e);
    } finally {
        IOUtils.closeQuietly(in);
        IOUtils.closeQuietly(out);
    }
}

From source file:org.openihs.seendroid.lib.Connection.java

/**
 * /*from   w w  w  .j  a  va 2s . co  m*/
 * @param message
 * @return
 * @throws ClientProtocolException
 * @throws IOException
 */
public HttpResponse query(HttpRequestBase message)
        throws ClientProtocolException, IOException, UnknownHostException {
    // SSL fixes (javax.net.ssl.SSLPeerUnverifiedException: No peer certificate)
    // From http://www.virtualzone.de/2011-02-27/how-to-use-apache-httpclient-with-httpsssl-on-android/
    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    schemeRegistry.register(new Scheme("https", new EasySSLSocketFactory(), 443));

    HttpParams params = new BasicHttpParams();
    params.setParameter(ConnManagerPNames.MAX_TOTAL_CONNECTIONS, 30);
    params.setParameter(ConnManagerPNames.MAX_CONNECTIONS_PER_ROUTE, new ConnPerRouteBean(30));
    params.setParameter(HttpProtocolParams.USE_EXPECT_CONTINUE, false);
    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);

    ClientConnectionManager cm = new SingleClientConnManager(params, schemeRegistry);

    // Real code:
    DefaultHttpClient client = new DefaultHttpClient(cm, params);

    HttpResponse response = client.execute(message);
    return response;
}

From source file:com.thoughtmetric.tl.TLLib.java

public static void sendPM(String to, String subject, String message) throws IOException {
    DefaultHttpClient httpclient = new DefaultHttpClient();
    httpclient.setCookieStore(cookieStore);
    HttpPost httpost = new HttpPost(PM_URL);

    List<NameValuePair> nvps = new ArrayList<NameValuePair>();
    nvps.add(new BasicNameValuePair("to", to));
    nvps.add(new BasicNameValuePair("subject", subject));
    nvps.add(new BasicNameValuePair("body", message));
    nvps.add(new BasicNameValuePair("view", "Send"));
    Log.d(TAG, "Sending message");
    Log.d(TAG, to);//from w  w w  .  ja va 2  s  .  c o m
    Log.d(TAG, subject);
    Log.d(TAG, message);

    try {
        httpost.setEntity(new UrlEncodedFormEntity(nvps));
        HttpResponse response = httpclient.execute(httpost);
        HttpEntity entity = response.getEntity();

        if (entity != null) {
            entity.consumeContent();
        }

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