mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-11-05 13:26:48 +08:00
http client improved
This commit is contained in:
15
declarations.android.d.ts
vendored
15
declarations.android.d.ts
vendored
@@ -367,8 +367,13 @@ declare module com {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export class AsyncHttpClient {
|
export module AsyncHttpClient {
|
||||||
static getDefaultInstance(): any;
|
function getDefaultInstance(): any;
|
||||||
|
|
||||||
|
export class StringCallback {
|
||||||
|
constructor(params: any);
|
||||||
|
static extends(params: any);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export module callback {
|
export module callback {
|
||||||
@@ -376,6 +381,12 @@ declare module com {
|
|||||||
constructor(params: any);
|
constructor(params: any);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export module body {
|
||||||
|
export class StringBody {
|
||||||
|
constructor(source: string);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,39 +9,58 @@ export function request(options: http.HttpRequestOptions): promises.Promise<http
|
|||||||
var d = promises.defer<http.HttpResponse>();
|
var d = promises.defer<http.HttpResponse>();
|
||||||
|
|
||||||
try {
|
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) {
|
if (options.headers) {
|
||||||
for (var key in 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;
|
if (typeof options.content == "string") {
|
||||||
var request = com.koushikdutta.ion.Ion.with(context, options.url);
|
request.setBody(new com.koushikdutta.async.http.body.StringBody(options.content));
|
||||||
|
}
|
||||||
|
|
||||||
request = isImage ? request.asBitmap() : request.asString();
|
var StringCallback = com.koushikdutta.async.http.AsyncHttpClient.StringCallback.extends({
|
||||||
|
onCompleted: function (error, response, result) {
|
||||||
request.setCallback(new com.koushikdutta.async.future.FutureCallback({
|
|
||||||
onCompleted: function (error, data) {
|
|
||||||
if (error) {
|
if (error) {
|
||||||
d.reject(error);
|
d.reject(error);
|
||||||
} else {
|
} 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({
|
d.resolve({
|
||||||
content: {
|
content: {
|
||||||
raw: data,
|
raw: result,
|
||||||
toString: () => { return data },
|
toString: () => { return result },
|
||||||
toJSON: () => { return JSON.parse(data) },
|
toJSON: () => { return JSON.parse(result) },
|
||||||
toImage: () => { return require("Image/image").Image.imageFromNativeBitmap(data); }
|
toImage: () =>
|
||||||
|
{
|
||||||
|
// TODO: Implement this!
|
||||||
|
return null;
|
||||||
|
//return require("Image/image").Image.imageFromNativeBitmap(response);
|
||||||
|
}
|
||||||
},
|
},
|
||||||
statusCode: 0,
|
statusCode: rawHeaders.getResponseCode(),
|
||||||
headers: {}
|
headers: headers
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}));
|
});
|
||||||
|
|
||||||
|
com.koushikdutta.async.http.AsyncHttpClient.getDefaultInstance().execute(request, new StringCallback());
|
||||||
|
|
||||||
} catch (ex) {
|
} catch (ex) {
|
||||||
d.reject(ex);
|
d.reject(ex);
|
||||||
}
|
}
|
||||||
|
|||||||
1
http/http_request.d.ts
vendored
1
http/http_request.d.ts
vendored
@@ -11,6 +11,7 @@ export interface HttpRequestOptions {
|
|||||||
method: string;
|
method: string;
|
||||||
headers?: any;
|
headers?: any;
|
||||||
content?: any;
|
content?: any;
|
||||||
|
timeout?: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface HttpResponse {
|
export interface HttpResponse {
|
||||||
|
|||||||
@@ -26,6 +26,10 @@ export function request(options: http.HttpRequestOptions): promises.Promise<http
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (typeof options.timeout == "number") {
|
||||||
|
urlRequest.setTimeoutInterval(options.timeout * 1000);
|
||||||
|
}
|
||||||
|
|
||||||
if (typeof options.content == "string") {
|
if (typeof options.content == "string") {
|
||||||
urlRequest.setHTTPBody(Foundation.NSString.initWithString(options.content).dataUsingEncoding(4));
|
urlRequest.setHTTPBody(Foundation.NSString.initWithString(options.content).dataUsingEncoding(4));
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user