mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-16 20:11:24 +08:00
http client image get temporary made with ION
This commit is contained in:
@ -4,65 +4,95 @@
|
|||||||
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;
|
||||||
var request = com.koushikdutta.ion.Ion.getDefault(context).configure().getAsyncHttpRequestFactory()
|
|
||||||
.createAsyncHttpRequest(java.net.URI.create(options.url), options.method, null);
|
|
||||||
|
|
||||||
if (options.headers) {
|
if (isImage) {
|
||||||
for (var key in options.headers) {
|
var request = com.koushikdutta.ion.Ion.with(context, options.url);
|
||||||
request.addHeader(key, options.headers[key])
|
request.asBitmap().setCallback(new com.koushikdutta.async.future.FutureCallback({
|
||||||
}
|
onCompleted: function (error, data) {
|
||||||
}
|
if (error) {
|
||||||
|
d.reject(error);
|
||||||
if (typeof options.timeout == "number") {
|
} else {
|
||||||
request.setTimeout(options.timeout);
|
d.resolve({
|
||||||
}
|
content: {
|
||||||
|
raw: data,
|
||||||
if (typeof options.content == "string") {
|
toString: () => { return null },
|
||||||
request.setBody(new com.koushikdutta.async.http.body.StringBody(options.content));
|
toJSON: () => { return null },
|
||||||
}
|
toImage: () => { return require("Image/image").Image.imageFromNativeBitmap(data); }
|
||||||
|
},
|
||||||
var StringCallback = com.koushikdutta.async.http.AsyncHttpClient.StringCallback.extends({
|
statusCode: 0,
|
||||||
onCompleted: function (error, response, result) {
|
headers: {}
|
||||||
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: result,
|
|
||||||
toString: () => { return result },
|
|
||||||
toJSON: () => { return JSON.parse(result) },
|
|
||||||
toImage: () =>
|
|
||||||
{
|
|
||||||
// TODO: Implement this!
|
|
||||||
return null;
|
|
||||||
//return require("Image/image").Image.imageFromNativeBitmap(response);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
statusCode: rawHeaders.getResponseCode(),
|
|
||||||
headers: headers
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}));
|
||||||
});
|
}
|
||||||
|
else {
|
||||||
|
var request = com.koushikdutta.ion.Ion.getDefault(context).configure().getAsyncHttpRequestFactory()
|
||||||
|
.createAsyncHttpRequest(java.net.URI.create(options.url), options.method, null);
|
||||||
|
|
||||||
com.koushikdutta.async.http.AsyncHttpClient.getDefaultInstance().execute(request, new StringCallback());
|
if (options.headers) {
|
||||||
|
for (var key in options.headers) {
|
||||||
|
request.addHeader(key, options.headers[key])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (typeof options.timeout == "number") {
|
||||||
|
request.setTimeout(options.timeout);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (typeof options.content == "string") {
|
||||||
|
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({
|
||||||
|
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: result,
|
||||||
|
toString: () => { return result },
|
||||||
|
toJSON: () => { return JSON.parse(result) },
|
||||||
|
toImage: () => {
|
||||||
|
var imageAsBytes = new java.lang.String(result).getBytes();
|
||||||
|
var bmp = android.graphics.BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length);
|
||||||
|
// TODO: Implement this!
|
||||||
|
//return null;
|
||||||
|
return require("Image/image").Image.imageFromNativeBitmap(bmp);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
statusCode: rawHeaders.getResponseCode(),
|
||||||
|
headers: headers
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
com.koushikdutta.async.http.AsyncHttpClient.getDefaultInstance().execute(request, new StringCallback());
|
||||||
|
}
|
||||||
|
|
||||||
} catch (ex) {
|
} catch (ex) {
|
||||||
d.reject(ex);
|
d.reject(ex);
|
||||||
}
|
}
|
||||||
return d.promise();
|
return d.promise();
|
||||||
}
|
}
|
Reference in New Issue
Block a user