use of org.apache.http.client.HttpResponseException in project ThinkAndroid by white-cat.
the class AsyncHttpResponseHandler method sendResponseMessage.
protected void sendResponseMessage(HttpResponse response) {
StatusLine status = response.getStatusLine();
String responseBody = null;
try {
HttpEntity entity = null;
HttpEntity temp = response.getEntity();
if (temp != null) {
entity = new BufferedHttpEntity(temp);
responseBody = EntityUtils.toString(entity, "UTF-8");
}
} catch (IOException e) {
sendFailureMessage(e, (String) null);
}
if (status.getStatusCode() >= 300) {
sendFailureMessage(new HttpResponseException(status.getStatusCode(), status.getReasonPhrase()), responseBody);
} else {
sendSuccessMessage(status.getStatusCode(), response.getAllHeaders(), responseBody);
}
}
use of org.apache.http.client.HttpResponseException in project musiccabinet by hakko.
the class AbstractMusicBrainzClientTest method failsWithApplicationExceptionOnInternalErrors.
@Test(expected = ApplicationException.class)
@SuppressWarnings("unchecked")
public void failsWithApplicationExceptionOnInternalErrors() throws Exception {
TestMusicBrainzClient client = getClient();
when(client.getHttpClient().execute(any(HttpUriRequest.class), any(ResponseHandler.class))).thenThrow(new HttpResponseException(503, "NA"));
client.get();
}
use of org.apache.http.client.HttpResponseException in project musiccabinet by hakko.
the class AbstractWSGetClientTest method validatePackagingOfResponseCode503.
@Test
public void validatePackagingOfResponseCode503() throws ApplicationException, IOException {
final int errorCode = 503;
final String errorMessage = "Service temporary unavailable";
HttpResponseException hpe = new HttpResponseException(errorCode, errorMessage);
TestWSGetClient testWSClient = getTestWSClient(hpe);
WSResponse response = testWSClient.testCall();
Assert.assertTrue(response.wasCallAllowed());
Assert.assertFalse(response.wasCallSuccessful());
Assert.assertTrue(response.isErrorRecoverable());
Assert.assertEquals(errorCode, response.getErrorCode());
Assert.assertEquals(errorMessage, response.getErrorMessage());
}
use of org.apache.http.client.HttpResponseException in project musiccabinet by hakko.
the class AbstractWSGetClient method invokeSingleCall.
/*
* Make a single call to a Last.fm web service, and return a packaged result.
*/
private WSResponse invokeSingleCall(List<NameValuePair> params) throws ApplicationException {
if (throttleService != null) {
throttleService.awaitAllowance();
}
WSResponse wsResponse;
HttpClient httpClient = getHttpClient();
try {
HttpGet httpGet = new HttpGet(getURI(params));
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String responseBody = httpClient.execute(httpGet, responseHandler);
wsResponse = new WSResponse(responseBody);
} catch (HttpResponseException e) {
wsResponse = new WSResponse(isHttpRecoverable(e.getStatusCode()), e.getStatusCode(), e.getMessage());
} catch (IOException e) {
LOG.warn("Could not fetch data from Last.fm!", e);
wsResponse = new WSResponse(true, -1, "Call failed due to " + e.getMessage());
}
return wsResponse;
}
use of org.apache.http.client.HttpResponseException in project android-sms-relay by nyaruka.
the class RelayService method checkOutbox.
/**
* Sends a message to our server.
* @param msg
*/
public void checkOutbox() throws IOException {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
String updateInterval = prefs.getString("update_interval", "30000");
long interval = Long.parseLong(updateInterval);
String outboxURL = prefs.getString("outbox_url", null);
TextMessageHelper helper = getHelper();
// no delivery url means we don't do anything
if (outboxURL == null || outboxURL.length() == 0) {
return;
}
// if our update interval is set to 0, then that means we shouldn't be checking, so skip
if (interval == 0) {
return;
}
Log.d(TAG, "Outbox URL: " + outboxURL);
try {
String content = fetchURL(outboxURL);
if (content.trim().length() > 0) {
JSONObject json = new JSONObject(content);
JSONArray responses = json.getJSONArray("outbox");
for (int i = 0; i < responses.length(); i++) {
JSONObject response = responses.getJSONObject(i);
if ("O".equals(response.getString("direction")) && "Q".equals(response.getString("status"))) {
String number = "+" + response.getString("contact");
String message = response.getString("text");
long serverId = response.getLong("id");
// if this message doesn't already exist
TextMessage existing = helper.withServerId(this.getApplicationContext(), serverId);
if (existing == null) {
Log.d(TAG, "New outgoing msg: " + serverId + ": " + message);
TextMessage toSend = new TextMessage(number, message, serverId);
helper.createMessage(toSend);
sendMessage(toSend);
} else {
if (existing.status == TextMessage.DONE) {
existing.status = TextMessage.SENT;
helper.updateMessage(existing);
}
Log.d(TAG, "Ignoring message: " + serverId + " already queued.");
}
}
}
}
Log.d(TAG, "Outbox fetched from server");
} catch (HttpResponseException e) {
Log.d(TAG, "Got Error: " + e.getMessage(), e);
} catch (IOException e) {
throw e;
} catch (Throwable t) {
Log.d(TAG, "Got Error: " + t.getMessage(), t);
}
}
Aggregations