use of org.apache.http.client.methods.HttpEntityEnclosingRequestBase in project clutchandroid by clutchio.
the class ClutchAPIClient method sendRequest.
private static void sendRequest(String url, boolean post, JSONObject payload, String version, final ClutchAPIResponseHandler responseHandler) {
BasicHeader[] headers = { new BasicHeader("X-App-Version", version), new BasicHeader("X-UDID", ClutchUtils.getUDID()), new BasicHeader("X-API-Version", VERSION), new BasicHeader("X-App-Key", appKey), new BasicHeader("X-Bundle-Version", versionName), new BasicHeader("X-Platform", "Android") };
StringEntity entity = null;
try {
entity = new StringEntity(payload.toString());
} catch (UnsupportedEncodingException e) {
Log.e(TAG, "Could not encode the JSON payload and attach it to the request: " + payload.toString());
return;
}
HttpRequestBase request = null;
if (post) {
request = new HttpPost(url);
((HttpEntityEnclosingRequestBase) request).setEntity(entity);
request.setHeaders(headers);
} else {
request = new HttpGet(url);
}
class StatusCodeAndResponse {
public int statusCode;
public String response;
public StatusCodeAndResponse(int statusCode, String response) {
this.statusCode = statusCode;
this.response = response;
}
}
new AsyncTask<HttpRequestBase, Void, StatusCodeAndResponse>() {
@Override
protected StatusCodeAndResponse doInBackground(HttpRequestBase... requests) {
try {
HttpResponse resp = client.execute(requests[0]);
HttpEntity entity = resp.getEntity();
InputStream inputStream = entity.getContent();
ByteArrayOutputStream content = new ByteArrayOutputStream();
int readBytes = 0;
byte[] sBuffer = new byte[512];
while ((readBytes = inputStream.read(sBuffer)) != -1) {
content.write(sBuffer, 0, readBytes);
}
inputStream.close();
String response = new String(content.toByteArray());
content.close();
return new StatusCodeAndResponse(resp.getStatusLine().getStatusCode(), response);
} catch (IOException e) {
if (responseHandler instanceof ClutchAPIDownloadResponseHandler) {
((ClutchAPIDownloadResponseHandler) responseHandler).onFailure(e, "");
} else {
responseHandler.onFailure(e, null);
}
}
return null;
}
@Override
protected void onPostExecute(StatusCodeAndResponse resp) {
if (responseHandler instanceof ClutchAPIDownloadResponseHandler) {
if (resp.statusCode == 200) {
((ClutchAPIDownloadResponseHandler) responseHandler).onSuccess(resp.response);
} else {
((ClutchAPIDownloadResponseHandler) responseHandler).onFailure(null, resp.response);
}
} else {
if (resp.statusCode == 200) {
responseHandler.handleSuccessMessage(resp.response);
} else {
responseHandler.handleFailureMessage(null, resp.response);
}
}
}
}.execute(request);
}
use of org.apache.http.client.methods.HttpEntityEnclosingRequestBase in project SmartAndroidSource by jaychou2012.
the class AbstractAjaxCallback method httpPut.
private void httpPut(String url, Map<String, String> headers, Map<String, Object> params, AjaxStatus status) throws ClientProtocolException, IOException {
AQUtility.debug("put", url);
HttpEntityEnclosingRequestBase req = new HttpPut(url);
httpEntity(url, req, headers, params, status);
}
use of org.apache.http.client.methods.HttpEntityEnclosingRequestBase in project SmartAndroidSource by jaychou2012.
the class AbstractAjaxCallback method httpPost.
private void httpPost(String url, Map<String, String> headers, Map<String, Object> params, AjaxStatus status) throws ClientProtocolException, IOException {
AQUtility.debug("post", url);
HttpEntityEnclosingRequestBase req = new HttpPost(url);
httpEntity(url, req, headers, params, status);
}
use of org.apache.http.client.methods.HttpEntityEnclosingRequestBase in project camel by apache.
the class RestletPostContentTest method testPostBody.
@Test
public void testPostBody() throws Exception {
HttpUriRequest method = new HttpPost("http://localhost:" + portNum + "/users/homer");
((HttpEntityEnclosingRequestBase) method).setEntity(new StringEntity(MSG_BODY));
HttpResponse response = doExecute(method);
assertHttpResponse(response, 200, "text/plain");
}
use of org.apache.http.client.methods.HttpEntityEnclosingRequestBase in project camel by apache.
the class HttpProducer method createMethod.
/**
* Creates the HttpMethod to use to call the remote server, either its GET or POST.
*
* @param exchange the exchange
* @return the created method as either GET or POST
* @throws URISyntaxException is thrown if the URI is invalid
* @throws Exception is thrown if error creating RequestEntity
*/
protected HttpRequestBase createMethod(Exchange exchange) throws Exception {
// creating the url to use takes 2-steps
String url = HttpHelper.createURL(exchange, getEndpoint());
URI uri = HttpHelper.createURI(exchange, url, getEndpoint());
// get the url from the uri
url = uri.toASCIIString();
// execute any custom url rewrite
String rewriteUrl = HttpHelper.urlRewrite(exchange, url, getEndpoint(), this);
if (rewriteUrl != null) {
// update url and query string from the rewritten url
url = rewriteUrl;
uri = new URI(url);
}
// create http holder objects for the request
HttpEntity requestEntity = createRequestEntity(exchange);
HttpMethods methodToUse = HttpMethodHelper.createMethod(exchange, getEndpoint(), requestEntity != null);
HttpRequestBase method = methodToUse.createMethod(url);
// special for HTTP DELETE if the message body should be included
if (getEndpoint().isDeleteWithBody() && "DELETE".equals(method.getMethod())) {
method = new HttpDeleteWithBodyMethod(url, requestEntity);
}
LOG.trace("Using URL: {} with method: {}", url, method);
if (methodToUse.isEntityEnclosing()) {
((HttpEntityEnclosingRequestBase) method).setEntity(requestEntity);
if (requestEntity != null && requestEntity.getContentType() == null) {
LOG.debug("No Content-Type provided for URL: {} with exchange: {}", url, exchange);
}
}
// there must be a host on the method
if (method.getURI().getScheme() == null || method.getURI().getHost() == null) {
throw new IllegalArgumentException("Invalid uri: " + uri + ". If you are forwarding/bridging http endpoints, then enable the bridgeEndpoint option on the endpoint: " + getEndpoint());
}
return method;
}
Aggregations