xamarin - Connecting Python Backend to Android APP -
how use python backend android app built using c#? python backend written using flask framework. android app built using xamarin.
no matter type of technology server or client use if can communicate each other using sort of standard "protocol".
there many ways communicate both sides (client , server) sockets, xml, json, etc. need understand each other.
in particular case suggest build rest or restful api (https://flask-restful.readthedocs.org/en/0.3.3/) on server , rest client library on client.
there many ways , libraries call rest apis c#:
the built-in method using httpwebrequest can see on link:
private async task<jsonvalue> fetchweatherasync (string url) { // create http web request using url: httpwebrequest request = (httpwebrequest)httpwebrequest.create (new uri (url)); request.contenttype = "application/json"; request.method = "get"; // send request server , wait response: using (webresponse response = await request.getresponseasync ()) { // stream representation of http web response: using (stream stream = response.getresponsestream ()) { // use stream build json document object: jsonvalue jsondoc = await task.run (() => jsonobject.load (stream)); console.out.writeline("response: {0}", jsondoc.tostring ()); // return json document: return jsondoc; } } }
but don´t recommend if don´t want app full of crap (boiler plate code) everywhere.
a helper library be, example, restsharp. allows build rest calls , cast response typed objects. here´s , example:
var client = new restclient("http://example.com"); // client.authenticator = new httpbasicauthenticator(username, password); var request = new restrequest("resource/{id}", method.post); request.addparameter("name", "value"); // adds post or url querystring based on method request.addurlsegment("id", "123"); // replaces matching token in request.resource // add http headers request.addheader("header", "value"); // add files upload (works compatible verbs) request.addfile(path); // execute request restresponse response = client.execute(request); var content = response.content; // raw content string // or automatically deserialize result // return content type sniffed can explicitly set via restclient.addhandler(); restresponse<person> response2 = client.execute<person>(request); var name = response2.data.name; // easy async support client.executeasync(request, response => { console.writeline(response.content); }); // async deserialization var asynchandle = client.executeasync<person>(request, response => { console.writeline(response.data.name); }); // abort request on demand asynchandle.abort();
you can search "c# rest client" on google , judge yourself. imho, easier , nicer code rest client i´ve ever used refit. why? define api calls , responses interface. no coding required @ all! more, api calls async default, needed mobile apps responsive. author´s readme:
public interface igithubapi { [get("/users/{user}")] task<user> getuser(string user); } var githubapi = restservice.for<igithubapi>("https://api.github.com"); var octocat = await githubapi.getuser("octocat");
i´ve used library on xamarin android/ios projects , works well. no issues @ all.
hope helps
Comments
Post a Comment