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:srdes.menupp.EntreeDbAdapter.java

/**
 * @brief   Create a new review using the title and body provided. If the review is
 *          successfully created return the new rowId for that review, otherwise return
 *          a -1 to indicate failure.//from  www  .  jav a2  s .  c  om
 * 
 * @param    title the title of the note
 * 
 * @param    body the body of the note
 * 
 * @param    entree the name of the entree the review is for
 * 
 * @param    rating the rating given to the entree in the review
 * 
 * @throws    JSONException if cannot get rowID
 * 
 * @return    rowId or -1 if failed
 */
public static Long createReview(String title, String body, String entree, float rating) throws JSONException {

    //add column information to pass to database
    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    nameValuePairs.add(new BasicNameValuePair(KEY_TITLE, title));
    nameValuePairs.add(new BasicNameValuePair(KEY_BODY, body));
    nameValuePairs.add(new BasicNameValuePair(KEY_ENTREE, entree));
    nameValuePairs.add(new BasicNameValuePair(KEY_RATING, new Float(rating).toString()));

    //http post
    JSONObject response = null;
    try {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(INSERT_REVIEW_SCRIPT);
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        String responseBody = httpclient.execute(httppost, responseHandler);
        response = new JSONObject(responseBody);

    } catch (ClientProtocolException e) {
        DebugLog.LOGD("Protocol error in http connection " + e.toString());
    } catch (UnsupportedEncodingException e) {
        DebugLog.LOGD("Encoding error in http connection " + e.toString());
    } catch (IOException e) {
        DebugLog.LOGD("IO error in http connection " + e.toString());
    }
    //rowID encoded in response
    return (Long) response.get(KEY_ROWID);
}

From source file:com.ibm.mobilefirstplatform.serversdk.java.push.PushNotifications.java

protected static void executePushPostRequest(HttpPost pushPost, CloseableHttpClient httpClient,
        PushNotificationsResponseListener listener) {
    CloseableHttpResponse response = null;

    try {//from  w w w . ja va  2  s  .c  o m
        response = httpClient.execute(pushPost);

        if (listener != null) {
            sendResponseToListener(response, listener);
        }
    } catch (ClientProtocolException e) {
        logger.log(Level.SEVERE, e.toString(), e);
        if (listener != null) {
            listener.onFailure(null, null, e);
        }
    } catch (IOException e) {
        logger.log(Level.SEVERE, e.toString(), e);
        if (listener != null) {
            listener.onFailure(null, null, e);
        }
    } finally {
        if (response != null) {
            try {
                response.close();
            } catch (IOException e) {
                // Closing response is merely a best effort.
            }
        }
    }
}

From source file:com.piusvelte.sonet.core.SonetHttpClient.java

protected static byte[] httpBlobResponse(HttpClient httpClient, HttpUriRequest httpRequest) {
    if (httpClient != null) {
        HttpResponse httpResponse;//from  w  w w.  jav a 2  s. c  o  m
        HttpEntity entity = null;
        try {
            httpResponse = httpClient.execute(httpRequest);
            StatusLine statusLine = httpResponse.getStatusLine();
            entity = httpResponse.getEntity();

            switch (statusLine.getStatusCode()) {
            case 200:
            case 201:
            case 204:
                if (entity != null) {
                    return getBlob(new FlushedInputStream(entity.getContent()));
                }
                break;
            }
        } catch (ClientProtocolException e) {
            Log.e(TAG, e.toString());
            try {
                httpRequest.abort();
            } catch (UnsupportedOperationException ignore) {
                Log.e(TAG, ignore.toString());
            }
        } catch (IOException e) {
            Log.e(TAG, e.toString());
            try {
                httpRequest.abort();
            } catch (UnsupportedOperationException ignore) {
                Log.e(TAG, ignore.toString());
            }
        } finally {
            if (entity != null) {
                try {
                    entity.consumeContent();
                } catch (IOException e) {
                    Log.e(TAG, e.toString());
                }
            }
        }
    }
    return null;
}

From source file:com.piusvelte.sonet.core.SonetHttpClient.java

