Search in sources :

Example 66 with ClientProtocolException

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

the class Feedback method postFeedback.

/**
     * Posting feedback or error info to the server. This is called from the AsyncTask.
     * 
     * @param url The url to post the feedback to.
     * @param type The type of the info, eg Feedback.TYPE_CRASH_STACKTRACE.
     * @param feedback For feedback types this is the message. For error/crash types this is the path to the error file.
     * @param groupId A single time generated ID, so that errors/feedback send together can be grouped together.
     * @param index The index of the error in the list
     * @return A Payload file showing success, response code and response message.
     */
public static Payload postFeedback(String url, String type, String feedback, String groupId, int index, Application app) {
    Payload result = new Payload(null);
    List<NameValuePair> pairs = null;
    if (!isErrorType(type)) {
        pairs = new ArrayList<NameValuePair>();
        pairs.add(new BasicNameValuePair("type", type));
        pairs.add(new BasicNameValuePair("groupid", groupId));
        pairs.add(new BasicNameValuePair("index", "0"));
        pairs.add(new BasicNameValuePair("message", feedback));
        addTimestamp(pairs);
    } else {
        pairs = Feedback.extractPairsFromError(type, feedback, groupId, index, app);
        if (pairs == null) {
            result.success = false;
            result.result = null;
        }
    }
    HttpClient httpClient = new DefaultHttpClient();
    HttpPost httpPost = new HttpPost(url);
    httpPost.addHeader("User-Agent", "AnkiDroid");
    try {
        httpPost.setEntity(new UrlEncodedFormEntity(pairs));
        HttpResponse response = httpClient.execute(httpPost);
        Log.e(AnkiDroidApp.TAG, String.format("Bug report posted to %s", url));
        int respCode = response.getStatusLine().getStatusCode();
        switch(respCode) {
            case 200:
                result.success = true;
                result.returnType = respCode;
                result.result = Utils.convertStreamToString(response.getEntity().getContent());
                Log.i(AnkiDroidApp.TAG, String.format("postFeedback OK: %s", result.result));
                break;
            default:
                Log.e(AnkiDroidApp.TAG, String.format("postFeedback failure: %d - %s", response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase()));
                result.success = false;
                result.returnType = respCode;
                result.result = response.getStatusLine().getReasonPhrase();
                break;
        }
    } catch (ClientProtocolException ex) {
        Log.e(AnkiDroidApp.TAG, "ClientProtocolException: " + ex.toString());
        result.success = false;
        result.result = ex.toString();
    } catch (IOException ex) {
        Log.e(AnkiDroidApp.TAG, "IOException: " + ex.toString());
        result.success = false;
        result.result = ex.toString();
    }
    return result;
}
Also used : BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) HttpPost(org.apache.http.client.methods.HttpPost) 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) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) HttpClient(org.apache.http.client.HttpClient) Payload(com.ichi2.async.Connection.Payload)

Example 67 with ClientProtocolException

use of org.apache.http.client.ClientProtocolException in project ribbon by Netflix.

the class NFHttpClient method determineTarget.

// copied from httpclient source code
private static HttpHost determineTarget(HttpUriRequest request) throws ClientProtocolException {
    // A null target may be acceptable if there is a default target.
    // Otherwise, the null target is detected in the director.
    HttpHost target = null;
    URI requestURI = request.getURI();
    if (requestURI.isAbsolute()) {
        target = URIUtils.extractHost(requestURI);
        if (target == null) {
            throw new ClientProtocolException("URI does not specify a valid host name: " + requestURI);
        }
    }
    return target;
}
Also used : HttpHost(org.apache.http.HttpHost) URI(java.net.URI) ClientProtocolException(org.apache.http.client.ClientProtocolException)

Example 68 with ClientProtocolException

use of org.apache.http.client.ClientProtocolException in project LiveSDK-for-Android by liveservices.

the class TokenRequest method execute.

/**
     * Performs the Token Request and returns the OAuth server's response.
     *
     * @return The OAuthResponse from the server
     * @throws LiveAuthException if there is any exception while executing the request
     *                           (e.g., IOException, JSONException)
     */
