2

I am using xamarin c# and Mvvmcross.

I am able to do a Get using the below code which returns a List of MyObject.

How can I change this so that I can edit a MyObject?

I would like to do soothing like:

url = "http://address/api/MyObject/myId";
request.Method = "Put";
request.SecondParamOfWebAPICall = new MyObject(){ObjectId = "myId", FieldToChange = "123"};

But then i am not sure how to do this?. This is the web API method I want to call

    // PUT api/MyObject/id
    public IHttpActionResult PutMyObject(int id, MyObject myObject)
    {
         //Use param id to get the required object to edit 
    }

This is my code which does a Get:

    public void GetMyObjectItems(Action<MyObject> success, Action<Exception> error)
    {
        var url = "http://address/api/MyObject";

        var request = (HttpWebRequest)WebRequest.Create(url);
        try
        {
            request.BeginGetResponse(result => ProcessResponse(success, error, request, result), null);
        }
        catch (Exception exception)
        {
            error(exception);
        }
    }

    private void ProcessResponse(Action<MyObject> success, Action<Exception> error, HttpWebRequest request, IAsyncResult result)
    {
        try
        {
            var response = request.EndGetResponse(result);
            //var locationList = response.Content.ReadAsAsync<MyObject>().Result;

            using (var stream = response.GetResponseStream())
            using (var reader = new StreamReader(stream))
            {
                var text = reader.ReadToEnd();
                var list = _jsonConverter.DeserializeObject<MyObject>(text);

                success(list);
            }
        }
        catch (Exception exception)
        {
            error(exception);
        }
    }
Bob
  • 4,236
  • 12
  • 45
  • 65
  • so your class has property `ObjectId` but in json it has name `myId` and for deserialization you want to set some mapping, am I right? – choper Apr 16 '14 at 07:51
  • My Choper. I have update the question so it's more clear. I have said the webAPI method I am trying to call and how I am attempting it. Let me know if it's not clear. Thanks – Bob Apr 16 '14 at 11:40
  • Try searching for questions about HttpWebRequest and PUT - e.g. http://stackoverflow.com/questions/13642401/windows-phone-8-http-request-with-custom-header and http://stackoverflow.com/questions/9054378/how-to-issue-put-httpwebrequest - there should be plenty out there on this already. Alternatively, install HttpClient from nuget and then use that - switching to `async` will rock your world! – Stuart Apr 16 '14 at 13:06
  • I'd have thought you would need to use the request.write() method, to 'PUT' information and then process the response. – l4rd Apr 16 '14 at 13:28

0 Answers0