protected static String httpResponse(HttpClient httpClient, HttpUriRequest httpRequest) {
    String response = null;//  w ww  . ja  v a 2s  . c om
    if (httpClient != null) {
        HttpResponse httpResponse;
        HttpEntity entity = null;
        try {
            httpResponse = httpClient.execute(httpRequest);
            StatusLine statusLine = httpResponse.getStatusLine();
            entity = httpResponse.getEntity();

            switch (statusLine.getStatusCode()) {
            case 200:
            case 201:
            case 204:
                if (entity != null) {
                    InputStream is = entity.getContent();
                    ByteArrayOutputStream content = new ByteArrayOutputStream();
                    byte[] sBuffer = new byte[512];
                    int readBytes = 0;
                    while ((readBytes = is.read(sBuffer)) != -1) {
                        content.write(sBuffer, 0, readBytes);
                    }
                    response = new String(content.toByteArray());
                } else {
                    response = "OK";
                }
                break;
            default:
                Log.e(TAG, httpRequest.getURI().toString());
                Log.e(TAG, "" + statusLine.getStatusCode() + " " + statusLine.getReasonPhrase());
                if (entity != null) {
                    InputStream is = entity.getContent();
                    ByteArrayOutputStream content = new ByteArrayOutputStream();
                    byte[] sBuffer = new byte[512];
                    int readBytes = 0;
                    while ((readBytes = is.read(sBuffer)) != -1) {
                        content.write(sBuffer, 0, readBytes);
                    }
                    Log.e(TAG, "response:" + new String(content.toByteArray()));
                }
                break;
            }
        } catch (ClientProtocolException e) {
            Log.e(TAG, e.toString());
            try {
                httpRequest.abort();
            } catch (UnsupportedOperationException ignore) {
                Log.e(TAG, ignore.toString());
            }
        } catch (IllegalStateException e) {
            Log.e(TAG, e.toString());
            try {
                httpRequest.abort();
            } catch (UnsupportedOperationException ignore) {
                Log.e(TAG, ignore.toString());
            }
        } catch (IOException e) {
            Log.e(TAG, e.toString());
            try {
                httpRequest.abort();
            } catch (UnsupportedOperationException ignore) {
                Log.e(TAG, ignore.toString());
            }
        } finally {
            if (entity != null) {
                try {
                    entity.consumeContent();
                } catch (IOException e) {
                    Log.e(TAG, e.toString());
                }
            }
        }
    }
    return response;
}

From source file:com.groupme.sdk.util.HttpUtils.java

public static InputStream openStream(HttpClient client, String url, int method, String body, Bundle params,
        Bundle headers) throws HttpResponseException {
    HttpResponse response;/*from w  w  w.j  av  a 2s  .c  om*/
    InputStream in = null;

    Log.v("HttpUtils", "URL = " + url);

    try {
        switch (method) {
        case METHOD_GET:
            url = url + "?" + encodeParams(params);
            HttpGet get = new HttpGet(url);

            if (headers != null && !headers.isEmpty()) {
                for (String header : headers.keySet()) {
                    get.setHeader(header, headers.getString(header));
                }
            }

            response = client.execute(get);
            break;
        case METHOD_POST:
            if (body != null) {
                url = url + "?" + encodeParams(params);
            }

            HttpPost post = new HttpPost(url);
            Log.d(Constants.LOG_TAG, "URL: " + url);

            if (headers != null && !headers.isEmpty()) {
                for (String header : headers.keySet()) {
                    post.setHeader(header, headers.getString(header));
                }
            }

            if (body == null) {
                List<NameValuePair> pairs = bundleToList(params);
                post.setEntity(new UrlEncodedFormEntity(pairs, HTTP.UTF_8));
            } else {
                post.setEntity(new StringEntity(body));
            }

            response = client.execute(post);
            break;
        case METHOD_PUT:
            if (body != null) {
                url = url + "?" + encodeParams(params);
            }

            HttpPut put = new HttpPut(url);

            if (headers != null && !headers.isEmpty()) {
                for (String header : headers.keySet()) {
                    put.setHeader(header, headers.getString(header));
                }
            }

            if (body == null) {
                List<NameValuePair> pairs = bundleToList(params);
                put.setEntity(new UrlEncodedFormEntity(pairs, HTTP.UTF_8));
            } else {
                put.setEntity(new StringEntity(body));
            }

            response = client.execute(put);
            break;
        default:
            throw new UnsupportedOperationException("Cannot execute HTTP method: " + method);
        }

        int statusCode = response.getStatusLine().getStatusCode();

        if (statusCode > 400) {
            throw new HttpResponseException(statusCode, read(response.getEntity().getContent()));
        }

        in = response.getEntity().getContent();
    } catch (ClientProtocolException e) {
        Log.e(Constants.LOG_TAG, "Client error: " + e.toString());
    } catch (IOException e) {
        Log.e(Constants.LOG_TAG, "IO error: " + e.toString());
    }

    return in;
}

From source file:info.unyttig.helladroid.newzbin.NewzBinController.java

/**
 * Finds reports based on the paramaters given in searchOptions
 * //ww  w  .j  a  v a 2s  . c  o m
 * @param searchOptions
 * @return ArrayList<NewzBinReport> - list of result reports.
 */
