Showing posts with label RestSharp. Show all posts
Showing posts with label RestSharp. Show all posts

Sunday, December 23, 2012

Rest Sharp MonoDroid, Post in Body.

   I needed to add an item in the body of a post via RestSharp in Mono Droid.  Luckily, the way I've built things, doesn't really expose any endpoints via code, for this reference.  So here is the code.. if you want to know how to post an object in the body of a Rest Sharp request.

public static Credentials Login(Credentials credentials)
        {
            var client = new RestClient();
            var request = new RestRequest();
            request.Method = Method.POST;
            var json = JsonConvert.SerializeObject(credentials);
            request.AddParameter("application/json; charset=utf-8", json, ParameterType.RequestBody);
            request.Resource = ServiceBase+string.Format(Data.Service.Login);

            RestResponse<Credentials> response = (RestResponse<Credentials>)client.Execute<Credentials>(request);
            Credentials item = new Credentials();
            JsonConvert.PopulateObject(response.Content,item);
            return item;
        }

Pretty simple, yes? -smiles-

Thursday, December 20, 2012

Useful Mono Droid Snippets.


Nowdays, everybody loves JSON.  Truth is.. I feel it is much easier to use in some regards for Web, and Mobile projects than XML, but there is something to be said for both data formats.

This is a snippet for retrieving any web response in JSON, via Rest Sharp in a Mono Droid, or Mono Touch environment, that you can then parse with your JSON Deserializer (Whatever flavor you prefer, hopefully one that supports AsParallel()).

This isn't the code I am using anymore.  As the first security layer is in place.  Below I have a post semi-related to the 2nd security layer, which is semi in-progress.. involving X509 certificates.

I also have made a model for a web service that will provide all User Application management functions for all mobile applications that I'll be developing.  I prefer the cloud.  It's cheap, I don't need anyone other than myself.  (Even though I'm a pretty decent sys admin).

        private static RestClient CutDownBloat()
        {
            var client = new RestClient();
            client.AddHandler("application/json; charset=utf-8",new RestSharp.Deserializers.JsonDeserializer());
            return client;
        }
        private static RestResponse<Object> CutDownCode(string url)
        {
            var request = new RestRequest();
            request.Method = Method.GET;
            request.Resource = url;
            var client = CutDownBloat();
            RestResponse<Object> response = (RestResponse<Object>)client.Execute<Object>(request);
            return response;
        }


In order to make a true secure system for a mobile application, you need a unique ID for the device attempting to access your services.

Just getting a unique device ID is a key component.


This is a snippet that will return a Unique Device ID for Android devices.


public static class DeviceData
    {
        public static string getDeviceID(Context context)
        {
            TelephonyManager tm = (TelephonyManager) context.GetSystemService(Context.TelephonyService);
            return tm.DeviceId;
        }
    }