Search in sources :

Example 26 with BasicResponseHandler

use of org.apache.http.impl.client.BasicResponseHandler in project cw-andtutorials by commonsguy.

the class Patchy method updateStatus.

private void updateStatus() {
    try {
        String s = status.getText().toString();
        HttpPost post = new HttpPost("https://identi.ca/api/statuses/update.json");
        post.addHeader("Authorization", "Basic " + getCredentials());
        List<NameValuePair> form = new ArrayList<NameValuePair>();
        form.add(new BasicNameValuePair("status", s));
        post.setEntity(new UrlEncodedFormEntity(form, HTTP.UTF_8));
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        String responseBody = client.execute(post, responseHandler);
        JSONObject response = new JSONObject(responseBody);
    } catch (Throwable t) {
        Log.e("Patchy", "Exception in updateStatus()", t);
        goBlooey(t);
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) JSONObject(org.json.JSONObject) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) ArrayList(java.util.ArrayList) BasicResponseHandler(org.apache.http.impl.client.BasicResponseHandler) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity)

Example 27 with BasicResponseHandler

use of org.apache.http.impl.client.BasicResponseHandler 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;
}
Also used : HttpClient(org.apache.http.client.HttpClient) HttpGet(org.apache.http.client.methods.HttpGet) BasicResponseHandler(org.apache.http.impl.client.BasicResponseHandler) HttpResponseException(org.apache.http.client.HttpResponseException) IOException(java.io.IOException)

Example 28 with BasicResponseHandler

use of org.apache.http.impl.client.BasicResponseHandler in project android-sms-relay by nyaruka.

the class RelayService method sendAlert.

/**
	 * Sends an alert to our server with the passed in subject.  The body will contain
	 * configuration attributes and debugging information.
	 * 
	 * TODO: Would be nice to echo these out to SMS if the client is configured 
	 *       with an alert phone number.
	 *       
	 * @param subject The subject of the alert to send to the server
	 */
public static boolean sendAlert(Context context, String subject, String body) {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    String hostname = prefs.getString("router_hostname", null);
    Log.d(TAG, "__SENDING ALERT: " + subject);
    if (hostname != null && subject != null) {
        // send the alert off
        HttpClient client = new DefaultHttpClient();
        client.getParams().setParameter("http.connection-manager.timeout", new Integer(60000));
        client.getParams().setParameter("http.connection.timeout", new Integer(60000));
        client.getParams().setParameter("http.socket.timeout", new Integer(60000));
        StringBuilder conf = new StringBuilder();
        conf.append("SMS Relay Version: " + AndroidRelay.getVersionNumber(context));
        conf.append("\n");
        conf.append("\nHostname: " + prefs.getString("router_hostname", null));
        String backend = prefs.getString("router_backend", null);
        conf.append("\nBackend:" + backend);
        conf.append("\nPassword:" + prefs.getString("router_password", null));
        conf.append("\n");
        conf.append("\nProcess Incoming:" + prefs.getBoolean("process_incoming", false));
        conf.append("\nProcess Outgoing:" + prefs.getBoolean("process_outgoing", false));
        conf.append("\nInterval:" + prefs.getString("update_interval", "null"));
        TextMessageHelper helper = AndroidRelay.getHelper(context);
        int total = helper.getAllMessages().size();
        int unsynced = helper.withStatus(context, TextMessage.INCOMING, TextMessage.RECEIVED).size();
        conf.append("\n");
        conf.append("\nTotal Messages:" + total);
        conf.append("\nUnsynced:" + unsynced);
        List<TextMessage> erroredOut = helper.withStatus(context, TextMessage.OUTGOING, TextMessage.ERRORED);
        conf.append("\n\nErrored Out: " + erroredOut.size());
        for (TextMessage msg : erroredOut) {
            conf.append("\n" + msg.number + ": " + msg.text);
        }
        List<TextMessage> erroredIn = helper.withStatus(context, TextMessage.INCOMING, TextMessage.ERRORED);
        conf.append("\n\nErrored In: " + erroredIn.size());
        for (TextMessage msg : erroredIn) {
            conf.append("\n" + msg.number + ": " + msg.text);
        }
        conf.append("\n\nLog:\n\n");
        // prepend our configuration to our body
        body = conf.toString() + body;
        try {
            HttpPost post = new HttpPost("http://" + hostname + "/router/alert");
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
            nameValuePairs.add(new BasicNameValuePair("password", "" + prefs.getString("router_password", null)));
            nameValuePairs.add(new BasicNameValuePair("subject", "[" + backend + "] " + subject));
            nameValuePairs.add(new BasicNameValuePair("body", body));
            post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            Log.e(MainActivity.TAG, "Sending log to: " + post.getURI().toURL().toString());
            ResponseHandler<String> responseHandler = new BasicResponseHandler();
            client.execute(post, responseHandler);
            return true;
        } catch (Throwable t) {
            Log.e(MainActivity.TAG, "Sending of alert failed", t);
            return false;
        }
    }
    return false;
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) SharedPreferences(android.content.SharedPreferences) ArrayList(java.util.ArrayList) BasicResponseHandler(org.apache.http.impl.client.BasicResponseHandler) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) HttpClient(org.apache.http.client.HttpClient) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) TextMessage(com.nyaruka.androidrelay.data.TextMessage) TextMessageHelper(com.nyaruka.androidrelay.data.TextMessageHelper)