public static ArrayList<NewzBinReport> findReport(final Handler messageHandler,
        final HashMap<String, String> searchOptions) {
    String url = NBAPIURL + "reportfind/";
    ArrayList<NewzBinReport> searchRes = new ArrayList<NewzBinReport>();
    try {
        HttpResponse response = doPost(url, searchOptions);
        checkReturnCode(response.getStatusLine().getStatusCode(), false);

        InputStream is = response.getEntity().getContent();
        BufferedInputStream bis = new BufferedInputStream(is);
        ByteArrayBuffer baf = new ByteArrayBuffer(20);

        int current = 0;
        while ((current = bis.read()) != -1) {
            baf.append((byte) current);
        }
        /* Convert the Bytes read to a String. */
        String text = new String(baf.toByteArray());
        //         Log.d(LOG_NAME, text);

        BufferedReader reader = new BufferedReader(new StringReader(text));
        String str = reader.readLine();
        totalRes = Integer.parseInt(str.substring(str.indexOf("=") + 1));
        while ((str = reader.readLine()) != null) {
            String[] values = str.split("   ");
            NewzBinReport temp2 = new NewzBinReport();
            temp2.setNzbId(Integer.parseInt(values[0]));
            temp2.setSize(Long.parseLong(values[1]));
            temp2.setTitle(values[2]);

            if (!reports.containsKey(temp2.getNzbId())) {
                reports.put(temp2.getNzbId(), temp2);
                searchRes.add(temp2);
            } else
                searchRes.add(reports.get(temp2.getNzbId()));
        }

        Object[] result = new Object[2];
        result[0] = totalRes;
        result[1] = searchRes;
        return searchRes;

        // TODO message handling
    } catch (ClientProtocolException e) {
        Log.e(LOG_NAME, "ClientProtocol thrown: ", e);
        sendUserMsg(messageHandler, e.toString());
    } catch (IOException e) {
        Log.e(LOG_NAME, "IOException thrown: ", e);
        sendUserMsg(messageHandler, e.toString());
    } catch (NewzBinPostReturnCodeException e) {
        Log.e(LOG_NAME, "POST ReturnCode error: " + e.toString());
        sendUserMsg(messageHandler, e.getMessage());
    }
    return searchRes;
}

From source file:org.wso2.cdm.agent.proxy.ServerApiAccess.java

public static Map<String, String> postDataAPI(APIUtilities apiUtilities, Map<String, String> headers) {
    String httpMethod = apiUtilities.getHttpMethod();
    String url = apiUtilities.getEndPoint();
    Map<String, String> params = apiUtilities.getRequestParamsMap();

    Map<String, String> response_params = new HashMap<String, String>();
    HttpClient httpclient = getCertifiedHttpClient();
    String payload = buildPayload(params);

    if (httpMethod.equals("POST")) {
        HttpPost httpPost = new HttpPost(url);
        Log.e("url", "" + url);
        HttpPost httpPostWithHeaders = (HttpPost) buildHeaders(httpPost, headers, httpMethod);
        byte[] postData = payload.getBytes();
        try {// w  w  w.j  a v a2 s .c  om
            httpPostWithHeaders.setEntity(new ByteArrayEntity(postData));
            HttpResponse response = httpclient.execute(httpPostWithHeaders);
            String status = String.valueOf(response.getStatusLine().getStatusCode());
            Log.d(TAG, status);
            response_params.put("response", getResponseBody(response));
            response_params.put("status", status);
            return response_params;
        } catch (ClientProtocolException e) {
            Log.d(TAG, "ClientProtocolException :" + e.toString());
            return null;
        } catch (IOException e) {
            Log.d(TAG, e.toString());
            response_params.put("response", "Internal Server Error");
            response_params.put("status", "500");
            return response_params;
        }
    }
    return null;
}

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

private static void updatePreferences(JSONObject user, Context ctx, String fbid) {
    DefaultHttpClient client = new MyHttpClient(ctx);
    HttpPost post = new HttpPost("https://54.194.14.115:443/upload");
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
    nameValuePairs.add(new BasicNameValuePair("user", user.toString()));
    try {//from www.  j  av a 2 s.  c  om
        post.setHeader("Content-type", "application/json");
        post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    } catch (UnsupportedEncodingException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    HttpResponse postResponse;
    try {
        postResponse = client.execute(post);
        HttpEntity responseEntity = postResponse.getEntity();
        InputStream is = responseEntity.getContent();
    } 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", "FIN THREAD");
}

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

private static void post2(String endpoint, final String msg, final String id, final Context ctx) {
    DefaultHttpClient client = new MyHttpClient(ctx);
    HttpPost post = new HttpPost("https://54.194.14.115:443/send");
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
    nameValuePairs.add(new BasicNameValuePair("receiver", id));
    nameValuePairs.add(new BasicNameValuePair("sender", Common.getFBID()));
    nameValuePairs.add(new BasicNameValuePair("data", msg));
    try {/*from   w  ww  .j a v  a  2s  .com*/
        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;
    s = "s No inicialitzada";
    try {
        postResponse = client.execute(post);
        HttpEntity responseEntity = postResponse.getEntity();
        InputStream is = responseEntity.getContent();
        s = convertStreamToString(is);

    } 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", "FIN THREAD");
    //return s;
}

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

/**
 * Register this account/device pair within the server.
 *//* w w w .  j  a  va  2  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);

}