如何建立web服务休息处理使用Java数据库 [英] how to build Rest webservice dealing with database using java

查看:110
本文介绍了如何建立web服务休息处理使用Java数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要让应用程序服务器从我的Andr​​oid应用程序获取数据并将其存储到数据库中。

i want to make Application server to get data from my android application and store it into database.

和还需要这台服务器在线24H

and also need to make this server online 24H

我在java关于网络服务的一个很好的知识和基础知识
但我不知道该怎么做,或者我应该知道的?

i have a good knowledge in java and basics about web-services but i don't know how to do that or what i should know?

任何一个可以指导我,请
谢谢

any one can guide me please thanks

推荐答案

首先,你需要使用一些提供框架developt RESTful服务。少数名是 RestEasy的眼色泽西等。经过你获得一些信心,用知识来开发使用Java的RESTful服务(这将是Web应用程序)。在这个应用程序,您可以连接到数据库或做任何你想要像Web应用程序。现在,当这个已经准备好,可以存放任何web服务器上,并使其可24小时像任何其他Java Web应用程序。出于测试目的,你可以使用邮递员这是Chrome应用。当你能够成功地从邮递员或任何其他应用程序使用它,你可以向前消耗从Android应用程序的服务。
有许多方法来做到这一点从Android的。我的preference使用弹簧Android的休息库。 Android版可能不会让你直接调用REST风格的服务,所以您可能需要在android系统学习的AsyncTask 。从扩展的AsyncTask ,你将覆盖 doInBackground()方法,从中你会调用RESTful服务的任何类。了解您所使用的库中的API文档。您可以覆盖另一种方法 onPostExecute()对后处理同一类。

First of all, you need to developt restful service using some provider frameworks. Few to name are resteasy,wink,Jersey,Spring etc. After you gain some confidence, use the knowledge to develop the restful service using java ( this will be web application). In this application, you can connect to database or do whatever you want like web application. Now when this is ready, you can host it on any web-server and make it available 24 hrs like any other java web application. For testing purpose you can use postman which is chrome app. When you are successfully able to consume it from postman or any other app , you can move forward to consume the service from android app. There are many way to do that from android. My preference is using spring-android-rest libraries. Android may not let you call restful service directly , so you might need to learn about AsyncTask in android. From any class that extend AsyncTask, you will override doInBackground() method from which you will call the restful service. Learn the API docs for the library you use. You can override another method onPostExecute() on the same class for post processings.

在Android样品code将是这样的:

sample code on android will be something like :

@Override
    protected YourResponse doInBackground(Object... args) {
         final String url ="YOUR_URI";
          RestTemplate restTemplate = new RestTemplate();
          restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
          response= restTemplate.postForObject(url, RequestObject, YourResponse.class)   ;
          System.out.println(response.toString());
          return response;
    }



@Override
    protected void onPostExecute(Object args) {

    }

这篇关于如何建立web服务休息处理使用Java数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