Search in sources :

Example 1 with NSUrlConnection

use of cli.MonoTouch.Foundation.NSUrlConnection 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;
}
Also used : NSData(cli.MonoTouch.Foundation.NSData) NSUrl(cli.MonoTouch.Foundation.NSUrl) NSUrlConnection(cli.MonoTouch.Foundation.NSUrlConnection) NSError(cli.MonoTouch.Foundation.NSError) NSUrlConnectionDelegate(cli.MonoTouch.Foundation.NSUrlConnectionDelegate) NSMutableData(cli.MonoTouch.Foundation.NSMutableData) NSUrlRequest(cli.MonoTouch.Foundation.NSUrlRequest) FileNotFoundException(java.io.FileNotFoundException)

Example 2 with NSUrlConnection

use of cli.MonoTouch.Foundation.NSUrlConnection 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);
}
Also used : NSObject(cli.MonoTouch.Foundation.NSObject) NSData(cli.MonoTouch.Foundation.NSData) HashMap(java.util.HashMap) NSDictionary(cli.MonoTouch.Foundation.NSDictionary) NSError(cli.MonoTouch.Foundation.NSError) NSUrlConnectionDelegate(cli.MonoTouch.Foundation.NSUrlConnectionDelegate) NSString(cli.MonoTouch.Foundation.NSString) NSHttpUrlResponse(cli.MonoTouch.Foundation.NSHttpUrlResponse) NSUrlResponse(cli.MonoTouch.Foundation.NSUrlResponse) NSUrlConnection(cli.MonoTouch.Foundation.NSUrlConnection) NSMutableData(cli.MonoTouch.Foundation.NSMutableData) List(java.util.List)

Aggregations

NSData (cli.MonoTouch.Foundation.NSData)2 NSError (cli.MonoTouch.Foundation.NSError)2 NSMutableData (cli.MonoTouch.Foundation.NSMutableData)2 NSUrlConnection (cli.MonoTouch.Foundation.NSUrlConnection)2 NSUrlConnectionDelegate (cli.MonoTouch.Foundation.NSUrlConnectionDelegate)2 NSDictionary (cli.MonoTouch.Foundation.NSDictionary)1 NSHttpUrlResponse (cli.MonoTouch.Foundation.NSHttpUrlResponse)1 NSObject (cli.MonoTouch.Foundation.NSObject)1 NSString (cli.MonoTouch.Foundation.NSString)1 NSUrl (cli.MonoTouch.Foundation.NSUrl)1 NSUrlRequest (cli.MonoTouch.Foundation.NSUrlRequest)1 NSUrlResponse (cli.MonoTouch.Foundation.NSUrlResponse)1 FileNotFoundException (java.io.FileNotFoundException)1 HashMap (java.util.HashMap)1 List (java.util.List)1