public OAuthResponse execute() throws LiveAuthException {
    final Uri requestUri = Config.INSTANCE.getOAuthTokenUri();
    final HttpPost request = new HttpPost(requestUri.toString());
    final List<NameValuePair> body = new ArrayList<NameValuePair>();
    body.add(new BasicNameValuePair(OAuth.CLIENT_ID, this.clientId));
    // constructBody allows subclasses to add to body
    this.constructBody(body);
    try {
        final UrlEncodedFormEntity entity = new UrlEncodedFormEntity(body, HTTP.UTF_8);
        entity.setContentType(CONTENT_TYPE);
        request.setEntity(entity);
    } catch (UnsupportedEncodingException e) {
        throw new LiveAuthException(ErrorMessages.CLIENT_ERROR, e);
    }
    final HttpResponse response;
    try {
        response = this.client.execute(request);
    } catch (ClientProtocolException e) {
        throw new LiveAuthException(ErrorMessages.SERVER_ERROR, e);
    } catch (IOException e) {
        throw new LiveAuthException(ErrorMessages.SERVER_ERROR, e);
    }
    final HttpEntity entity = response.getEntity();
    final String stringResponse;
    try {
        stringResponse = EntityUtils.toString(entity);
    } catch (IOException e) {
        throw new LiveAuthException(ErrorMessages.SERVER_ERROR, e);
    }
    final JSONObject jsonResponse;
    try {
        jsonResponse = new JSONObject(stringResponse);
    } catch (JSONException e) {
        throw new LiveAuthException(ErrorMessages.SERVER_ERROR, e);
    }
    if (OAuthErrorResponse.validOAuthErrorResponse(jsonResponse)) {
        return OAuthErrorResponse.createFromJson(jsonResponse);
    } else if (OAuthSuccessfulResponse.validOAuthSuccessfulResponse(jsonResponse)) {
        return OAuthSuccessfulResponse.createFromJson(jsonResponse);
    } else {
        throw new LiveAuthException(ErrorMessages.SERVER_ERROR);
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) HttpEntity(org.apache.http.HttpEntity) ArrayList(java.util.ArrayList) UnsupportedEncodingException(java.io.UnsupportedEncodingException) HttpResponse(org.apache.http.HttpResponse) JSONException(org.json.JSONException) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) IOException(java.io.IOException) Uri(android.net.Uri) ClientProtocolException(org.apache.http.client.ClientProtocolException) JSONObject(org.json.JSONObject) BasicNameValuePair(org.apache.http.message.BasicNameValuePair)

Example 69 with ClientProtocolException

use of org.apache.http.client.ClientProtocolException in project LiveSDK-for-Android by liveservices.

the class ApiRequest method execute.

/**
     * Performs the Http Request and returns the response from the server
     *
     * @return an instance of ResponseType from the server
     * @throws LiveOperationException if there was an error executing the HttpRequest
     */
public ResponseType execute() throws LiveOperationException {
    // Let subclass decide which type of request to instantiate
    HttpUriRequest request = this.createHttpRequest();
    request.addHeader(LIVE_LIBRARY_HEADER);
    if (this.session.willExpireInSecs(SESSION_REFRESH_BUFFER_SECS)) {
        this.session.refresh();
    }
    // risk a request with an invalid token.
    if (!this.session.willExpireInSecs(SESSION_TOKEN_SEND_BUFFER_SECS)) {
        request.addHeader(createAuthroizationHeader(this.session));
    }
    try {
        HttpResponse response = this.client.execute(request);
        for (Observer observer : this.observers) {
            observer.onComplete(response);
        }
        return this.responseHandler.handleResponse(response);
    } catch (ClientProtocolException e) {
        throw new LiveOperationException(ErrorMessages.SERVER_ERROR, e);
    } catch (IOException e) {
        // the IOException.
        try {
            new JSONObject(e.getMessage());
            throw new LiveOperationException(e.getMessage());
        } catch (JSONException jsonException) {
            throw new LiveOperationException(ErrorMessages.SERVER_ERROR, e);
        }
    }
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) JSONObject(org.json.JSONObject) HttpResponse(org.apache.http.HttpResponse) JSONException(org.json.JSONException) IOException(java.io.IOException) ClientProtocolException(org.apache.http.client.ClientProtocolException)

Example 70 with ClientProtocolException

use of org.apache.http.client.ClientProtocolException in project XobotOS by xamarin.

the class AbstractHttpClient method execute.

// non-javadoc, see interface HttpClient
public final HttpResponse execute(HttpHost target, HttpRequest request, HttpContext context) throws IOException, ClientProtocolException {
    if (request == null) {
        throw new IllegalArgumentException("Request must not be null.");
    }
    // a null target may be acceptable, this depends on the route planner
    // a null context is acceptable, default context created below
    HttpContext execContext = null;
    RequestDirector director = null;
    // all shared objects that are potentially threading unsafe.
    synchronized (this) {
        HttpContext defaultContext = createHttpContext();
        if (context == null) {
            execContext = defaultContext;
        } else {
            execContext = new DefaultedHttpContext(context, defaultContext);
        }
        // Create a director for this request
        director = createClientRequestDirector(getRequestExecutor(), getConnectionManager(), getConnectionReuseStrategy(), getConnectionKeepAliveStrategy(), getRoutePlanner(), getHttpProcessor().copy(), getHttpRequestRetryHandler(), getRedirectHandler(), getTargetAuthenticationHandler(), getProxyAuthenticationHandler(), getUserTokenHandler(), determineParams(request));
    }
    try {
        return director.execute(target, request, execContext);
    } catch (HttpException httpException) {
        throw new ClientProtocolException(httpException);
    }
}
Also used : RequestDirector(org.apache.http.client.RequestDirector) DefaultedHttpContext(org.apache.http.protocol.DefaultedHttpContext) DefaultedHttpContext(org.apache.http.protocol.DefaultedHttpContext) HttpContext(org.apache.http.protocol.HttpContext) HttpException(org.apache.http.HttpException) 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