use of cli.MonoTouch.Foundation.NSUrlConnectionDelegate in project playn by threerings.
the class IOSAssets method getRemoteImage.
@Override
public Image getRemoteImage(String url, float width, float height) {
final IOSAsyncImage image = new IOSAsyncImage(platform.graphics().ctx, width, height);
new NSUrlConnection(new NSUrlRequest(new NSUrl(url)), new NSUrlConnectionDelegate() {
private NSMutableData data = new NSMutableData();
@Override
public void ReceivedData(NSUrlConnection conn, NSData data) {
this.data.AppendData(data);
}
@Override
public void FailedWithError(NSUrlConnection conn, NSError error) {
onFailure(new Exception(error.get_LocalizedDescription()));
}
@Override
public void FinishedLoading(NSUrlConnection conn) {
try {
setImageLater(image, UIImage.LoadFromData(this.data), Scale.ONE);
} catch (Throwable cause) {
onFailure(cause);
}
}
protected void onFailure(final Throwable cause) {
setErrorLater(image, cause);
}
}, true);
return image;
}
use of cli.MonoTouch.Foundation.NSUrlConnectionDelegate in project playn by threerings.
the class IOSNet method sendRequest.
protected void sendRequest(NSUrlRequest req, final Callback<Response> callback) {
new NSUrlConnection(req, new NSUrlConnectionDelegate() {
private NSMutableData data;
private int rspCode = -1;
private NSDictionary headers;
@Override
public void ReceivedResponse(NSUrlConnection conn, NSUrlResponse rsp) {
// if we are redirected, we may accumulate data as we bounce through requests, so we reset
// our data accumulator each time we receive the response headers
data = new NSMutableData();
// note our most recent response code
if (rsp instanceof NSHttpUrlResponse) {
NSHttpUrlResponse hrsp = (NSHttpUrlResponse) rsp;
rspCode = hrsp.get_StatusCode();
headers = hrsp.get_AllHeaderFields();
}
}
@Override
public void ReceivedData(NSUrlConnection conn, NSData data) {
this.data.AppendData(data);
}
@Override
public void FailedWithError(NSUrlConnection conn, NSError error) {
String errmsg = error.get_LocalizedDescription();
Exception exn = rspCode > 0 ? new HttpException(rspCode, errmsg) : new Exception(errmsg);
platform.notifyFailure(callback, exn);
}
@Override
public void FinishedLoading(NSUrlConnection conn) {
platform.notifySuccess(callback, new ResponseImpl(rspCode) {
@Override
protected Map<String, List<String>> extractHeaders() {
Map<String, List<String>> headerMap = new HashMap<String, List<String>>();
for (NSObject key : headers.get_Keys()) {
// iOS concatenates all repeated headers into a single header separated by commas,
// which is known to be a fucking stupid thing to do, but hey, they're doing it!
headerMap.put(key.ToString(), Collections.singletonList(headers.get_Item(key).ToString()));
}
return headerMap;
}
@Override
public String payloadString() {
return NSString.op_Implicit(NSString.FromData(data, NSStringEncoding.wrap(NSStringEncoding.UTF8)));
}
@Override
public byte[] payload() {
int length = Convert.ToInt32(data.get_Length());
byte[] bytes = new byte[length];
Marshal.Copy(data.get_Bytes(), bytes, 0, length);
return bytes;
}
});
}
}, true);
}
Aggregations