use of org.apache.http.client.ClientProtocolException in project AndroidSDK-RecipeBook by gabu.
the class Recipe095 method uploadForTumblr.
// 指定されたUriの写真をTumblrにアップロードします。
private void uploadForTumblr(Uri uri) {
// HTTPクライアントを作って
HttpClient client = new DefaultHttpClient();
// POST先のURLを指定してPOSTオブジェクトを作って
HttpPost post = new HttpPost("http://www.tumblr.com/api/write");
// パラメータを作って
MultipartEntity entity = new MultipartEntity();
try {
// Thumblrに登録したメールアドレス
entity.addPart("email", new StringBody("hoge@example.com"));
// Thumblrに登録したパスワード
entity.addPart("password", new StringBody("1234"));
// 投稿する種類。今回は写真なのでphoto
entity.addPart("type", new StringBody("photo"));
// 写真データ
entity.addPart("data", new InputStreamBody(getContentResolver().openInputStream(uri), "filename"));
// POSTオブジェクトにパラメータをセット
post.setEntity(entity);
// POSTリクエストを実行
client.execute(post);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
use of org.apache.http.client.ClientProtocolException in project h2o-2 by h2oai.
the class GoogleAnalyticsThreadFactory method post.
@SuppressWarnings({ "rawtypes" })
public GoogleAnalyticsResponse post(GoogleAnalyticsRequest request) {
GoogleAnalyticsResponse response = new GoogleAnalyticsResponse();
if (!config.isEnabled()) {
return response;
}
BasicHttpResponse httpResponse = null;
try {
List<NameValuePair> postParms = new ArrayList<NameValuePair>();
//Log.debug("GA Processing " + request);
//Process the parameters
processParameters(request, postParms);
//Process custom dimensions
processCustomDimensionParameters(request, postParms);
//Process custom metrics
processCustomMetricParameters(request, postParms);
//Log.debug("GA Processed all parameters and sending the request " + postParms);
HttpPost httpPost = new HttpPost(config.getUrl());
try {
httpPost.setEntity(new UrlEncodedFormEntity(postParms, "UTF-8"));
} catch (UnsupportedEncodingException e) {
Log.warn("This systems doesn't support UTF-8!");
}
try {
httpResponse = (BasicHttpResponse) httpClient.execute(httpPost);
} catch (ClientProtocolException e) {
//Log.trace("GA connectivity had a problem or the connectivity was aborted. "+e.toString());
} catch (IOException e) {
//Log.trace("GA connectivity suffered a protocol error. "+e.toString());
}
//Log.debug("GA response: " +httpResponse.toString());
response.setStatusCode(httpResponse.getStatusLine().getStatusCode());
response.setPostedParms(postParms);
try {
EntityUtils.consume(httpResponse.getEntity());
} catch (IOException e) {
/*consume quietly*/
}
if (config.isGatherStats()) {
gatherStats(request);
}
} catch (Exception e) {
if (e instanceof UnknownHostException) {
//Log.trace("Coudln't connect to GA. Internet may not be available. " + e.toString());
} else {
//Log.trace("Exception while sending the GA tracker request: " + request +". "+ e.toString());
}
}
return response;
}
use of org.apache.http.client.ClientProtocolException in project musiccabinet by hakko.
the class AbstractWSPostClient method executeWSRequest.
/*
* Executes a request to a Last.fm web service.
*
* When adding support for a new web service, a class extending this should be
* implemented. The web service can then be invoked by calling this method, using
* relevant parameters.
*
* The parameter api_key, which is identical for all web service invocations, is
* automatically included.
*
* The response is bundled in a WSResponse object, with eventual error code/message.
*
* Note: For non US-ASCII characters, Last.fm distinguishes between upper and lower
* case. Make sure to use proper capitalization.
*/
protected WSResponse executeWSRequest(List<NameValuePair> params) throws ApplicationException {
authenticateParameterList(params);
WSResponse wsResponse;
HttpClient httpClient = getHttpClient();
try {
HttpPost httpPost = new HttpPost(getURI(params));
httpPost.setEntity(new UrlEncodedFormEntity(params, CharSet.UTF8));
HttpResponse response = httpClient.execute(httpPost);
int statusCode = response.getStatusLine().getStatusCode();
HttpEntity responseEntity = response.getEntity();
String responseBody = EntityUtils.toString(responseEntity);
EntityUtils.consume(responseEntity);
LOG.debug("post responseBody: " + responseBody);
wsResponse = (statusCode == 200) ? new WSResponse(responseBody) : new WSResponse(isHttpRecoverable(statusCode), statusCode, responseBody);
} catch (ClientProtocolException e) {
throw new ApplicationException("The request to post data to Last.fm could not be completed!", e);
} catch (IOException e) {
LOG.warn("Could not post data to Last.fm!", e);
wsResponse = new WSResponse(true, -1, "Call failed due to " + e.getMessage());
}
return wsResponse;
}
use of org.apache.http.client.ClientProtocolException in project Java-Mandrill-Wrapper by cribbstechnologies.
the class MandrillRESTRequestTest method testPostRequestNon200Response.
@Test
public void testPostRequestNon200Response() {
try {
this.request = new MandrillRESTRequest();
this.request.setHttpClient(this.client);
this.request.setConfig(this.config);
this.request.setObjectMapper(this.mapper);
doReturn("postData").when(this.mapper).writeValueAsString(this.emptyBaseRequest);
doReturn(this.response).when(this.client).execute(isA(HttpPost.class));
doReturn(this.manager).when(this.client).getConnectionManager();
Mockito.when(this.response.getEntity()).thenReturn(this.entity);
InputStream inputStream = IOUtils.toInputStream("INPUT");
Mockito.when(this.entity.getContent()).thenReturn(inputStream);
Mockito.when(this.response.getStatusLine()).thenReturn(this.statusLine);
Mockito.when(this.statusLine.getStatusCode()).thenReturn(500);
this.request.postRequest(this.emptyBaseRequest, "Foo", null);
} catch (RequestFailedException rfe) {
assertEquals("Failed : HTTP error code : 500 INPUT", rfe.getMessage());
} catch (ClientProtocolException e) {
fail("Mockito is a good mocking framework, this shouldn't happen");
} catch (IOException e) {
fail("Mockito is a good mocking framework, this shouldn't happen");
}
}
use of org.apache.http.client.ClientProtocolException in project coinbase-bitmonet-sdk by coinbase.
the class HTTPUtils method makeHttpPostWithJSONRequest.
public static HttpResponse makeHttpPostWithJSONRequest(String path, String json) {
try {
HttpPost httpPost = new HttpPost(path);
httpPost.setEntity(new StringEntity(json));
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
return new DefaultHttpClient().execute(httpPost);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
Aggregations