2011-12-15 21 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中編寫了類似的東西)。

相關問題