Return JSON in Xamarin.iOS -
i trying run sample webrequest return json data. keep on getting xml response. idea why?
this.btngetcoordinates.touchupinside += (sender, e) => { var rxcui = "198440"; var request = httpwebrequest.create(string.format (@"http://rxnav.nlm.nih.gov/rest/rxterms/rxcui/{0}/allinfo", rxcui)); request.method = "get"; request.contenttype = "application/json"; request.begingetresponse(new asynccallback(processgetcoordinates), request); }; return true; } void processgetcoordinates(iasyncresult iar) { httpwebrequest request = (httpwebrequest)iar.asyncstate; using (httpwebresponse response = (httpwebresponse)request.endgetresponse (iar)) { if (response.statuscode != httpstatuscode.ok) { console.out.writeline("error fetching data. server returned status code: {0}", response.statuscode); } using (streamreader strm = new streamreader (response.getresponsestream())) { string content = strm.readtoend (); if (string.isnullorwhitespace (content)) { console.out.writeline("response contained empty body..."); } else { console.out.writeline("response body: \r\n {0}", content); } } } }
you need set accept header on request "application/json".
var request = httpwebrequest.create(string.format (@"http://rxnav.nlm.nih.gov/rest/rxterms/rxcui/{0}/allinfo", rxcui)); request.method = "get"; request.accept = "application/json";
Comments
Post a Comment