java 如何创建与 Android 应用程序通信的服务器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7136220/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 18:46:25  来源:igfitidea点击:

How to create a server for communication with an Android app

javaphpandroidtrial

提问by asd2005

I need to create a server which initially checks an Android applications trial period, so

我需要创建一个最初检查 Android 应用程序试用期的服务器,所以

  • on running an Android application the phones info is sent over to ther server.
  • The server stores this information.
  • Whenever the application is opened within that android phone
  • the server checks its trial period has or has not expired.
  • 在运行 Android 应用程序时,手机信息会发送到服务器。
  • 服务器存储此信息。
  • 每当应用程序在该 Android 手机中打开时
  • 服务器检查其试用期是否已到期。

In a way it's like an Android time bomb.

在某种程度上,它就像一个 Android 定时炸弹。

I'm completely new to servers and really just want a basic, secure, server that simply registers a phone and compares it against a trial limit.

我对服务器完全陌生,真的只想要一个基本的、安全的服务器,只需注册电话并将其与试用限制进行比较。

Anyone have any information on how I could do this? Any code examples or tutorials are very much appreciated.

任何人都有关于我如何做到这一点的任何信息?非常感谢任何代码示例或教程。

回答by momo

You didn't specify your server technology, but in principal you need to do the following:

您没有指定您的服务器技术,但原则上您需要执行以下操作:

  1. You probably want to expose them as a REST Webservice. All you need is a GET operation to basically figure out if the trial has expired or not. Since you are using Android and have gained familiarity with Java, I suggest you look at JAX-RSwhich is one way to implement REST in Java. If you are familiar with other language, then feel free to go for that.
  2. The simplest form of your GET URL would probably look like http://yoursite/getTrial/[beginTrialDate]where [beginTrialDate] is a date in millis since Jan 1, 1970 GMT (standard approach)
  3. On the server side, you simply took the [beginTrialDate] and check if it has exceed your trial period by comparing current time to [beginTrialDate] + [trial period]
  4. You would then return a simple JSONresponse containing the information whether the app has expired or not. The simplest form would be: { "hasExpired" : true/false }

  5. You would call this WebService in Android using HttpClient as you would probably know already. Check this HTTP Client Tutorial

  6. You could make the server more robust by storing the phone identifier and your GET URL change to http://yoursite/getTrial/[phoneID]. The only additional complexity is you have to look up the begin trial date by phoneID and then compare it again using the step #4

  1. 您可能希望将它们公开为 REST Web 服务。您所需要的只是一个 GET 操作,以基本上确定试用期是否已过期。由于您使用的是 Android 并且已经熟悉 Java,我建议您查看JAX-RS,这是在 Java 中实现 REST 的一种方法。如果您熟悉其他语言,那么请随意选择。
  2. GET URL 的最简单形式可能类似于http://yoursite/getTrial/[beginTrialDate]其中 [beginTrialDate] 是自格林威治标准时间 1970 年 1 月 1 日以来以毫秒为单位的日期(标准方法)
  3. 在服务器端,您只需获取 [beginTrialDate] 并通过将当前时间与 [beginTrialDate] + [试用期] 进行比较来检查它是否已超过您的试用期
  4. 然后,您将返回一个简单的JSON响应,其中包含应用程序是否已过期的信息。最简单的形式是:{ "hasExpired" : true/false }

  5. 您可能已经知道,您将使用 HttpClient 在 Android 中调用此 WebService。检查此HTTP 客户端教程

  6. 您可以通过将电话标识符和您的 GET URL 更改存储为http://yoursite/getTrial/[phoneID]来使服务器更加健壮。唯一额外的复杂性是您必须通过 phoneID 查找开始试用日期,然后使用第 4 步再次比较它

Let me know if you need more clarification and I will add it to the post

如果您需要更多说明,请告诉我,我会将其添加到帖子中

回答by Pavan Kulkarni

Easiest way would be write a JSON service. here is a link to a sample PHP JSON service - http://davidwalsh.name/web-service-php-mysql-xml-jsonYou can easily find JSON code for your choice of language.

最简单的方法是编写一个 JSON 服务。这是一个示例 PHP JSON 服务的链接 - http://davidwalsh.name/web-service-php-mysql-xml-json您可以轻松找到您选择的语言的 JSON 代码。

I'm guessing that you dont need the service to return lot of data - probably a flag or minimal data. You could simply parse through the JSON string that is returned to the device. If you have lot of data to be passed, you could try some free JSON libraries available

我猜您不需要该服务来返回大量数据——可能是一个标志或最少的数据。您可以简单地解析返回到设备的 JSON 字符串。如果您有大量数据要传递,您可以尝试一些可用的免费 JSON 库