diff --git a/declarations.android.d.ts b/declarations.android.d.ts index 441688267..a1a673d0c 100644 --- a/declarations.android.d.ts +++ b/declarations.android.d.ts @@ -367,8 +367,13 @@ declare module com { } } - export class AsyncHttpClient { - static getDefaultInstance(): any; + export module AsyncHttpClient { + function getDefaultInstance(): any; + + export class StringCallback { + constructor(params: any); + static extends(params: any); + } } export module callback { @@ -376,6 +381,12 @@ declare module com { constructor(params: any); } } + + export module body { + export class StringBody { + constructor(source: string); + } + } } } } diff --git a/http/http_request.android.ts b/http/http_request.android.ts index 83d554e5e..7bc7bea1f 100644 --- a/http/http_request.android.ts +++ b/http/http_request.android.ts @@ -9,39 +9,58 @@ export function request(options: http.HttpRequestOptions): promises.Promise(); try { - var headers = new com.koushikdutta.async.http.libcore.RawHeaders(); + var context = require("Application/application").Application.current.android.context; + var request = com.koushikdutta.ion.Ion.getDefault(context).configure().getAsyncHttpRequestFactory() + .createAsyncHttpRequest(java.net.URI.create(options.url), options.method, null); if (options.headers) { for (var key in options.headers) { - headers.add(key, options.headers[key]) + request.addHeader(key, options.headers[key]) } } - var isImage = options.url.match(/\.(jpeg|jpg|gif|png)$/i) != null; + if (typeof options.timeout == "number") { + request.setTimeout(options.timeout); + } - var context = require("Application/application").Application.current.android.context; - var request = com.koushikdutta.ion.Ion.with(context, options.url); + if (typeof options.content == "string") { + request.setBody(new com.koushikdutta.async.http.body.StringBody(options.content)); + } - request = isImage ? request.asBitmap() : request.asString(); - - request.setCallback(new com.koushikdutta.async.future.FutureCallback({ - onCompleted: function (error, data) { + var StringCallback = com.koushikdutta.async.http.AsyncHttpClient.StringCallback.extends({ + onCompleted: function (error, response, result) { if (error) { d.reject(error); } else { + var headers = {}; + var rawHeaders = response.getHeaders().headers; + + for (var i = 0, l = rawHeaders.length(); i < l; i++) { + var key = rawHeaders.getFieldName(i); + headers[key] = rawHeaders.getValue(i); + } + d.resolve({ content: { - raw: data, - toString: () => { return data }, - toJSON: () => { return JSON.parse(data) }, - toImage: () => { return require("Image/image").Image.imageFromNativeBitmap(data); } + raw: result, + toString: () => { return result }, + toJSON: () => { return JSON.parse(result) }, + toImage: () => + { + // TODO: Implement this! + return null; + //return require("Image/image").Image.imageFromNativeBitmap(response); + } }, - statusCode: 0, - headers: {} + statusCode: rawHeaders.getResponseCode(), + headers: headers }); } } - })); + }); + + com.koushikdutta.async.http.AsyncHttpClient.getDefaultInstance().execute(request, new StringCallback()); + } catch (ex) { d.reject(ex); } diff --git a/http/http_request.d.ts b/http/http_request.d.ts index 04d87efff..143e5cb08 100644 --- a/http/http_request.d.ts +++ b/http/http_request.d.ts @@ -11,6 +11,7 @@ export interface HttpRequestOptions { method: string; headers?: any; content?: any; + timeout?: number; } export interface HttpResponse { diff --git a/http/http_request.ios.ts b/http/http_request.ios.ts index cc641bfb8..cfdd8c9c8 100644 --- a/http/http_request.ios.ts +++ b/http/http_request.ios.ts @@ -26,6 +26,10 @@ export function request(options: http.HttpRequestOptions): promises.Promise