c# - Posting to a Web API from a Xamarin Android applicaion -
hi building xamarin android app connects web api. want post complex object api , receive object again. works when post object , receive bool value, not receiving complex object. works when use in windows mobile app. method in portable library. here post method:
public static iresponseobject post<t, tout>(string url, t bindingmodel) { iresponseobject result; using (var client = new httpclient()) { var response = client.postasjsonasync(string.format("{0}{1}", _base, url), bindingmodel).result; if (response.statuscode == httpstatuscode.ok || response.statuscode == httpstatuscode.created) result = response.content.readasasync<successresponseobject<tout>>().result; else result = response.content.readasasync<badrequestresponse>().result; ((responseobject)result).status = response.statuscode; return result; } }
should doing different android? thanks
looks not using async / await. causing issue.
try extension method:
public static async task<tout> postjsonasyncpost<t, tout>this httpclient client, string url, t param) { httpresponsemessage result = await client.postasjsonasync(url, param); string content = await result.ensuresuccessful().content.readasstringasync(); return jsonconvert.deserializeobject<toutput>(content); }
then can do:
using (var httpclient = new httpclient ()) { var result = await httpclient.postjsonasyncpost<t,out>(url,myobject); }
Comments
Post a Comment