Dazzmaster1
Dazzmaster1

Reputation: 169

Best way to access database on server using Android Application

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.

Upvotes: 9

Views: 13637

Answers (2)

AD14
AD14

Reputation: 1218

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

Upvotes: 3

Christophe Debove
Christophe Debove

Reputation: 6296

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.

Upvotes: 2

Related Questions