简体   繁体   English

Android应用程序开发和Web服务器交互

[英]Android App Development and Web Server Interactions

I am just learning about Android Development so excuse me if this is a bit off in nature. 我只是在学习Android开发,因此,如果这本质上有点偏离,请原谅。 I am wanting to make an app that interacts with a Database from my website, in a sense the two things will be one feeding the other. 从某种意义上讲,我想做一个与我的网站与数据库交互的应用程序,从某种意义上说,这两件事将相互促进。 So with that. 因此。 I am trying to figure out whats the best way to interact with my server. 我试图找出什么是与服务器交互的最佳方法。 I don't want an app thats an app in a browser like environment I want to dev a full app that works independently of the site only sharing the DB and like features. 我不希望这样的应用程序在类似浏览器的环境中使用,我希望开发一个完整的应用程序,该应用程序独立于站点而仅共享数据库和类似功能。 So what would be my best approach? 那么我最好的方法是什么?

Is building the app so it can post/get to php files on the server interacting basically through JSON/XML my best and or safest bet or is there a better approach that connects the App to the servers Database that doesn't require me to open the database to any ip that makes a request. 正在构建应用程序,以便可以将其发布/获取基本上通过JSON / XML交互的服务器上的php文件,这是我最好和/或最安全的选择,还是有一种更好的方法将应用程序连接到服务器,不需要我打开数据库到发出请求的任何IP。

Just looking for opinions and suggestions here. 只是在这里寻找意见和建议。 I figure everyone who's going to see this is familiar with Android development and best practices where as I could and have surfed blogs and all else but the opinion seems to be 50/50 as to which is best. 我认为将要看到这一点的每个人都熟悉Android开发和最佳实践,在我可以并且已经浏览过博客和其他所有东西的地方,但对于哪种最好的看法似乎是50/50。

I'm sure there are libraries out there for Android that help you with HTTP Get and Post, however, if you really want to understand what is going there are just a couple of classes you have to understand in order to make the necessary classes yourself. 我确定有很多Android的库可以帮助您进行HTTP Get和Post操作,但是,如果您真的想了解发生的事情,则只需要了解几个类即可自己创建必要的类。

First, get to know HttpClient, HTTPGet, HTTPPost, and HTTPResponse. 首先,了解HttpClient,HTTPGet,HTTPPost和HTTPResponse。 Some of the later versions of Android have some nice other classes as well, but those four is pretty much all you need to get started. Android的某些更高版本也具有其他一些不错的类,但是这四个几乎是您入门所需的全部。

You need to do something like this: 你需要做这样的事情:

HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet("http://www.myurl.com/api_name");
HttpResponse response = client.execute(request);

If you debug this (with a real URL of course), you'll notice that your app kind of freezes during client.execute(). 如果您对此进行调试(当然使用真实的URL),则会注意到您的应用在client.execute()期间冻结。 This is the point at which the request has actually fired and the app is waiting for a response. 这是请求实际触发并且应用程序正在等待响应的地方。 Once you actually get the response, it isn't very difficult to get the data out of it. 实际获得响应后,从中获取数据并不是很困难。

Once you understand this, you'll want to get to know AsyncTask, which is endlessly useful for performing background tasks. 了解这一点后,您将需要了解AsyncTask,它对于执行后台任务无穷无尽。 You can find the documentation here: http://developer.android.com/reference/android/os/AsyncTask.html There is a great example of how to use this right at the top. 您可以在以下位置找到文档: http : //developer.android.com/reference/android/os/AsyncTask.html顶部有一个很好的示例,说明了如何使用此文档。

Using these two concepts together you can perform asynchronous HTTP requests. 结合使用这两个概念,您可以执行异步HTTP请求。 Basically, put your actual HTTP execute code in doInBackground of your AsyncTask. 基本上,将实际的HTTP执行代码放入AsyncTask的doInBackground中。 At the end of the doInBackground return your response, and then do what you want with your data in the AsyncTask's onPostExecute. 在doInBackground的最后,返回您的响应,然后对AsyncTask的onPostExecute中的数据执行所需的操作。

We've found that providing a proper RESTful web API that hits the database on the backend in whatever language you choose (be it PHP, RoR, whatever) provides a useful interface for any number of uses (your own website, mobile apps, etc). 我们发现,提供适当的RESTful Web API,可以使用您选择的任何语言(无论是PHP,RoR还是其他语言)在后端数据库上提供数据库,可为多种用途(您自己的网站,移动应用等)提供有用的界面)。

Then it's a matter of your Android app interacting with the RESTful API, which is simply HTTP requests. 然后,这就是您的Android应用程序与RESTful API(仅是HTTP请求)进行交互的问题。 Those can be encapsulated in helper classes to make them straightforward as well. 可以将它们封装在帮助器类中,以使其变得简单明了。

Based on my experience, the best framework for doing RESTFul things with Android is: Spring Android 根据我的经验,使用Android执行RESTFul的最佳框架是: Spring Android

From a client perspective, it provides all the tools needed to access secure RESTFul services. 从客户端的角度来看,它提供了访问安全RESTFul服务所需的所有工具。 Since it is Spring, it provides nice abstractions over most of the boiler plate http code. 由于它是Spring,因此它为大多数样板HTTP代码提供了很好的抽象。 As an example, it provides a clean way to perform a GET that returns json, and then serialize that to a POJO. 例如,它提供了一种干净的方法来执行返回json的GET,然后将其序列化为POJO。

As an example: 举个例子:

RestTemplate restTemplate = new RestTemplate();

// Add Jackson JSON Message Converter to Template
restTemplate.setMessageConverters(
    new ArrayList<HttpMessageConverter<?>>() {
        {              
            add(new MappingJacksonHttpMessageConverter());
        }
    }
);  

// Simple Conversion - pojo is now populated
MyPojo pojo = restTemplate.getForObject(url, MyPojo.class);

The approach you mention in the question: PHP on the server and JSON for requests/responses, does work. 您在问题中提到的方法是可行的:服务器上的PHP和用于请求/响应的JSON。 But getting it perfect can be tricky. 但是,使其变得完美可能会很棘手。

I found it helpful to have small request/reponse classes for each call on the Android side, like SaveNoteToServerRequest, SaveNoteToServerResponse classes which are just plain java objects with whatever fields are needed for the request/response. 我发现为Android端的每个调用设置较小的请求/响应类很有帮助,例如SaveNoteToServerRequest,SaveNoteToServerResponse类,它们都是纯Java对象,具有请求/响应所需的任何字段。 Then you can use a library like GSON to convert the request object to JSON and convert the http response from JSON to the response object. 然后,您可以使用GSON之类的库将请求对象转换为JSON,并将http响应从JSON转换为响应对象。

On the PHP side you can create a small class for the response object, then json_encode at the end. 在PHP方面,您可以为响应对象创建一个小类,然后在末尾创建json_encode。

That way you're not directly manipulating JSON objects, just using your own plain java objects or php classes most of the time. 这样,您就不会直接操纵JSON对象,而是大多数时候仅使用自己的纯java对象或php类。

Hope that helps. 希望有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM