Search in sources :

Example 16 with ClientProtocolException

use of org.apache.http.client.ClientProtocolException in project Notes by MiCode.

the class GTaskClient method postRequest.

private JSONObject postRequest(JSONObject js) throws NetworkFailureException {
    if (!mLoggedin) {
        Log.e(TAG, "please login first");
        throw new ActionFailureException("not logged in");
    }
    HttpPost httpPost = createHttpPost();
    try {
        LinkedList<BasicNameValuePair> list = new LinkedList<BasicNameValuePair>();
        list.add(new BasicNameValuePair("r", js.toString()));
        UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, "UTF-8");
        httpPost.setEntity(entity);
        // execute the post
        HttpResponse response = mHttpClient.execute(httpPost);
        String jsString = getResponseContent(response.getEntity());
        return new JSONObject(jsString);
    } catch (ClientProtocolException e) {
        Log.e(TAG, e.toString());
        e.printStackTrace();
        throw new NetworkFailureException("postRequest failed");
    } catch (IOException e) {
        Log.e(TAG, e.toString());
        e.printStackTrace();
        throw new NetworkFailureException("postRequest failed");
    } catch (JSONException e) {
        Log.e(TAG, e.toString());
        e.printStackTrace();
        throw new ActionFailureException("unable to convert response content to jsonobject");
    } catch (Exception e) {
        Log.e(TAG, e.toString());
        e.printStackTrace();
        throw new ActionFailureException("error occurs when posting request");
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) ActionFailureException(net.micode.notes.gtask.exception.ActionFailureException) HttpResponse(org.apache.http.HttpResponse) JSONException(org.json.JSONException) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) IOException(java.io.IOException) LinkedList(java.util.LinkedList) ClientProtocolException(org.apache.http.client.ClientProtocolException) JSONException(org.json.JSONException) NetworkFailureException(net.micode.notes.gtask.exception.NetworkFailureException) IOException(java.io.IOException) ActionFailureException(net.micode.notes.gtask.exception.ActionFailureException) ClientProtocolException(org.apache.http.client.ClientProtocolException) JSONObject(org.json.JSONObject) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NetworkFailureException(net.micode.notes.gtask.exception.NetworkFailureException)

Example 17 with ClientProtocolException

use of org.apache.http.client.ClientProtocolException in project Anki-Android by Ramblurr.

the class HttpUtility method postReport.

public static Boolean postReport(String url, List<NameValuePair> values) {
    HttpClient httpClient = new DefaultHttpClient();
    HttpPost httpPost = new HttpPost(url);
    try {
        httpPost.setEntity(new UrlEncodedFormEntity(values));
        HttpResponse response = httpClient.execute(httpPost);
        switch(response.getStatusLine().getStatusCode()) {
            case 200:
                Log.e(AnkiDroidApp.TAG, String.format("feedback report posted to %s", url));
                return true;
            default:
                Log.e(AnkiDroidApp.TAG, String.format("feedback report posted to %s message", url));
                Log.e(AnkiDroidApp.TAG, String.format("%d: %s", response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase()));
                break;
        }
    } catch (ClientProtocolException ex) {
        Log.e(AnkiDroidApp.TAG, ex.toString());
    } catch (IOException ex) {
        Log.e(AnkiDroidApp.TAG, ex.toString());
    }
    return false;
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) HttpClient(org.apache.http.client.HttpClient) HttpResponse(org.apache.http.HttpResponse) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) IOException(java.io.IOException) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) ClientProtocolException(org.apache.http.client.ClientProtocolException)

Example 18 with ClientProtocolException

use of org.apache.http.client.ClientProtocolException in project UltimateAndroid by cymcsg.

the class HttpUtils_Deprecated method loadImageFromUrl.

public static byte[] loadImageFromUrl(String url) {
    InputStream i = null;
    byte[] filename = null;
    try {
        byte[] dbfilename = null;
        DefaultHttpClient httpClient = new DefaultHttpClient();
        String geturl = url;
        HttpGet request = new HttpGet(geturl);
        request.setHeader("referer", "http://pic.qingdaonews.com");
        HttpResponse response = httpClient.execute(request);
        i = response.getEntity().getContent();
        ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
        int ch;
        while ((ch = i.read()) != -1) {
            bytestream.write(ch);
        }
        filename = bytestream.toByteArray();
        bytestream.close();
        i.close();
        return filename;
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return filename;
}
Also used : HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) ClientProtocolException(org.apache.http.client.ClientProtocolException)

Example 19 with ClientProtocolException

