Search in sources :

Example 1 with NSDictionary

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