【问题标题】:How would I create an android app with an array stored on a web server using android studio?如何使用 android studio 创建一个带有存储在 Web 服务器上的数组的 android 应用程序?
【发布时间】:2019-07-11 16:42:28
【问题描述】:

我正在尝试制作一个“淋浴想法”应用程序。它具有存储在数组中的三个类别。在每个下面嵌套了用户可以看到的用户帖子的实际列表(作为数组)。当然,用户将无法看到其他帖子。我有一个用作网络服务器的 rasbpi。我想这样做的方法是在 Web 服务器上编写一些 javascript 代码,其中包含两个函数:一个将用户帖子添加到数组中(输入参数是帖子本身),另一个返回数组,以便应用程序可以更新。但是,我不知道如何从应用程序调用 Web 服务器上的这个 js 文件。我该怎么做?

【问题讨论】:

    标签: javascript java android android-studio raspberry-pi


    【解决方案1】:

    您必须为此构建一个 Web API。您可以在 Android 中使用 NodeJS on the serversend requests to this API 来完成此操作

    【讨论】:

    • 如何在 eht 服务器上运行它?节点服务器只是说它找不到名为 server 的文件夹及其中的文件。
    【解决方案2】:

    基本上你要做的是创建一个简单的服务器-客户端架构。

    有多种方法可以做到这一点。我将解释使用NodeJS 作为服务器和客户端(android)端的截击。

    首先,您可以使用Volley,从android 创建一个API 调用,该调用将与NodeJS API 交互,这反过来将创建数组并将其存储为SQL 或您想要的任何其他形式。

    作为参考,您可以查看这两个项目。 这是一个Android app,它使用 Volley 与服务器通信。 这是NodeJS Server,它是一个简单的 REST API。

    如果您不知道,也可以使用此link 来学习有关 NodeJS 的基础知识。

    这是创建服务器然后向客户端发送响应的基本 NodeJS 代码。

    var http = require('http');
    
    //create a server object:
    http.createServer(function (req, res) {
      res.write('Hello World!'); //write a response to the client
      res.end(); //end the response
    }).listen(8080); //the server object listens on port 8080
    

    这是向服务器发出请求的基本 Android Volley 代码。

    // Instantiate the RequestQueue.
    RequestQueue queue = Volley.newRequestQueue(this);
    String url ="http://www.google.com";
    
    // Request a string response from the provided URL.
    StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
                new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            // Display the first 500 characters of the response string.
            textView.setText("Response is: "+ response.substring(0,5));
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            textView.setText("That didn't work!");
        }
    });
    
    // Add the request to the RequestQueue.
    queue.add(stringRequest);
    

    如果您不理解这些代码中的任何一个,可以在下面发表评论。

    【讨论】:

      猜你喜欢
      • 2015-11-22
      • 1970-01-01
      • 2015-05-21
      • 2013-03-14
      • 2013-06-17
      • 1970-01-01
      • 2015-03-23
      • 2016-08-24
      • 1970-01-01
      相关资源
      最近更新 更多