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:com.nullwire.ExceptionHandler.java

/**
 * Look into the files folder to see if there are any "*.stacktrace" files.
 * If any are present, submit them to the trace server.
 *//*from   w ww  .  j  a va 2  s.  co m*/
public static void submitStackTraces() {
    try {
        Log.d(TAG, "Looking for exceptions in: " + G.FILES_PATH);
        String[] list = searchForStackTraces();
        Log.d(TAG, "File Size: " + list.length);
        if (list != null && list.length > 0) {
            Log.d(TAG, "Found " + list.length + " stacktrace(s)");
            for (int i = 0; i < list.length; i++) {
                String filePath = G.FILES_PATH + "/" + list[i];
                // Extract the version from the filename: "packagename-version-...."
                String version = list[i].split("-")[0];
                Log.d(TAG, "Stacktrace in file '" + filePath + "' belongs to version " + version);
                // Read contents of stacktrace
                StringBuilder contents = new StringBuilder();
                BufferedReader input = new BufferedReader(new FileReader(filePath));
                String line = null;
                String androidVersion = null;
                String phoneModel = null;
                while ((line = input.readLine()) != null) {
                    if (androidVersion == null) {
                        androidVersion = line;
                        continue;
                    } else if (phoneModel == null) {
                        phoneModel = line;
                        continue;
                    }
                    contents.append(line);
                    contents.append(System.getProperty("line.separator"));
                }
                input.close();
                String stacktrace;
                stacktrace = contents.toString();
                Log.d(TAG, "Transmitting stack trace: " + stacktrace);
                // Transmit stack trace with POST request
                DefaultHttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(G.URL);
                List<NameValuePair> nvps = new ArrayList<NameValuePair>();
                nvps.add(new BasicNameValuePair("package_name", G.APP_PACKAGE));
                nvps.add(new BasicNameValuePair("package_version", version));
                nvps.add(new BasicNameValuePair("phone_model", phoneModel));
                nvps.add(new BasicNameValuePair("android_version", androidVersion));
                nvps.add(new BasicNameValuePair("stacktrace", stacktrace));
                httpPost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
                // We don't care about the response, so we just hope it went well and on with it
                httpClient.execute(httpPost);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            String[] list = searchForStackTraces();
            for (int i = 0; i < list.length; i++) {
                File file = new File(G.FILES_PATH + "/" + list[i]);
                file.delete();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

From source file:org.redpin.android.util.ExceptionReporter.java

/**
 * Look into the files folder to see if there are any "*.stacktrace" files.
 * If any are present, submit them to the trace server.
 *///  ww  w.jav  a  2 s .  c  o m
public static void submitStackTraces() {
    try {
        Log.d(TAG, "Looking for exceptions in: " + E.FILES_PATH);
        String[] list = searchForStackTraces();
        if (list != null && list.length > 0) {
            Log.d(TAG, "Found " + list.length + " stacktrace(s)");
            for (String aList : list) {
                String filePath = E.FILES_PATH + "/" + aList;
                // Extract the version from the filename:
                // "packagename-version-...."
                String version = aList.split("-")[0];
                Log.d(TAG, "Stacktrace in file '" + filePath + "' belongs to version " + version);
                // Read contents of stacktrace
                StringBuilder contents = new StringBuilder();
                BufferedReader input = new BufferedReader(new FileReader(filePath));
                String line = null;
                String androidVersion = null;
                String phoneModel = null;
                while ((line = input.readLine()) != null) {
                    if (androidVersion == null) {
                        androidVersion = line;
                        continue;
                    } else if (phoneModel == null) {
                        phoneModel = line;
                        continue;
                    }
                    contents.append(line);
                    contents.append(System.getProperty("line.separator"));
                }
                input.close();
                String stacktrace;
                stacktrace = contents.toString();
                Log.d(TAG, "Transmitting stack trace: " + stacktrace);
                // Transmit stack trace with POST request
                DefaultHttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(E.URL);
                List<NameValuePair> nvps = new ArrayList<>();
                nvps.add(new BasicNameValuePair("package_name", E.APP_PACKAGE));
                nvps.add(new BasicNameValuePair("package_version", version));
                nvps.add(new BasicNameValuePair("phone_model", phoneModel));
                nvps.add(new BasicNameValuePair("android_version", androidVersion));
                nvps.add(new BasicNameValuePair("stacktrace", stacktrace));
                httpPost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
                // We don't care about the response, so we just hope it went
                // well and on with it
                httpClient.execute(httpPost);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            String[] list = searchForStackTraces();
            for (String aList : list) {
                File file = new File(E.FILES_PATH + "/" + aList);
                file.delete();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

From source file:com.application.error.ExceptionHandler.java

/**
 * Look into the files folder to see if there are any "*.stacktrace" files.
 * If any are present, submit them to the trace server.
 *///from ww w. ja  va  2  s .c om
public static void submitStackTraces() {
    try {
        Log.d(TAG, "Looking for exceptions in: " + G.FILES_PATH);
        String[] list = searchForStackTraces();
        if (list != null && list.length > 0) {
            Log.d(TAG, "Found " + list.length + " stacktrace(s)");
            for (int i = 0; i < list.length; i++) {
                String filePath = G.FILES_PATH + "/" + list[i];
                // Extract the version from the filename: "packagename-version-...."
                String version = list[i].split("-")[0];
                Log.d(TAG, "Stacktrace in file '" + filePath + "' belongs to version " + version);
                // Read contents of stacktrace
                StringBuilder contents = new StringBuilder();
                BufferedReader input = new BufferedReader(new FileReader(filePath));
                String line = null;
                String androidVersion = null;
                String phoneModel = null;
                while ((line = input.readLine()) != null) {
                    if (androidVersion == null) {
                        androidVersion = line;
                        continue;
                    } else if (phoneModel == null) {
                        phoneModel = line;
                        continue;
                    }
                    contents.append(line);
                    contents.append(System.getProperty("line.separator"));
                }
                input.close();
                String stacktrace;
                stacktrace = contents.toString();
                Log.d(TAG, "Transmitting stack trace: " + stacktrace);
                // Transmit stack trace with POST request
                DefaultHttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(G.URL);
                List<NameValuePair> nvps = new ArrayList<NameValuePair>();
                nvps.add(new BasicNameValuePair("package_name", G.APP_PACKAGE));
                nvps.add(new BasicNameValuePair("package_version", version));
                nvps.add(new BasicNameValuePair("phone_model", phoneModel));
                nvps.add(new BasicNameValuePair("android_version", androidVersion));
                nvps.add(new BasicNameValuePair("stacktrace", stacktrace));
                httpPost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
                // We don't care about the response, so we just hope it went well and on with it
                httpClient.execute(httpPost);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            String[] list = searchForStackTraces();
            for (int i = 0; i < list.length; i++) {
                File file = new File(G.FILES_PATH + "/" + list[i]);
                file.delete();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

From source file:co.cask.cdap.internal.app.services.http.AppFabricTestBase.java

protected static HttpResponse doGet(String resource, Header[] headers) throws Exception {
    DefaultHttpClient client = new DefaultHttpClient();
    HttpGet get = new HttpGet(getEndPoint(resource));

    if (headers != null) {
        get.setHeaders(ObjectArrays.concat(AUTH_HEADER, headers));
    } else {/*from   w ww  .  j  av  a 2 s  .  com*/
        get.setHeader(AUTH_HEADER);
    }
    return client.execute(get);
}

From source file:com.l.notel.notel.org.redpin.android.util.ExceptionReporter.java

/**
 * Look into the files folder to see if there are any "*.stacktrace" files.
 * If any are present, submit them to the trace server.
 *//*from   w  ww .ja  v a  2 s. com*/
public static void submitStackTraces() {
    try {
        Log.d(TAG, "Looking for exceptions in: " + E.FILES_PATH);
        String[] list = searchForStackTraces();
        if (list != null && list.length > 0) {
            Log.d(TAG, "Found " + list.length + " stacktrace(s)");
            for (int i = 0; i < list.length; i++) {
                String filePath = E.FILES_PATH + "/" + list[i];
                // Extract the version from the filename:
                // "packagename-version-...."
                String version = list[i].split("-")[0];
                Log.d(TAG, "Stacktrace in file '" + filePath + "' belongs to version " + version);
                // Read contents of stacktrace
                StringBuilder contents = new StringBuilder();
                BufferedReader input = new BufferedReader(new FileReader(filePath));
                String line = null;
                String androidVersion = null;
                String phoneModel = null;
                while ((line = input.readLine()) != null) {
                    if (androidVersion == null) {
                        androidVersion = line;
                        continue;
                    } else if (phoneModel == null) {
                        phoneModel = line;
                        continue;
                    }
                    contents.append(line);
                    contents.append(System.getProperty("line.separator"));
                }
                input.close();
                String stacktrace;
                stacktrace = contents.toString();
                Log.d(TAG, "Transmitting stack trace: " + stacktrace);
                // Transmit stack trace with POST request
                DefaultHttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(E.URL);
                List<NameValuePair> nvps = new ArrayList<NameValuePair>();
                nvps.add(new BasicNameValuePair("package_name", E.APP_PACKAGE));
                nvps.add(new BasicNameValuePair("package_version", version));
                nvps.add(new BasicNameValuePair("phone_model", phoneModel));
                nvps.add(new BasicNameValuePair("android_version", androidVersion));
                nvps.add(new BasicNameValuePair("stacktrace", stacktrace));
                httpPost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
                // We don't care about the response, so we just hope it went
                // well and on with it
                httpClient.execute(httpPost);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            String[] list = searchForStackTraces();
            for (int i = 0; i < list.length; i++) {
                File file = new File(E.FILES_PATH + "/" + list[i]);
                file.delete();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

From source file:dk.moerks.ratebeermobile.io.NetBroker.java

public static String doPost(Context context, DefaultHttpClient httpclient, String url,
        List<NameValuePair> parameters) throws NetworkException {
    if (httpclient == null) {
        httpclient = init();/*from  w  w w.  j  av a  2 s .co  m*/
    }

    HttpPost httppost = new HttpPost(url);
    //httppost.setHeader("User-Agent", "Mozilla/5.0 (compatible; MSIE 7.0; Windows 2000)");

    for (Iterator<NameValuePair> iterator = parameters.iterator(); iterator.hasNext();) {
        NameValuePair nameValuePair = iterator.next();
        Log.d(LOGTAG, nameValuePair.getName() + " :: " + nameValuePair.getValue());
    }

    try {
        // Add your data  
        httppost.setEntity(new UrlEncodedFormEntity(parameters));

        // Execute HTTP Post Request
        Log.d(LOGTAG, "Executing Post");
        HttpResponse response = httpclient.execute(httppost);
        Log.d(LOGTAG, "Post Executed");
        Log.d(LOGTAG, "Post Response Code: " + response.getStatusLine().getStatusCode());
        String result = responseString(response);
        Log.d(LOGTAG, "Response String length: " + result.length());
        //Log.d(LOGTAG, result);
        response.getEntity().consumeContent();

        if (response.getStatusLine().getStatusCode() != 200) {
            return null;
        }

        return result;
    } catch (ClientProtocolException e) {
        throw new NetworkException(context, LOGTAG, "Network Error - Do you have a network connection?", e);
    } catch (IOException e) {
        throw new NetworkException(context, LOGTAG, "Network Error - Do you have a network connection?", e);
    } catch (Exception e) {
        throw new NetworkException(context, LOGTAG, "Network Error - Do you have a network connection?", e);
    } finally {
        httpclient.getConnectionManager().shutdown();
    }
}

From source file:org.apache.camel.component.cxf.jaxrs.CxfRsEndpointWithProperties.java

@Test
public void testCxfRsBeanWithCamelPropertiesHolder() throws Exception {
    // get the camelContext from application context
    CamelContext camelContext = ctx.getBean("camel", CamelContext.class);
    CxfRsEndpoint testEndpoint = camelContext.getEndpoint("cxfrs:bean:testEndpoint", CxfRsEndpoint.class);
    assertEquals("Got a wrong address", "http://localhost:9900/testEndpoint", testEndpoint.getAddress());
    HttpGet get = new HttpGet(testEndpoint.getAddress());
    DefaultHttpClient httpclient = new DefaultHttpClient();
    HttpResponse response = httpclient.execute(get);
    assertEquals(404, response.getStatusLine().getStatusCode());
}

From source file:test.Http.java

/**
 * post url :??jsonString???json,?json//from   w w  w  . ja  va2 s  . co  m
 * 
 * @date
 * @param url
 * @param jsonString
 * @return
 * @throws Exception
 */
public static String httpPostWithJson(String url, String jsonString) {
    String result = "";
    DefaultHttpClient httpClient = new DefaultHttpClient();
    // 
    httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 6000);
    // ?
    httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 6000);
    HttpPost httpPost = new HttpPost(url);
    // ???
    try {
        StringEntity reqEntity = new StringEntity(jsonString, HTTP.UTF_8);
        httpPost.setHeader("Content-type", "text/xml; charset=utf-8");
        // ?
        httpPost.setEntity(reqEntity);
        HttpResponse response = httpClient.execute(httpPost);// post
        HttpEntity entity = response.getEntity();
        // 
        BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent(), "UTF-8"));
        String line = null;
        result = "";
        while ((line = reader.readLine()) != null) {
            result += line;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}

From source file:com.optimusinfo.elasticpath.cortex.common.Utils.java

public static int putRequest(String deleteUrl, JSONObject requestBody, String accessToken, String contentType,
        String contentTypeString, String authorizationString, String accessTokenInitializer) {
    // Making HTTP request
    try {/*from   w  w  w  .  ja  v  a2s. co m*/
        DefaultHttpClient httpClient = new DefaultHttpClient();
        Log.i("PUT REQUEST", deleteUrl);
        HttpPut httpPut = new HttpPut(deleteUrl);
        httpPut.setHeader(contentTypeString, contentType);
        httpPut.setHeader(authorizationString, accessTokenInitializer + " " + accessToken);

        if (requestBody != null) {
            StringEntity requestEntity = new StringEntity(requestBody.toString());
            requestEntity.setContentEncoding("UTF-8");
            requestEntity.setContentType(contentType);
            httpPut.setEntity(requestEntity);
        }

        HttpResponse httpResponse = httpClient.execute(httpPut);
        return httpResponse.getStatusLine().getStatusCode();
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return 0;
}

From source file:it.restrung.rest.client.DefaultRestClientImpl.java

/**
 * Performs a DELETE request to the given url
 *
 * @param url      the url//w  w w . j a  v  a2s.c  om
 * @param user     an optional user for basic auth
 * @param password an optional password for basic auth
 * @param body     the requests body if any
 * @param timeout  the request timeout
 * @param delegate the api delegate that will receive callbacks regarding progress and results   @return the response body as a string
 * @throws APIException
 */
public static String performDelete(String url, String user, String password, JSONSerializable body, int timeout,
        APIDelegate<?> delegate) throws APIException {
    Log.d(RestClient.class.getSimpleName(), "DELETE: " + url);
    String responseBody;
    try {
        DefaultHttpClient httpclient = HttpClientFactory.getClient();
        HttpRequestBase httpDelete = body == null ? new HttpDelete(url) : new HttpDeleteWithBody(url);
        setupTimeout(timeout, httpclient);
        setupCommonHeaders(httpDelete);
        setupBasicAuth(user, password, httpDelete);
        if (body != null && HttpEntityEnclosingRequestBase.class.isAssignableFrom(httpDelete.getClass())) {
            setupRequestBody((HttpEntityEnclosingRequestBase) httpDelete, body.toJSON(), null, null);
        }
        handleRequest(httpDelete, delegate);
        HttpResponse response = httpclient.execute(httpDelete);
        responseBody = handleResponse(response, delegate);
    } catch (ClientProtocolException e) {
        throw new APIException(e, APIException.UNTRACKED_ERROR);
    } catch (IOException e) {
        throw new APIException(e, APIException.UNTRACKED_ERROR);
    }
    return responseBody;
}