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

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

Introduction

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

Prototype

public String toString() 

Source Link

Document

Returns a short description of this throwable.

Usage

From source file:com.oDesk.api.oDeskRestClient.java

/**
* Execute GET request/* w  w  w.j  a va2  s. c  o m*/
* 
* @param   url Request object for GET
* @param   method HTTP method
* @param   params POST parameters
* @throws  JSONException
* @return  {@link JSONObject}
* */
private static JSONObject doGetRequest(HttpGet httpGet) throws JSONException {
    JSONObject json = null;
    HttpClient httpClient = HttpClientBuilder.create().build();
    HttpResponse response;

    try {
        response = httpClient.execute(httpGet);

        if (response.getStatusLine().getStatusCode() == 200) {
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                InputStream instream = entity.getContent();
                String result = convertStreamToString(instream);
                instream.close();

                json = new JSONObject(result);
            }
        } else {
            json = oDeskRestClient.genError(response);
        }
    } catch (ClientProtocolException e) {
        json = oDeskRestClient.genError(HTTP_RESPONSE_503, "Exception: ClientProtocolException");
    } catch (IOException e) {
        json = oDeskRestClient.genError(HTTP_RESPONSE_503, "Exception: IOException");
    } catch (JSONException e) {
        json = oDeskRestClient.genError(HTTP_RESPONSE_503, "Exception: JSONException");
    } catch (Exception e) {
        json = oDeskRestClient.genError(HTTP_RESPONSE_503, "Exception: Exception " + e.toString());
    } finally {
        httpGet.abort();
    }

    return json;
}

From source file:com.oDesk.api.oDeskRestClient.java

/**
 * Execute POST request//from  ww  w  .  j av  a2  s .com
 * 
 * @param   url Request object for POST
 * @param   method HTTP method
 * @param   params POST parameters
 * @throws  JSONException
 * @return  {@link JSONObject}
 * */
private static JSONObject doPostRequest(HttpPost httpPost, HashMap<String, String> params)
        throws JSONException {
    JSONObject json = null;
    HttpClient postClient = HttpClientBuilder.create().build();
    HttpResponse response;

    try {
        response = postClient.execute(httpPost);

        if (response.getStatusLine().getStatusCode() == 200) {
            HttpEntity entity = response.getEntity();

            if (entity != null) {
                InputStream instream = entity.getContent();
                String result = convertStreamToString(instream);
                instream.close();

                json = new JSONObject(result);
            }
        } else {
            json = oDeskRestClient.genError(response);
        }
    } catch (ClientProtocolException e) {
        json = oDeskRestClient.genError(HTTP_RESPONSE_503, "Exception: ClientProtocolException");
    } catch (IOException e) {
        json = oDeskRestClient.genError(HTTP_RESPONSE_503, "Exception: IOException");
    } catch (JSONException e) {
        json = oDeskRestClient.genError(HTTP_RESPONSE_503, "Exception: JSONException");
    } catch (Exception e) {
        json = oDeskRestClient.genError(HTTP_RESPONSE_503, "Exception: Exception " + e.toString());
    } finally {
        httpPost.abort();
    }

    return json;
}

From source file:com.Upwork.api.UpworkRestClient.java

/**
* Execute GET request// www  .jav  a2s  .c o m
* 
* @param   url Request object for GET
* @param   method HTTP method
* @param   params POST parameters
* @throws  JSONException
* @return  {@link JSONObject}
* */
private static JSONObject doGetRequest(HttpGet httpGet) throws JSONException {
    JSONObject json = null;
    HttpClient httpClient = HttpClientBuilder.create().build();
    HttpResponse response;

    try {
        response = httpClient.execute(httpGet);

        if (response.getStatusLine().getStatusCode() == 200) {
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                InputStream instream = entity.getContent();
                String result = convertStreamToString(instream);
                instream.close();

                json = new JSONObject(result);
            }
        } else {
            json = UpworkRestClient.genError(response);
        }
    } catch (ClientProtocolException e) {
        json = UpworkRestClient.genError(HTTP_RESPONSE_503, "Exception: ClientProtocolException");
    } catch (IOException e) {
        json = UpworkRestClient.genError(HTTP_RESPONSE_503, "Exception: IOException");
    } catch (JSONException e) {
        json = UpworkRestClient.genError(HTTP_RESPONSE_503, "Exception: JSONException");
    } catch (Exception e) {
        json = UpworkRestClient.genError(HTTP_RESPONSE_503, "Exception: Exception " + e.toString());
    } finally {
        httpGet.abort();
    }

    return json;
}

From source file:com.Upwork.api.UpworkRestClient.java

/**
 * Execute POST request//  w  ww  .j a v a2  s. c  o m
 * 
 * @param   url Request object for POST
 * @param   method HTTP method
 * @param   params POST parameters
 * @throws  JSONException
 * @return  {@link JSONObject}
 * */
