mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-17 21:01:34 +08:00
http client with promises for both Android and iOS
This commit is contained in:
@ -1,5 +1,3 @@
|
|||||||
|
|
||||||
|
|
||||||
export function when(...promises: Promise[]): Promise {
|
export function when(...promises: Promise[]): Promise {
|
||||||
var all_done = new Deferred();
|
var all_done = new Deferred();
|
||||||
var results = [];
|
var results = [];
|
||||||
@ -31,7 +29,7 @@ export class Promise {
|
|||||||
|
|
||||||
constructor(private deferred: Deferred) { }
|
constructor(private deferred: Deferred) { }
|
||||||
|
|
||||||
then(callback: Function, error: Function): Promise {
|
then(callback: Function, error?: Function): Promise {
|
||||||
return this.deferred.then(callback, error);
|
return this.deferred.then(callback, error);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,19 +1,16 @@
|
|||||||
Sample code:
|
Sample code:
|
||||||
```
|
```
|
||||||
var webClientModule = require("WebClient");
|
var http = require("net").http;
|
||||||
var webClient = webClientModule.Client;
|
|
||||||
|
|
||||||
var client = new webClient();
|
http.getString("http://www.reddit.com/").then(function(result) {
|
||||||
|
|
||||||
client.getString("http://www.reddit.com/", function(result) {
|
|
||||||
// Result is string!
|
// Result is string!
|
||||||
}, function(e) { console.log("Error:" + e.message); });
|
}, function(e) { console.log("Error:" + e.message); });
|
||||||
|
|
||||||
client.getJSON("http://www.reddit.com/r/aww.json?limit=10", function(result) {
|
http.getJSON("http://www.reddit.com/r/aww.json?limit=10").then(function(result) {
|
||||||
// Result is JSON!
|
// Result is JSON!
|
||||||
}, function(e) { console.log("Error:" + e.message); });
|
}, function(e) { console.log("Error:" + e.message); });
|
||||||
|
|
||||||
client.getImage("http://www.telerik.com/sfimages/default-source/Homepage/hp_any_approachf6e4079a7a99493a8ab2e367b9cb3f7d.png", function(result) {
|
http.getImage("http://www.telerik.com/sfimages/default-source/Homepage/hp_any_approachf6e4079a7a99493a8ab2e367b9cb3f7d.png").then(function(result) {
|
||||||
// Result is tk.ui.Image!
|
// Result is tk.ui.Image!
|
||||||
}, function(e) { console.log("Error:" + e.message); });
|
}, function(e) { console.log("Error:" + e.message); });
|
||||||
|
|
||||||
|
@ -1,85 +1,74 @@
|
|||||||
/**
|
/**
|
||||||
* Android specific WebClient implementation.
|
* Android specific http client implementation.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import image_module = require("Image/image");
|
import image_module = require("Image/image");
|
||||||
import app_module = require("Application/application");
|
import app_module = require("Application/application");
|
||||||
|
import promises = require("promises/promises");
|
||||||
|
|
||||||
export class HttpClient {
|
export class http {
|
||||||
/**
|
/**
|
||||||
* Downloads string from url.
|
* Gets string from url.
|
||||||
*/
|
*/
|
||||||
public getString(url: string, successCallback: (result: string) => void, errorCallback?: (e: Error) => void) {
|
public static getString(url: string): promises.Promise {
|
||||||
try {
|
var d = new promises.Deferred();
|
||||||
if (successCallback) {
|
|
||||||
var context = app_module.tk.ui.Application.current.android.context;
|
|
||||||
com.koushikdutta.ion.Ion.with(context, url).asString().setCallback(new com.koushikdutta.async.future.FutureCallback({
|
|
||||||
onCompleted: function (e, result) {
|
|
||||||
if (e && errorCallback) {
|
|
||||||
errorCallback(new Error(e.toString()));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
successCallback(result);
|
|
||||||
}
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
} catch (ex) {
|
|
||||||
|
|
||||||
if (errorCallback) {
|
var context = app_module.tk.ui.Application.current.android.context;
|
||||||
errorCallback(ex);
|
com.koushikdutta.ion.Ion.with(context, url).asString().setCallback(new com.koushikdutta.async.future.FutureCallback({
|
||||||
}
|
onCompleted: function (e, result) {
|
||||||
|
if (e) {
|
||||||
}
|
d.reject(e);
|
||||||
}
|
return;
|
||||||
|
|
||||||
public getJSON(url: string, successCallback: (result: Object) => void, errorCallback?: (e: Error) => void) {
|
|
||||||
try {
|
|
||||||
this.getString(url, function (data) {
|
|
||||||
if (successCallback) {
|
|
||||||
successCallback(JSON.parse(data));
|
|
||||||
}
|
}
|
||||||
}, errorCallback);
|
d.resolve(result);
|
||||||
} catch (ex) {
|
|
||||||
if (errorCallback) {
|
|
||||||
errorCallback(ex);
|
|
||||||
}
|
}
|
||||||
}
|
}));
|
||||||
|
|
||||||
|
return d.promise();
|
||||||
}
|
}
|
||||||
|
|
||||||
public getImage(url: string, successCallback: (result: image_module.Image) => void, errorCallback?: (e: Error) => void) {
|
/**
|
||||||
try {
|
* Gets JSON from url.
|
||||||
if (successCallback) {
|
*/
|
||||||
var context = app_module.tk.ui.Application.current.android.context;
|
public static getJSON(url: string): promises.Promise {
|
||||||
com.koushikdutta.ion.Ion.with(context, url).asBitmap().setCallback(new com.koushikdutta.async.future.FutureCallback({
|
var d = new promises.Deferred();
|
||||||
onCompleted: function (e, result) {
|
|
||||||
if (e && errorCallback) {
|
|
||||||
errorCallback(new Error(e.toString()));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
var image = new image_module.Image();
|
var context = app_module.tk.ui.Application.current.android.context;
|
||||||
image.loadFromBitmap(result);
|
com.koushikdutta.ion.Ion.with(context, url).asString().setCallback(new com.koushikdutta.async.future.FutureCallback({
|
||||||
|
onCompleted: function (e, result) {
|
||||||
successCallback(image);
|
if (e) {
|
||||||
}
|
d.reject(e);
|
||||||
}));
|
return;
|
||||||
|
}
|
||||||
|
d.resolve(JSON.parse(result));
|
||||||
}
|
}
|
||||||
} catch (ex) {
|
}));
|
||||||
|
|
||||||
if (errorCallback) {
|
return d.promise();
|
||||||
errorCallback(ex);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static get(url: string, successCallback: (result: any) => void, errorCallback?: (e: Error) => void) {
|
/**
|
||||||
try {
|
* Gets image from url.
|
||||||
|
*/
|
||||||
|
public static getImage(url: string): promises.Promise {
|
||||||
|
var d = new promises.Deferred();
|
||||||
|
|
||||||
} catch (ex) {
|
var context = app_module.tk.ui.Application.current.android.context;
|
||||||
if (errorCallback) {
|
com.koushikdutta.ion.Ion.with(context, url).asBitmap().setCallback(new com.koushikdutta.async.future.FutureCallback({
|
||||||
errorCallback(ex);
|
onCompleted: function (e, result) {
|
||||||
|
if (e) {
|
||||||
|
d.reject(e);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var image = new image_module.Image();
|
||||||
|
image.loadFromBitmap(result);
|
||||||
|
|
||||||
|
d.resolve(image);
|
||||||
}
|
}
|
||||||
}
|
}));
|
||||||
|
|
||||||
|
|
||||||
|
return d.promise();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
12
net/http_client.d.ts
vendored
12
net/http_client.d.ts
vendored
@ -1,11 +1,11 @@
|
|||||||
/**
|
/**
|
||||||
* The Client interface.
|
* The http client interface.
|
||||||
*/
|
*/
|
||||||
import image_module = require("Image/image");
|
import image_module = require("Image/image");
|
||||||
|
import promises = require("promises/promises");
|
||||||
|
|
||||||
export declare class HttpClient {
|
export declare class http {
|
||||||
private static get(url: string, successCallback?: (result: any) => void, errorCallback?: (e: Error) => void)
|
private static getString(url: string): promises.Promise;
|
||||||
getString(url: string, successCallback?: (result: string) => void, errorCallback?: (e: Error) => void)
|
private static getJSON(url: string): promises.Promise;
|
||||||
getJSON(url: string, successCallback?: (result: Object) => void, errorCallback?: (e: Error) => void)
|
private static getImage(url: string): promises.Promise;
|
||||||
getImage(url: string, successCallback?: (result: image_module.Image) => void, errorCallback?: (e: Error) => void)
|
|
||||||
}
|
}
|
@ -1,70 +1,43 @@
|
|||||||
/**
|
/**
|
||||||
* iOS specific WebClient implementation.
|
* iOS specific http client implementation.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import image_module = require("Image/image");
|
import image_module = require("Image/image");
|
||||||
import promises = require("promises/promises");
|
import promises = require("promises/promises");
|
||||||
|
|
||||||
export class HttpClient {
|
export class http {
|
||||||
/**
|
/**
|
||||||
* Downloads string from url.
|
* Gets string from url.
|
||||||
*/
|
*/
|
||||||
public getString(url: string, successCallback: (result: string) => void, errorCallback?: (e: Error) => void) {
|
public static getString(url : string) : promises.Promise {
|
||||||
try {
|
|
||||||
HttpClient.get(url, function (data) {
|
|
||||||
if (successCallback) {
|
|
||||||
successCallback(Foundation.NSString.initWithDataEncoding(data, 4).toString());
|
|
||||||
}
|
|
||||||
}, errorCallback);
|
|
||||||
} catch (ex) {
|
|
||||||
if (errorCallback) {
|
|
||||||
errorCallback(ex);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static getString(url : string) {
|
|
||||||
var d = new promises.Deferred();
|
var d = new promises.Deferred();
|
||||||
new HttpClient().getString(url, r => d.resolve(r), e => d.reject(e));
|
http.get(url, r => d.resolve(Foundation.NSString.initWithDataEncoding(r, 4).toString()), e => d.reject(e));
|
||||||
return d.promise();
|
return d.promise();
|
||||||
}
|
}
|
||||||
|
|
||||||
public getJSON(url: string, successCallback: (result: Object) => void, errorCallback?: (e: Error) => void) {
|
/**
|
||||||
try {
|
* Gets JSON from url.
|
||||||
this.getString(url, function (data) {
|
*/
|
||||||
if (successCallback) {
|
public static getJSON(url: string) : promises.Promise {
|
||||||
successCallback(JSON.parse(data));
|
var d = new promises.Deferred();
|
||||||
}
|
http.get(url, r => d.resolve(JSON.parse(Foundation.NSString.initWithDataEncoding(r, 4).toString())), e => d.reject(e));
|
||||||
}, errorCallback);
|
return d.promise();
|
||||||
} catch (ex) {
|
|
||||||
if (errorCallback) {
|
|
||||||
errorCallback(ex);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public getImage(url: string, successCallback: (result: image_module.Image) => void, errorCallback?: (e: Error) => void) {
|
/**
|
||||||
HttpClient.get(url, function (data) {
|
* Gets image from url.
|
||||||
if (successCallback) {
|
*/
|
||||||
var image = new image_module.Image();
|
public static getImage(url: string) : promises.Promise {
|
||||||
image.loadFromData(data);
|
var d = new promises.Deferred();
|
||||||
successCallback(image);
|
http.get(url, r => {
|
||||||
}
|
var image = new image_module.Image();
|
||||||
}, errorCallback);
|
image.loadFromData(r);
|
||||||
|
d.resolve(image);
|
||||||
|
}, e => d.reject(e));
|
||||||
|
return d.promise();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static get(url: string, successCallback?: (result: any) => void, errorCallback?: (e: Error) => void) {
|
private static get(url: string, successCallback: (result: any) => void, errorCallback?: (e: Error) => void) {
|
||||||
if (!successCallback && !errorCallback)
|
|
||||||
{
|
|
||||||
var d = new promises.Deferred();
|
|
||||||
HttpClient.getUrl(url, r => d.resolve(r), e => d.reject(e));
|
|
||||||
return d.promise();
|
|
||||||
}
|
|
||||||
|
|
||||||
HttpClient.getUrl(url, successCallback, errorCallback);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static getUrl(url: string, successCallback: (result: any) => void, errorCallback?: (e: Error) => void) {
|
|
||||||
try {
|
try {
|
||||||
var sessionConfig = Foundation.NSURLSessionConfiguration.defaultSessionConfiguration();
|
var sessionConfig = Foundation.NSURLSessionConfiguration.defaultSessionConfiguration();
|
||||||
var queue = Foundation.NSOperationQueue.mainQueue();
|
var queue = Foundation.NSOperationQueue.mainQueue();
|
||||||
|
Reference in New Issue
Block a user