2011-12-15 71 views
9

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

编辑:对于那些在我完成解释我的问题之前回复的人,对不起,我发布了意外,并且有些人在我完成编辑之前回答了问题。

回答

3

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

的http:\服务器\方法An Iteraitve \ get_somedata名=东西

和服务器应该在数据库中查询此参数并发送给您的效应初探为JSON。 解析json并获取您的详细信息。

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

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所

+0

如何确保我在C#中的web服务实际上以JSON格式发送数据? – Dazzmaster1 2011-12-28 07:17:54

2

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

如果您可以修改您的桌面应用程序,直接实施HTTP服务。 因此,只有在您的应用程序启动后才能使用该服务。

正如AnDro所说,你可以选择json,它将成为数据传输的打火机。 如果您选择json techno,请看jackson

0

也建议其接受JSON被张贴一个RESTful API发送的JSON - 并返回JSON结果。

您也可以添加URL重写,它将URL分派给各个控制器。

例如http // somehost.com/controller/action ...您可以在其中张贴。

这可以在ASP.NET中完成(最近刚刚在PHP中编写了类似的东西)。

相关问题