private static JSONObject doPostRequest(HttpPost httpPost, HashMap<String, String> params)
        throws JSONException {
    JSONObject json = null;
    HttpClient postClient = HttpClientBuilder.create().build();
    HttpResponse response;

    try {
        response = postClient.execute(httpPost);

        if (response.getStatusLine().getStatusCode() == 200) {
            HttpEntity entity = response.getEntity();

            if (entity != null) {
                InputStream instream = entity.getContent();
                String result = convertStreamToString(instream);
                instream.close();

                json = new JSONObject(result);
            }
        } else {
            json = UpworkRestClient.genError(response);
        }
    } catch (ClientProtocolException e) {
        json = UpworkRestClient.genError(HTTP_RESPONSE_503, "Exception: ClientProtocolException");
    } catch (IOException e) {
        json = UpworkRestClient.genError(HTTP_RESPONSE_503, "Exception: IOException");
    } catch (JSONException e) {
        json = UpworkRestClient.genError(HTTP_RESPONSE_503, "Exception: JSONException");
    } catch (Exception e) {
        json = UpworkRestClient.genError(HTTP_RESPONSE_503, "Exception: Exception " + e.toString());
    } finally {
        httpPost.abort();
    }

    return json;
}

From source file:de.schneider.dev.poi.service.GoogleMapGeoService.java

public void getGeoCoordinate(String location) throws EncoderException {

    LOG.info("Start GeoCoordination Google Map");

    // create default HttpClient
    HttpClient httpClient = new DefaultHttpClient();

    // get data from URI
    URLCodec urlCodec = new URLCodec("UTF-8");
    HttpGet httpGet = new HttpGet(DEFAULT_JSON_URI + urlCodec.encode(location) + "&sensor=false");
    LOG.info("HttpGet: " + httpGet);

    // get response
    try {/*ww  w .j  av a 2 s  .co m*/
        HttpResponse httpResponse = httpClient.execute(httpGet);
        LOG.info("HttpResponse: " + httpResponse);
        LOG.info("Status Code: " + httpResponse.getStatusLine().getStatusCode());
        LOG.info("Status Phrase: " + httpResponse.getStatusLine().getReasonPhrase());

        HttpEntity httpEntity = httpResponse.getEntity();
        LOG.info("HttpEntity: " + httpEntity);
        LOG.info("HttpEntity Streaming: " + httpEntity.isStreaming());
        if (httpEntity.isStreaming()) {
            InputStream inputStream = httpEntity.getContent();
            String content = EntityUtils.toString(httpEntity);
            LOG.info(content);
            inputStream.close();
        }

    } catch (ClientProtocolException cpe) {
        LOG.error(cpe.toString());
    } catch (IOException ioe) {
        LOG.error(ioe.toString());
    }
}

From source file:edu.rit.csh.androidwebnews.HttpsPostAsyncTask.java

/**
 * The method that gets run when execute() is run. This sends the URL with the
 * POST parameters to the server and gets the results
 *
 * @param params - [0] is the URL to got to, the rest are parameters to the request
 * @return String representation of page results
 *//*from  ww w .  j  av a 2 s  . c om*/
@Override
protected String doInBackground(BasicNameValuePair... params) {
    ArrayList<NameValuePair> nvp = new ArrayList<NameValuePair>();
    String line;
    try {
        HttpPost request = new HttpPost(params[0].getValue());
        request.addHeader("accept", "application/json");
        //params = Arrays.copyOfRange(params, 1, params.length);
        for (BasicNameValuePair pair : params) {
            nvp.add(new BasicNameValuePair(pair.getName(), pair.getValue()));
        }

        request.setEntity(new UrlEncodedFormEntity(nvp));

        HttpResponse response = httpclient.execute(request);
        BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

        StringBuilder sb = new StringBuilder("");

        String NL = System.getProperty("line.separator");

        while ((line = in.readLine()) != null) {
            sb.append(line).append(NL);
        }
        in.close();
        String page = sb.toString();
        Log.d("jd - post result", " : " + page);
        return page;

    } catch (ClientProtocolException e) {
        Log.d("jd - post error", e.toString());
    } catch (ConnectTimeoutException e) {
        Log.d("jd - post timeout error ", e.toString());
        doInBackground(params);
    } catch (IOException e) {
        Log.d("jd - post error", e.toString());
    }
    return "";
}

From source file:com.example.shutapp.HttpMessage.java

/**
 * Run application.//  w w  w . j  a va 2  s . co  m
 */
