9

I need to develop an application which can access an SQL Server database stored on a server in an office. On call staff remote desktop into a terminal server, and access the database using the desktop application for which the Database was made. The desktop application accesses the database with an ODBC connection. I am writing an android application which will allow a small part of the access to this database that is available in the desktop application, and I am looking for ways to connect my application to this database, whether it is using the ODBC connection (Potentially using .Net), or in some other way.

Edit: Sorry for those who replied before I finished explaining my question, it posted by accident and some of you answered before I finished editing.

Dazzmaster1
  • 169
  • 1
  • 2
  • 8

2 Answers2

3

you have to write a webservice on the server side. can send the data as Json packets to the device and in device parse the json packets and access the data. your calls to webservice should be a http call eg

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

and the server should query the database for this parameter and send you the reponse as Json. parse json and get your details.

Edit: set the content-type as "application/json" in the server response header. This is a example for client to send a http post request to the server. here jsonobjSend is the json i have contructed to send to the server with some details. ex {table:"sometable", id:90 }. jsonobjRecv is the json which will be sent by the server

    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;

}

to create/parse a json check json.org

AD14
  • 1,218
  • 19
  • 32
  • How do I ensure that my webservice in C# actually sends the data in the JSON format? – Dazzmaster1 Dec 28 '11 at 07:17
  • i have edited the answer. please let me know if its enough for you – AD14 Dec 28 '11 at 12:15
  • So, does the client determine which format it receives the data? I don't need to do anything different on the server side? – Dazzmaster1 Dec 28 '11 at 12:57
  • Oh, I just saw what you wrote below the code. So if I create a string with the correct format from that website, I can parse it in the java? – Dazzmaster1 Dec 28 '11 at 13:40
  • yes. iam not familar with C# you can check in google how to generate a json and send it as response. you can get an idea here http://stackoverflow.com/questions/1056121/how-to-create-json-string-in-c-sharp – AD14 Dec 29 '11 at 05:16
2

You must create a web server who will be the interface, you can then provide a webservice for your application.

If you can modify your desktop application, implement directly the http service to it. So the service will be avaible only when your application is launched.

As AnDro said you can choose json it will be the lighter to the datas's transfer. If you choose json techno have a look to jackson.

Christophe Debove
  • 6,088
  • 20
  • 73
  • 124