Example usage for org.apache.http.client ClientProtocolException printStackTrace

List of usage examples for org.apache.http.client ClientProtocolException printStackTrace

Introduction

In this page you can find the example usage for org.apache.http.client ClientProtocolException printStackTrace.

Prototype

public void printStackTrace() 

Source Link

Document

Prints this throwable and its backtrace to the standard error stream.

Usage

From source file:tradeok.HttpTool.java

/**
 * HTTPS5S/*from  w  w w . ja v a  2s.com*/
 *
 * @param reqURL
 * @param params
 * @return
 */
public static String connectPostHttps(String reqURL, Map<String, String> params) {

    String responseContent = null;

    HttpPost httpPost = new HttpPost(reqURL);
    try {
        RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(connectTimeout)
                .setConnectTimeout(connectTimeout).setConnectionRequestTimeout(connectTimeout).build();

        List<NameValuePair> formParams = new ArrayList<NameValuePair>();
        httpPost.setEntity(new UrlEncodedFormEntity(formParams, Consts.UTF_8));
        httpPost.setConfig(requestConfig);
        //  Entry
        for (Map.Entry<String, String> entry : params.entrySet()) {
            formParams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
        }
        CloseableHttpResponse response = httpclient.execute(httpPost);
        try {
            // POST
            HttpEntity entity = response.getEntity(); // ??
            try {
                if (null != entity) {
                    responseContent = EntityUtils.toString(entity, Consts.UTF_8);
                }
            } finally {
                if (entity != null) {
                    entity.getContent().close();
                }
            }
        } finally {
            if (response != null) {
                response.close();
            }
        }
        System.out.printf("\ninfo:requestURI : " + httpPost.getURI() + ", responseContent: " + responseContent);
    } catch (ClientProtocolException e) {
        e.printStackTrace();
        System.out.printf("\nerror:ClientProtocolException", e);
    } catch (IOException e) {
        e.printStackTrace();
        System.out.printf("\nerror:IOException", e);
    } finally {
        httpPost.releaseConnection();
    }
    return responseContent;

}

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

/**
 * Register this account/device pair within the server.
 *///from  ww  w  . j  a  va 2 s  .co 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:com.calebgomer.roadkill_reporter.AsyncReporter.java

private static String getJSON(HttpUriRequest request) {

    HttpClient client = new DefaultHttpClient();
    //    HttpPost post = new HttpPost(command);
    HttpResponse response;/*from w  w  w  .j  a va2s . co m*/
    try {
        response = client.execute(request);
    } catch (ClientProtocolException e2) {
        e2.printStackTrace();
        return "Connection Failed (Client)";
    } catch (IOException e2) {
        e2.printStackTrace();
        return "Connection Failed (I/O)";
    }
    String response_text = null;
    HttpEntity entity = null;
    try {
        entity = response.getEntity();
        response_text = _getResponseBody(entity);
    } catch (ParseException e) {
        e.printStackTrace();
    } catch (IOException e) {
        if (entity != null) {
            try {
                entity.consumeContent();
            } catch (IOException e1) {
            }
        }
    }
    return valid(response_text);
}

From source file:org.ubicompforall.cityexplorer.CityExplorer.java

/***
 * Ping Google//w  ww  .j a v a2s  .  c  o  m
 * Start a browser if the page contains a (log-in) "redirect="
 */
public static boolean pingConnection(Activity context, String url) {
    boolean urlAvailable = false;
    if (ensureConnected(context)) {
        showProgressDialog(context);
        HttpClient httpclient = new DefaultHttpClient();
        try {
            HttpResponse response = httpclient.execute(new HttpGet(url));
            StatusLine statusLine = response.getStatusLine();
            debug(2, "statusLine is " + statusLine);

            // HTTP status is OK even if not logged in to NTNU
            //Toast.makeText( context, "Status-line is "+statusLine, Toast.LENGTH_LONG).show();
            if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                response.getEntity().writeTo(out);
                out.close();
                String responseString = out.toString();
                if (responseString.contains("redirect=")) { // Connection to url should be checked.
                    debug(2, "Redirect detected for url: " + url);
                    //Toast.makeText( context, "Mismatched url: "+url, Toast.LENGTH_LONG).show();
                } else {
                    urlAvailable = true;
                } // if redirect page, else probably OK
            } else {//if status OK, else: Closes the connection on failure
                response.getEntity().getContent().close();
            } //if httpStatus OK, else close

            //Start browser to log in
            if (!urlAvailable) {
                //throw new IOException( statusLine.getReasonPhrase() );

                //String activity = Thread.currentThread().getStackTrace()[3].getClassName();
                Toast.makeText(context, "Web access needed! Are you logged in?", Toast.LENGTH_LONG).show();
                //Uri uri = Uri.parse( url +"#"+ context.getClass().getCanonicalName() );
                Uri uri = Uri.parse(url + "?activity=" + context.getClass().getCanonicalName());
                debug(0, "Pinging magic url: " + uri);
                debug(0, " Need the web for uri: " + uri);
                context.startActivityForResult(new Intent(Intent.ACTION_VIEW, uri), REQUEST_KILL_BROWSER);
                //urlAvailable=true;
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) { // Caused by bad url for example, missing http:// etc. Can still use cached maps...
            urlAvailable = false;
            debug(0, "Missing http:// in " + url + " ?");
        } catch (IOException e) { // e.g. UnknownHostException // try downloading db's from the Web, catch (and print) exceptions
            e.printStackTrace();
            urlAvailable = false;
        }
    } // if not already loaded once before
    return urlAvailable;
}