Example 29 with BasicResponseHandler

use of org.apache.http.impl.client.BasicResponseHandler in project android-sms-relay by nyaruka.

the class RelayService method fetchURL.

public String fetchURL(String url) throws ClientProtocolException, IOException {
    HttpClient client = new DefaultHttpClient();
    client.getParams().setParameter("http.connection-manager.timeout", new Integer(25000));
    client.getParams().setParameter("http.connection.timeout", new Integer(25000));
    client.getParams().setParameter("http.socket.timeout", new Integer(25000));
    HttpGet httpget = new HttpGet(url);
    ResponseHandler<String> responseHandler = new BasicResponseHandler();
    String content = client.execute(httpget, responseHandler);
    return content;
}
Also used : DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) HttpClient(org.apache.http.client.HttpClient) HttpGet(org.apache.http.client.methods.HttpGet) BasicResponseHandler(org.apache.http.impl.client.BasicResponseHandler) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient)

Example 30 with BasicResponseHandler

use of org.apache.http.impl.client.BasicResponseHandler in project qi4j-sdk by Qi4j.

the class ServletTest method test.

// END SNIPPET: usage
@Test
public void test() throws Exception {
    int port = FreePortFinder.findFreePortOnLoopback(9001);
    Server server = new Server(port);
    try {
        ServletContextHandler context = new ServletContextHandler();
        context.setContextPath("/");
        context.addEventListener(new FooServletContextListener());
        context.addServlet(FooServlet.class, "/*");
        server.setHandler(context);
        server.start();
        HttpClient client = new DefaultHttpClient();
        String result = client.execute(new HttpGet("http://127.0.0.1:" + port + "/"), new BasicResponseHandler());
        Assert.assertEquals(APP_NAME, result.trim());
    } finally {
        server.stop();
    }
}
Also used : Server(org.eclipse.jetty.server.Server) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) HttpClient(org.apache.http.client.HttpClient) HttpGet(org.apache.http.client.methods.HttpGet) BasicResponseHandler(org.apache.http.impl.client.BasicResponseHandler) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) Test(org.junit.Test)

Aggregations

BasicResponseHandler (org.apache.http.impl.client.BasicResponseHandler)39 HttpGet (org.apache.http.client.methods.HttpGet)26 Test (org.junit.Test)15 IOException (java.io.IOException)10 HttpPost (org.apache.http.client.methods.HttpPost)10 ArrayList (java.util.ArrayList)7 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)7 NameValuePair (org.apache.http.NameValuePair)6 HttpClient (org.apache.http.client.HttpClient)6 HttpResponseException (org.apache.http.client.HttpResponseException)6 UrlEncodedFormEntity (org.apache.http.client.entity.UrlEncodedFormEntity)6 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)6 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)6 ClientProtocolException (org.apache.http.client.ClientProtocolException)5 RequestConfig (org.apache.http.client.config.RequestConfig)5 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)4 BasicClientCookie (org.apache.http.impl.cookie.BasicClientCookie)4 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 URI (java.net.URI)2 URISyntaxException (java.net.URISyntaxException)2