use of org.apache.http.client.ClientProtocolException in project mobile-android by photo.

the class ApiBase method execute.

/**
     * Execute a request to the API.
     * 
     * @param request request to perform
     * @param baseUrl the base server url
     * @param consumer the oauth consumer key to sign request
     * @param listener Progress Listener with callback on progress
     * @param connectionTimeout the connection and socket timeout
     * @return the response from the API
     * @throws ClientProtocolException
     * @throws IOException
     */
public ApiResponse execute(ApiRequest request, String baseUrl, OAuthConsumer consumer, ProgressListener listener, int connectionTimeout) throws ClientProtocolException, IOException {
    // PoolingClientConnectionManager();
    HttpParams params = new BasicHttpParams();
    // set params for connection...
    HttpConnectionParams.setStaleCheckingEnabled(params, false);
    HttpConnectionParams.setConnectionTimeout(params, connectionTimeout);
    HttpConnectionParams.setSoTimeout(params, connectionTimeout);
    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
    DefaultHttpClient httpClient = new DefaultHttpClient(params);
    HttpUriRequest httpRequest = createHttpRequest(request, baseUrl, listener);
    httpRequest.getParams().setBooleanParameter("http.protocol.expect-continue", false);
    httpRequest.setHeader("User-Agent", "android");
    httpRequest.setHeader("source", "android");
    if (consumer != null) {
        try {
            consumer.sign(httpRequest);
        } catch (Exception e) {
            GuiUtils.noAlertError(TAG, "Error signing request", e);
        }
    } else {
        TrackerUtils.trackBackgroundEvent("not_signed_request", baseUrl + request.getPath());
    }
    return new ApiResponse(baseUrl + request.getPath(), httpClient.execute(httpRequest));
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) BasicHttpParams(org.apache.http.params.BasicHttpParams) HttpParams(org.apache.http.params.HttpParams) BasicHttpParams(org.apache.http.params.BasicHttpParams) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) ClientProtocolException(org.apache.http.client.ClientProtocolException) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 20 with ClientProtocolException

use of org.apache.http.client.ClientProtocolException in project tdi-studio-se by Talend.

the class WsdlTokenManager method getSOAPResponse.

private static String getSOAPResponse(URI issuerUri, String soapEnvelope) {
    HttpResponse response = null;
    // Create the request that will submit the request to the server
    try {
        HttpParams params = new BasicHttpParams();
        params.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 180000);
        HttpClient client = new SystemDefaultHttpClient(params);
        HttpPost post = new HttpPost(issuerUri);
        StringEntity entity = new StringEntity(soapEnvelope);
        post.setHeader("Content-Type", "application/soap+xml; charset=UTF-8");
        post.setEntity(entity);
        response = client.execute(post);
        return EntityUtils.toString(response.getEntity());
    } catch (ClientProtocolException e) {
        logger.error(e.getMessage());
    } catch (IOException e) {
        logger.error(e.getMessage());
    }
    return null;
}
Also used : SystemDefaultHttpClient(org.apache.http.impl.client.SystemDefaultHttpClient) HttpPost(org.apache.http.client.methods.HttpPost) StringEntity(org.apache.http.entity.StringEntity) BasicHttpParams(org.apache.http.params.BasicHttpParams) HttpParams(org.apache.http.params.HttpParams) HttpClient(org.apache.http.client.HttpClient) SystemDefaultHttpClient(org.apache.http.impl.client.SystemDefaultHttpClient) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) BasicHttpParams(org.apache.http.params.BasicHttpParams) ClientProtocolException(org.apache.http.client.ClientProtocolException)

Aggregations

ClientProtocolException (org.apache.http.client.ClientProtocolException)92 IOException (java.io.IOException)80 HttpResponse (org.apache.http.HttpResponse)49 HttpPost (org.apache.http.client.methods.HttpPost)37 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)30 HttpClient (org.apache.http.client.HttpClient)29 HttpGet (org.apache.http.client.methods.HttpGet)25 HttpEntity (org.apache.http.HttpEntity)23 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)20 UrlEncodedFormEntity (org.apache.http.client.entity.UrlEncodedFormEntity)19 InputStreamReader (java.io.InputStreamReader)18 ArrayList (java.util.ArrayList)18 UnsupportedEncodingException (java.io.UnsupportedEncodingException)17 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)14 BufferedReader (java.io.BufferedReader)13 InputStream (java.io.InputStream)13 NameValuePair (org.apache.http.NameValuePair)13 StringEntity (org.apache.http.entity.StringEntity)12 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)11 URI (java.net.URI)10