将Python后端连接到Android APP

拉金德拉·卡丹(Rajendra Kadam)

如何使用python作为使用C#构建的Android应用程序的后端?Python后端是使用Flask框架编写的。Android应用程序是使用xamarin构建的。

le

无论您的服务器或客户端是否可以使用某种标准的“协议”相互通信,无论使用哪种技术。

有很多通信双方(客户端和服务器)的方法,例如套接字,xml,json等。它们只需要彼此了解即可。

在您的特定情况下,我建议在服务器上构建REST或RESTful API(https://flask-restful.readthedocs.org/en/0.3.3/),并在客户端上构建REST客户端库。

有很多方法和库可以从C#调用REST API:

内置方法将使用HttpWebRequest,如您在此链接上看到的

private async Task<JsonValue> FetchWeatherAsync (string url)
{
    // Create an HTTP web request using the URL:
    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create (new Uri (url));
    request.ContentType = "application/json";
    request.Method = "GET";

    // Send the request to the server and wait for the response:
    using (WebResponse response = await request.GetResponseAsync ())
    {
        // Get a stream representation of the HTTP web response:
        using (Stream stream = response.GetResponseStream ())
        {
            // Use this stream to build a JSON document object:
            JsonValue jsonDoc = await Task.Run (() => JsonObject.Load (stream));
            Console.Out.WriteLine("Response: {0}", jsonDoc.ToString ());

            // Return the JSON document:
            return jsonDoc;
        }
    }
}

但是,如果您不希望您的应用到处都是垃圾(样板代码),我不建议您这样做。

例如,帮助程序库可以是RESTSharp它使您可以轻松构建REST调用,并将响应转换为键入的对象。这是示例:

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 to POST or URL querystring based on Method
request.AddUrlSegment("id", "123"); // replaces matching token in request.Resource

// easily add HTTP Headers
request.AddHeader("header", "value");

// add files to upload (works with compatible verbs)
request.AddFile(path);

// execute the request
RestResponse response = client.Execute(request);
var content = response.Content; // raw content as string

// or automatically deserialize result
// return content type is sniffed but can be 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 with deserialization
var asyncHandle = client.ExecuteAsync<Person>(request, response => {
    Console.WriteLine(response.Data.Name);
});

// abort the request on demand
asyncHandle.Abort();

您可以在Google上搜索“ C#REST客户端”并自行判断。但是恕我直言,我曾经使用过的更容易,更好的REST客户端代码是Refit为什么?您只需通过一个接口即可定义API调用和响应。完全不需要编码!更重要的是,默认情况下,您所有的API调用都会是异步的,这对于移动应用程序需要响应。根据作者的自述文件:

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");

我已经在Xamarin Android / iOS项目上使用了该库,并且运行良好。完全没有问题。

希望能帮助到你

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

将Android App连接到AWS dynamoDB

来自分类Dev

Android蓝牙UUID将APP连接到ANDROID

来自分类Dev

连接到Firebase时Android App崩溃

来自分类Dev

Android App连接到JSP页面

来自分类Dev

从Python Google App Engine连接到Firebase

来自分类Dev

如何将Android App连接到SQL Server数据库

来自分类Dev

在没有Web服务的情况下将Android App连接到ASP网站

来自分类Dev

如何将React.js / React Native Apps连接到一个单一的Azure App Service后端实例?

来自分类Dev

如何将Android应用程序连接到python-socketio后端?

来自分类Dev

如何将前端HTML / JQuery连接到后端Python

来自分类Dev

如何将Google App Engine PaaS连接到DaaS

来自分类Dev

将外部mySql连接到Google App Engine

来自分类Dev

Azure将Java Web App(java)连接到SQLDatabase

来自分类Dev

将Sproutcore App连接到MySQL数据库

来自分类Dev

将App Engine连接到Cloud SQL Access被拒绝

来自分类Dev

使用凭据将Azure Data Factory连接到Logic App

来自分类Dev

将Sproutcore App连接到MySQL数据库

来自分类Dev

将Azure Web App连接到Service Fabric

来自分类Dev

Azure将Java Web App(java)连接到SQLDatabase

来自分类Dev

将 Unity App 连接到 mySQL 数据库

来自分类Dev

无法将 Azure Web App - NodeJS 连接到 Azure Mysql

来自分类Dev

Android App无法连接到本地服务器

来自分类Dev

App Engine后端配置(python)

来自分类Dev

App Engine后端配置(python)

来自分类Dev

具有auth标头的Office App Ajax,连接到启用了cors的安全Web api 2后端

来自分类Dev

如何将Azure App Service Web App连接到Google SQL Server数据库?

来自分类Dev

将Spring Boot App部署到Google App Engine-连接到SQL实例(PostgreSQL)的问题

来自分类Dev

Android App Engine后端实体上传

来自分类Dev

Android后端:Google App Engine与Compute Engine

Related 相关文章

  1. 1

    将Android App连接到AWS dynamoDB

  2. 2

    Android蓝牙UUID将APP连接到ANDROID

  3. 3

    连接到Firebase时Android App崩溃

  4. 4

    Android App连接到JSP页面

  5. 5

    从Python Google App Engine连接到Firebase

  6. 6

    如何将Android App连接到SQL Server数据库

  7. 7

    在没有Web服务的情况下将Android App连接到ASP网站

  8. 8

    如何将React.js / React Native Apps连接到一个单一的Azure App Service后端实例?

  9. 9

    如何将Android应用程序连接到python-socketio后端?

  10. 10

    如何将前端HTML / JQuery连接到后端Python

  11. 11

    如何将Google App Engine PaaS连接到DaaS

  12. 12

    将外部mySql连接到Google App Engine

  13. 13

    Azure将Java Web App(java)连接到SQLDatabase

  14. 14

    将Sproutcore App连接到MySQL数据库

  15. 15

    将App Engine连接到Cloud SQL Access被拒绝

  16. 16

    使用凭据将Azure Data Factory连接到Logic App

  17. 17

    将Sproutcore App连接到MySQL数据库

  18. 18

    将Azure Web App连接到Service Fabric

  19. 19

    Azure将Java Web App(java)连接到SQLDatabase

  20. 20

    将 Unity App 连接到 mySQL 数据库

  21. 21

    无法将 Azure Web App - NodeJS 连接到 Azure Mysql

  22. 22

    Android App无法连接到本地服务器

  23. 23

    App Engine后端配置(python)

  24. 24

    App Engine后端配置(python)

  25. 25

    具有auth标头的Office App Ajax,连接到启用了cors的安全Web api 2后端

  26. 26

    如何将Azure App Service Web App连接到Google SQL Server数据库?

  27. 27

    将Spring Boot App部署到Google App Engine-连接到SQL实例(PostgreSQL)的问题

  28. 28

    Android App Engine后端实体上传

  29. 29

    Android后端:Google App Engine与Compute Engine

热门标签

归档