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;
}
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;
}
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);
}
}
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);
}
}
}
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);
}
}
Aggregations