public void run() {
    // Create a new HttpClient and Post Header
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(StringLiterals.SERVER_ADRESS);

    try {
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

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

    } catch (ClientProtocolException e) {
        String exception = e.toString();
        Log.e("Excpetion", exception);
    } catch (IOException e) {
        String exception = e.toString();
        Log.e("Excpetion", exception);
    }

}

From source file:com.arthurassuncao.cepview.net.ClienteHttp.java

public Object obterJson(Class<?> classe) {
    Object json = null;//ww w . j a  va 2s. c o m
    try {
        HttpEntity httpEntity = resposta.getEntity();

        if (httpEntity != null) {
            Gson gson = new Gson();

            BufferedReader in = new BufferedReader(new InputStreamReader(httpEntity.getContent()));
            json = gson.fromJson(in, classe);
            in.close();
            return json;
        }
    } catch (ClientProtocolException e) {
        Log.d("ClientProtocolException: ", e.toString());
    } catch (IOException e) {
        Log.d("IOException: ", e.toString());
    }
    return json;
}

From source file:com.michael.feng.utils.YahooWeather4a.WOEIDUtils.java

private String queryYahooWeather(Context context, String queryString) {
    Log.d("tag", "QueryYahooWeather");
    String qResult = "";
    //      queryString = Uri.encode(queryString);

    HttpClient httpClient = new DefaultHttpClient();

    // return Uri.encode(queryString);

    HttpGet httpGet = new HttpGet(queryString);

    try {//w  ww  .  j a v a  2  s  .co m
        HttpEntity httpEntity = httpClient.execute(httpGet).getEntity();

        if (httpEntity != null) {
            InputStream inputStream = httpEntity.getContent();
            Reader in = new InputStreamReader(inputStream);
            BufferedReader bufferedreader = new BufferedReader(in);
            StringBuilder stringBuilder = new StringBuilder();

            String stringReadLine = null;

            while ((stringReadLine = bufferedreader.readLine()) != null) {
                stringBuilder.append(stringReadLine + "\n");
            }

            qResult = stringBuilder.toString();
        }

    } catch (ClientProtocolException e) {
        e.printStackTrace();
        Toast.makeText(context, e.toString(), Toast.LENGTH_LONG).show();
    } catch (IOException e) {
        e.printStackTrace();
        Toast.makeText(context, e.toString(), Toast.LENGTH_LONG).show();
    }

    return qResult;

}

From source file:org.openmrs.module.dhisreport.web.controller.ReportDefinitionController.java

@RequestMapping(value = "/module/dhisreport/getReportDefinitions", method = RequestMethod.POST)
public String getReportDefinitions(WebRequest webRequest, HttpServletRequest request) {
    String username = Context.getAdministrationService().getGlobalProperty("dhisreport.dhis2UserName");
    String password = Context.getAdministrationService().getGlobalProperty("dhisreport.dhis2Password");
    String dhisurl = Context.getAdministrationService().getGlobalProperty("dhisreport.dhis2URL");
    String url = dhisurl + "/api/dataSets";
    //String url = "https://play.dhis2.org/demo/api/dataSets";
    String referer = webRequest.getHeader("Referer");
    HttpSession session = request.getSession();

    try {//from   www.ja v a 2s  .  c o  m
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpGet getRequest = new HttpGet(url);
        getRequest.addHeader("accept", "application/dsd+xml");
        getRequest.addHeader(
                BasicScheme.authenticate(new UsernamePasswordCredentials(username, password), "UTF-8", false));
        HttpResponse response = httpClient.execute(getRequest);

        if (response.getStatusLine().getStatusCode() != 200) {
            log.error("Failed : HTTP error code : " + response.getStatusLine().getStatusCode());
        }

        InputStream is = response.getEntity().getContent();
        try {
            DHIS2ReportingService service = Context.getService(DHIS2ReportingService.class);
            service.unMarshallandSaveReportTemplates(is);
            session.setAttribute(WebConstants.OPENMRS_MSG_ATTR,
                    Context.getMessageSourceService().getMessage("dhisreport.uploadSuccess"));
        } catch (Exception ex) {
            log.error("Error loading file: " + ex);
            session.setAttribute(WebConstants.OPENMRS_ERROR_ATTR,
                    Context.getMessageSourceService().getMessage("dhisreport.uploadError"));
        } finally {
            is.close();
        }
        httpClient.getConnectionManager().shutdown();
        return "redirect:" + referer;
    } catch (ClientProtocolException ee) {
        log.debug("An error occured in the HTTP protocol." + ee.toString());
        ee.printStackTrace();
    } catch (IOException ee) {
        log.debug("Problem accessing DHIS2 server: " + ee.toString());
        session.setAttribute(WebConstants.OPENMRS_ERROR_ATTR,
                Context.getMessageSourceService().getMessage("dhisreport.checkConnectionWithDHIS2"));
    }
    return "redirect:" + referer;
}