Mvvmcross:如何使用Method =进行Web API调用

时间:2014-04-16 00:09:02

标签: c# json xamarin mvvmcross portable-class-library

我正在使用xamarin c#和Mvvmcross。

我可以使用下面的代码返回一个MyObject列表。

如何更改此设置以便我可以编辑MyObject?

我想像以下一样安慰:

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

但后来我不知道该怎么做?这是我想要调用的Web API方法

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

这是我的代码,它执行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);
        }
    }

0 个答案:

没有答案