mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-17 21:01:34 +08:00
http client image get temporary made with ION
This commit is contained in:
@ -4,12 +4,37 @@
|
|||||||
import promises = require("promises/promises");
|
import promises = require("promises/promises");
|
||||||
import http = require("http/http_request");
|
import http = require("http/http_request");
|
||||||
|
|
||||||
// TODO: Replace with similar to iOS implementation!
|
|
||||||
export function request(options: http.HttpRequestOptions): promises.Promise<http.HttpResponse> {
|
export function request(options: http.HttpRequestOptions): promises.Promise<http.HttpResponse> {
|
||||||
var d = promises.defer<http.HttpResponse>();
|
var d = promises.defer<http.HttpResponse>();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
||||||
|
var isImage = options.url.match(/\.(jpeg|jpg|gif|png)$/i) != null;
|
||||||
|
|
||||||
var context = require("Application/application").Application.current.android.context;
|
var context = require("Application/application").Application.current.android.context;
|
||||||
|
|
||||||
|
if (isImage) {
|
||||||
|
var request = com.koushikdutta.ion.Ion.with(context, options.url);
|
||||||
|
request.asBitmap().setCallback(new com.koushikdutta.async.future.FutureCallback({
|
||||||
|
onCompleted: function (error, data) {
|
||||||
|
if (error) {
|
||||||
|
d.reject(error);
|
||||||
|
} else {
|
||||||
|
d.resolve({
|
||||||
|
content: {
|
||||||
|
raw: data,
|
||||||
|
toString: () => { return null },
|
||||||
|
toJSON: () => { return null },
|
||||||
|
toImage: () => { return require("Image/image").Image.imageFromNativeBitmap(data); }
|
||||||
|
},
|
||||||
|
statusCode: 0,
|
||||||
|
headers: {}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
else {
|
||||||
var request = com.koushikdutta.ion.Ion.getDefault(context).configure().getAsyncHttpRequestFactory()
|
var request = com.koushikdutta.ion.Ion.getDefault(context).configure().getAsyncHttpRequestFactory()
|
||||||
.createAsyncHttpRequest(java.net.URI.create(options.url), options.method, null);
|
.createAsyncHttpRequest(java.net.URI.create(options.url), options.method, null);
|
||||||
|
|
||||||
@ -26,6 +51,9 @@ export function request(options: http.HttpRequestOptions): promises.Promise<http
|
|||||||
if (typeof options.content == "string") {
|
if (typeof options.content == "string") {
|
||||||
request.setBody(new com.koushikdutta.async.http.body.StringBody(options.content));
|
request.setBody(new com.koushikdutta.async.http.body.StringBody(options.content));
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
|
// TODO: How to transfer everything else?
|
||||||
|
}
|
||||||
|
|
||||||
var StringCallback = com.koushikdutta.async.http.AsyncHttpClient.StringCallback.extends({
|
var StringCallback = com.koushikdutta.async.http.AsyncHttpClient.StringCallback.extends({
|
||||||
onCompleted: function (error, response, result) {
|
onCompleted: function (error, response, result) {
|
||||||
@ -45,11 +73,12 @@ export function request(options: http.HttpRequestOptions): promises.Promise<http
|
|||||||
raw: result,
|
raw: result,
|
||||||
toString: () => { return result },
|
toString: () => { return result },
|
||||||
toJSON: () => { return JSON.parse(result) },
|
toJSON: () => { return JSON.parse(result) },
|
||||||
toImage: () =>
|
toImage: () => {
|
||||||
{
|
var imageAsBytes = new java.lang.String(result).getBytes();
|
||||||
|
var bmp = android.graphics.BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length);
|
||||||
// TODO: Implement this!
|
// TODO: Implement this!
|
||||||
return null;
|
//return null;
|
||||||
//return require("Image/image").Image.imageFromNativeBitmap(response);
|
return require("Image/image").Image.imageFromNativeBitmap(bmp);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
statusCode: rawHeaders.getResponseCode(),
|
statusCode: rawHeaders.getResponseCode(),
|
||||||
@ -60,6 +89,7 @@ export function request(options: http.HttpRequestOptions): promises.Promise<http
|
|||||||
});
|
});
|
||||||
|
|
||||||
com.koushikdutta.async.http.AsyncHttpClient.getDefaultInstance().execute(request, new StringCallback());
|
com.koushikdutta.async.http.AsyncHttpClient.getDefaultInstance().execute(request, new StringCallback());
|
||||||
|
}
|
||||||
|
|
||||||
} catch (ex) {
|
} catch (ex) {
|
||||||
d.reject(ex);
|
d.reject(ex);
|
||||||
|
Reference in New Issue
Block a user