Search in sources :

Example 1 with NameValuePair

use of com.koushikdutta.async.http.NameValuePair in project AndroidAsync by koush.

the class HttpServerTests method setUp.

@Override
protected void setUp() throws Exception {
    super.setUp();
    httpServer = new AsyncHttpServer();
    httpServer.setErrorCallback(new CompletedCallback() {

        @Override
        public void onCompleted(Exception ex) {
            fail();
        }
    });
    httpServer.listen(AsyncServer.getDefault(), 5000);
    httpServer.get("/hello", new HttpServerRequestCallback() {

        @Override
        public void onRequest(AsyncHttpServerRequest request, AsyncHttpServerResponse response) {
            assertNotNull(request.getHeaders().get("Host"));
            response.send("hello");
        }
    });
    httpServer.post("/echo", new HttpServerRequestCallback() {

        @Override
        public void onRequest(AsyncHttpServerRequest request, final AsyncHttpServerResponse response) {
            try {
                assertNotNull(request.getHeaders().get("Host"));
                JSONObject json = new JSONObject();
                if (request.getBody() instanceof UrlEncodedFormBody) {
                    UrlEncodedFormBody body = (UrlEncodedFormBody) request.getBody();
                    for (NameValuePair pair : body.get()) {
                        json.put(pair.getName(), pair.getValue());
                    }
                } else if (request.getBody() instanceof JSONObjectBody) {
                    json = ((JSONObjectBody) request.getBody()).get();
                } else if (request.getBody() instanceof StringBody) {
                    json.put("foo", ((StringBody) request.getBody()).get());
                } else if (request.getBody() instanceof MultipartFormDataBody) {
                    MultipartFormDataBody body = (MultipartFormDataBody) request.getBody();
                    for (NameValuePair pair : body.get()) {
                        json.put(pair.getName(), pair.getValue());
                    }
                }
                response.send(json);
            } catch (Exception e) {
            }
        }
    });
}
Also used : NameValuePair(com.koushikdutta.async.http.NameValuePair) CompletedCallback(com.koushikdutta.async.callback.CompletedCallback) HttpServerRequestCallback(com.koushikdutta.async.http.server.HttpServerRequestCallback) AsyncHttpServerRequest(com.koushikdutta.async.http.server.AsyncHttpServerRequest) AsyncHttpServerResponse(com.koushikdutta.async.http.server.AsyncHttpServerResponse) JSONObjectBody(com.koushikdutta.async.http.body.JSONObjectBody) JSONObject(org.json.JSONObject) StringBody(com.koushikdutta.async.http.body.StringBody) AsyncHttpServer(com.koushikdutta.async.http.server.AsyncHttpServer) UrlEncodedFormBody(com.koushikdutta.async.http.body.UrlEncodedFormBody) MultipartFormDataBody(com.koushikdutta.async.http.body.MultipartFormDataBody)

Example 2 with NameValuePair

use of com.koushikdutta.async.http.NameValuePair in project AndroidAsync by koush.

the class MainActivity method getChartFile.

private void getChartFile() {
    final ImageView iv = chart;
    final String filename = getFileStreamPath(randomFile()).getAbsolutePath();
    ArrayList<NameValuePair> pairs = new ArrayList<NameValuePair>();
    pairs.add(new BasicNameValuePair("cht", "lc"));
    pairs.add(new BasicNameValuePair("chtt", "This is a google chart"));
    pairs.add(new BasicNameValuePair("chs", "512x512"));
    pairs.add(new BasicNameValuePair("chxt", "x"));
    pairs.add(new BasicNameValuePair("chd", "t:40,20,50,20,100"));
    UrlEncodedFormBody writer = new UrlEncodedFormBody(pairs);
    try {
        AsyncHttpPost post = new AsyncHttpPost("http://chart.googleapis.com/chart");
        post.setBody(writer);
        AsyncHttpClient.getDefaultInstance().executeFile(post, filename, new AsyncHttpClient.FileCallback() {

            @Override
            public void onCompleted(Exception e, AsyncHttpResponse response, File result) {
                if (e != null) {
                    e.printStackTrace();
                    return;
                }
                Bitmap bitmap = BitmapFactory.decodeFile(filename);
                result.delete();
                if (bitmap == null)
                    return;
                BitmapDrawable bd = new BitmapDrawable(bitmap);
                assignImageView(iv, bd);
            }
        });
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}
Also used : BasicNameValuePair(com.koushikdutta.async.http.BasicNameValuePair) NameValuePair(com.koushikdutta.async.http.NameValuePair) ArrayList(java.util.ArrayList) BitmapDrawable(android.graphics.drawable.BitmapDrawable) IOException(java.io.IOException) Bitmap(android.graphics.Bitmap) AsyncHttpResponse(com.koushikdutta.async.http.AsyncHttpResponse) BasicNameValuePair(com.koushikdutta.async.http.BasicNameValuePair) ImageView(android.widget.ImageView) UrlEncodedFormBody(com.koushikdutta.async.http.body.UrlEncodedFormBody) AsyncHttpPost(com.koushikdutta.async.http.AsyncHttpPost) File(java.io.File) AsyncHttpClient(com.koushikdutta.async.http.AsyncHttpClient)

Example 3 with NameValuePair

use of com.koushikdutta.async.http.NameValuePair in project AndroidAsync by koush.

the class UrlEncodedFormBody method buildData.

private void buildData() {
    boolean first = true;
    StringBuilder b = new StringBuilder();
    try {
        for (NameValuePair pair : mParameters) {
            if (pair.getValue() == null)
                continue;
            if (!first)
                b.append('&');
            first = false;
            b.append(URLEncoder.encode(pair.getName(), "UTF-8"));
            b.append('=');
            b.append(URLEncoder.encode(pair.getValue(), "UTF-8"));
        }
        mBodyBytes = b.toString().getBytes("UTF-8");
    } catch (UnsupportedEncodingException e) {
        throw new AssertionError(e);
    }
}
Also used : NameValuePair(com.koushikdutta.async.http.NameValuePair) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Aggregations

NameValuePair (com.koushikdutta.async.http.NameValuePair)3 UrlEncodedFormBody (com.koushikdutta.async.http.body.UrlEncodedFormBody)2 Bitmap (android.graphics.Bitmap)1 BitmapDrawable (android.graphics.drawable.BitmapDrawable)1 ImageView (android.widget.ImageView)1 CompletedCallback (com.koushikdutta.async.callback.CompletedCallback)1 AsyncHttpClient (com.koushikdutta.async.http.AsyncHttpClient)1 AsyncHttpPost (com.koushikdutta.async.http.AsyncHttpPost)1 AsyncHttpResponse (com.koushikdutta.async.http.AsyncHttpResponse)1 BasicNameValuePair (com.koushikdutta.async.http.BasicNameValuePair)1 JSONObjectBody (com.koushikdutta.async.http.body.JSONObjectBody)1 MultipartFormDataBody (com.koushikdutta.async.http.body.MultipartFormDataBody)1 StringBody (com.koushikdutta.async.http.body.StringBody)1 AsyncHttpServer (com.koushikdutta.async.http.server.AsyncHttpServer)1 AsyncHttpServerRequest (com.koushikdutta.async.http.server.AsyncHttpServerRequest)1 AsyncHttpServerResponse (com.koushikdutta.async.http.server.AsyncHttpServerResponse)1 HttpServerRequestCallback (com.koushikdutta.async.http.server.HttpServerRequestCallback)1 File (java.io.File)1 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1