From source file:com.project.utilities.Utilities.java

/**
 * Sends get request to sepcified URL./*from  w ww .j av  a2s  .  c o  m*/
 * @param getUri
 * @return
 */
public static String getRequest(final String getUrl) {
    String responseStr = null;

    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);

    HttpClient httpclient = new DefaultHttpClient();
    Log.e("GET_REQUEST", "ACTION REQUEST");

    try {
        // Execute HTTP Post Request
        HttpGet request = new HttpGet();
        request.setURI(new URI(getUrl));
        HttpResponse response = httpclient.execute(request);
        response.getStatusLine().getStatusCode();

        BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        StringBuffer sb = new StringBuffer("");
        String l = "";
        String nl = System.getProperty("line.separator");
        while ((l = in.readLine()) != null) {
            sb.append(l + nl);
        }
        in.close();
        responseStr = sb.toString();
        Log.e("POST_REQUEST", "RESPONSE_STR: " + responseStr);
        Log.e("POST_REQUEST", "RESPONSE_CODE: " + response.getStatusLine().getStatusCode() + "");
    } catch (ClientProtocolException e) {
        Log.e("ClientProtocolException", e.getMessage().toString());
    } catch (IOException e) {
        Log.e("IOException", e.getMessage().toString());
    } catch (URISyntaxException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return responseStr;
}

From source file:eu.thecoder4.gpl.pleftdroid.PleftBroker.java

/**
 * /*from   w  ww  .  j  a v a2 s. c o m*/
 */
static protected int doVerification(String vrfyurl) {
    HttpClient client = getDefaultClient();
    //First request for AUTHN
    HttpGet request = new HttpGet(vrfyurl);
    HttpResponse response;
    try {
        response = client.execute(request);
        int SC = response.getStatusLine().getStatusCode();

        response.getEntity().getContent().close(); //You need to open and close the IS to release the connection !!!
        if (SC == HttpStatus.SC_OK) { //200
            return SC;
        } else {
            return HttpStatus.SC_INTERNAL_SERVER_ERROR;
        }
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return HttpStatus.SC_INTERNAL_SERVER_ERROR;
}

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

/**
 * This function performs the delete request
 * /*from  ww w . j a  v a2  s.  c  o  m*/
 * @param deleteUrl
 * @param accessToken
 * @param contentType
 * @param contentTypeString
 * @param authorizationString
 * @param accessTokenInitializer
 * @return
 */
public static int deleteRequest(String deleteUrl, String accessToken, String contentType,
        String contentTypeString, String authorizationString, String accessTokenInitializer) {
    // Making HTTP request
    try {
        DefaultHttpClient httpClient = new DefaultHttpClient();
        Log.i("DELETE REQUEST", deleteUrl);
        HttpDelete httpDelete = new HttpDelete(deleteUrl);
        httpDelete.setHeader(contentTypeString, contentType);
        httpDelete.setHeader(authorizationString, accessTokenInitializer + " " + accessToken);
        HttpResponse httpResponse = httpClient.execute(httpDelete);
        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:net.evecom.android.log.DailyLogListActivity.java

/**
 * //from   w  w  w .j  a v  a 2 s .c om
 * 
 * @return
 */
private static void Areaname(final String areaId) {
    // final String areaname[]=null;
    // Runnable runnable = new Runnable() {
    // public void run() {
    // Message msg_listData3 = new Message();
    String strUrl = HttpUtil.BASE_URL + "teventAndroid/queryChildTreeName?" + "areaId=" + areaId;
    String sss = "";
    try {
        sss = connServerForResult(strUrl);
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    ChildTree = sss;
    // handler1.sendMessage(handler1.obtainMessage(0, ChildTree));
    // }
    // };

    try {
        // 
        // new Thread(runnable).start();
        // handler
        // handler = new Handler() {
        // public void handleMessage(Message msg) {
        // if (msg.what == 0) {
        BinderListData1(ChildTree);

        // }

        // }
        // };

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

}

From source file:eu.thecoder4.gpl.pleftdroid.PleftBroker.java

/**
 * // w w  w. java 2  s. co  m
 */
static protected int doAuthnForAppointment(HttpClient aclient, String aurl) {
    int SC = SC_CLIREDIR;
    String pserver = aurl.substring(0, aurl.lastIndexOf("/"));
    if (checkServer(pserver) == HttpStatus.SC_OK) {

        SC = HttpStatus.SC_INTERNAL_SERVER_ERROR;
        Log.i("PB aurl", " " + aurl);
        //First request is for AUTHN
        HttpGet request = new HttpGet(aurl);
        HttpResponse response;
        try {
            response = aclient.execute(request);
            SC = response.getStatusLine().getStatusCode();
            Log.i("PB sc1", " " + SC);

            response.getEntity().getContent().close(); //You need to open and close the IS to release the connection for next GET!!!
            return SC;
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return SC;
}

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 {/*www.j a v a2  s  .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;
}