【问题标题】:Best way to access database on server using Android Application使用 Android 应用程序访问服务器上数据库的最佳方式
【发布时间】:2011-12-15 09:49:38
【问题描述】:

我需要开发一个可以访问存储在办公室服务器上的 SQL Server 数据库的应用程序。随叫随到的员工远程桌面进入终端服务器,并使用为其创建数据库的桌面应用程序访问数据库。桌面应用程序通过 ODBC 连接访问数据库。 我正在编写一个 android 应用程序,它将允许访问桌面应用程序中可用的该数据库的一小部分,并且我正在寻找将我的应用程序连接到该数据库的方法,无论它是否使用 ODBC 连接(可能使用.Net),或以其他方式。

编辑:抱歉那些在我解释完我的问题之前回复的人,它是偶然发布的,你们中的一些人在我完成编辑之前回答了。

【问题讨论】:

    标签: android sql


    【解决方案1】:

    你必须在服务器端编写一个 web 服务。可以将数据作为 Json 数据包发送到设备,并在设备中解析 json 数据包并访问数据。您对 webservice 的调用应该是 http 调用,例如

    http:\server\metnod\get_somedata?name=something

    并且服务器应该在数据库中查询这个参数并将响应作为 Json 发送给你。 解析 json 并获取您的详细信息。

    编辑: 在服务器响应标头中将内容类型设置为“application/json”。这是客户端向服务器发送 http post 请求的示例。这里的 jsonobjSend 是我构建的 json 发送到服务器的一些细节。例如 {table:"sometable", id:90 }。 jsonobjRecv 是服务器要发送的json

        HttpPost httpPostRequest = new HttpPost(url);
            StringEntity se;
            se = new StringEntity(jsonObjSend.toString());
    
            // Set HTTP parameters
            httpPostRequest.setEntity(se);
            httpPostRequest.setHeader("Authorization", usercredential);
            httpPostRequest.setHeader("Accept", "application/json");
            httpPostRequest.setHeader("Content-type", "application/json");
            httpPostRequest.setHeader("Accept-Encoding", "gzip"); // only set this parameter if you would like to use gzip compression
            long t = System.currentTimeMillis();
            response = (HttpResponse) httpclient.execute(httpPostRequest);
            Log.i(TAG, "HTTPResponse received in [" + (System.currentTimeMillis()-t) + "ms]");
            //Get hold of the response entity (-> the data):
            HttpEntity entity = response.getEntity();
    
            if (entity != null) {
                // Read the content stream
                InputStream instream = entity.getContent();
                Header contentEncoding = response.getFirstHeader("Content-Encoding");
                if (contentEncoding != null && contentEncoding.getValue().equalsIgnoreCase("gzip")) {
                    instream = new GZIPInputStream(instream);
                }
    
                // convert content stream to a String
                String resultString= convertStreamToString(instream);
                Log.v(null, "resultString "+resultString);
                instream.close();
    
    
                // Transform the String into a JSONObject
                if(resultString!=null){
                    jsonObjRecv = new JSONObject(resultString);
    
                }
    
                // Raw DEBUG output of our received JSON object:
                Log.i(TAG,"<jsonobject>\n"+jsonObjRecv.toString()+"\n</jsonobject>");
    
                return jsonObjRecv;
    

    }

    创建/解析一个 json 检查 json.org

    【讨论】:

    • 如何确保我在 C# 中的 web 服务实际上以 JSON 格式发送数据?
    • 我已经编辑了答案。请让我知道它是否适合您
    • 那么,客户端是否确定它接收数据的格式?我不需要在服务器端做任何不同的事情吗?
    • 哦,我刚看到你在代码下面写的。因此,如果我从该网站创建一个格式正确的字符串,我可以在 java 中解析它?
    • 是的。我不熟悉 C#,您可以在 google 中查看如何生成 json 并将其作为响应发送。你可以在这里得到一个想法stackoverflow.com/questions/1056121/…
    【解决方案2】:

    您必须创建一个作为接口的网络服务器,然后您才能为您的应用程序提供网络服务。

    如果您可以修改您的桌面应用程序,请直接为其实现 http 服务。 因此,该服务仅在您的应用程序启动时可用。

    正如 AnDro 所说,您可以选择 json 它将更轻松地传输数据。 如果您选择 json techno,请查看jackson

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-23
      • 2012-10-20
      • 1970-01-01
      • 2011-08-19
      • 2023-03-14
      • 1970-01-01
      • 2016-07-08
      • 1970-01-01
      相关资源
      最近更新 更多