0

So i'm a web developer by trade but am making an app at the moment.

I've got a program on my PC which sends data from my PC to a database on my webhost.

What I'm after now, is to create an app that is able to download that data with a series of queries.

Are there any good resources around that would help me to develop some Java code, to connect to a server through my tablet when I'm out and about and download the information from my webhost when on a different wifi network?

Thanks chaps, any help is appreciated.

UKPixie
  • 151
  • 1
  • 2
  • 7
  • 1
    My approach is to run the query > convert results to JSON both on the server side. Then in Java I establish the connection > use GET to obtain the JSON data > parse the JSONarray > then either store locally in SQLite or just as variables, depending on my use. – jnthnjns Oct 25 '12 at 15:01

1 Answers1

1

The way I handle it is that I just create a web page based on what I want to do. For example if I want to verify a login on an app I create a page that takes in a username and password. On the device I just post the username and password values by using a HTTP post transaction.

Example:

public boolean LoginUser()
{
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("your URL comes here...");

    try
    {
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        DataCrypt crypt = new DataCrypt();

        try
        {
            this.Cryptemail = crypt.bytesToHex(crypt.encrypt(this.Email));;
            this.Cryptpass = crypt.bytesToHex(crypt.encrypt(this.Password));
        }
        catch (Exception e)
        {
            DebugLog.log(e.getMessage());
        }

        nameValuePairs.add(new BasicNameValuePair("Password", this.Cryptpass));
        nameValuePairs.add(new BasicNameValuePair("Mail", this.Cryptemail));

        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        // Execute HTTP Post Request
        BasicHttpResponse response = (BasicHttpResponse) httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        String response = EntityUtils.toString(entity);


      return true;
    }
    catch (Exception e)
    {
        DebugLog.log(e.getMessage());
        return false;
    }

    return false;
}

EDIT: So you can just make a page that takes in some parameters that you want to base your query on and return the result in some way (for example by XML).

Hope this helps you out! :)

Araw
  • 2,410
  • 3
  • 29
  • 57
  • Word of warning, [Google suggests](http://android-developers.blogspot.com/2011/09/androids-http-clients.html) using `HttpURLConnection` rather the `HttpPost` for future support reasons: "For Gingerbread and better, `HttpURLConnection` is the best choice. Its simple API and small size makes it great fit for Android. Transparent compression and response caching reduce network use, improve speed and save battery. New applications should use `HttpURLConnection;` it is where we will be spending our energy going forward." – jnthnjns Oct 25 '12 at 15:04
  • **However** [read this disclaimer](http://stackoverflow.com/a/11583328/1134705) to `HttpURLConnection` timeouts. Better to be informed. – jnthnjns Oct 25 '12 at 15:07
  • No problem, just to be clear I am not saying you or the OP need to avoid/replace existing `HttpPost` code. In fact my application still uses it. Just wanted to share knowledge, for future proofing, that I recently came across. I'll eventually convert over. :) – jnthnjns Oct 25 '12 at 15:16
  • Thanks for the quick responses guys but am a little confused if this would infact work for what I want. I'll reiterate what I want to do just to confirm. I've got a list of names and numbers that are automatically updated on my web host from my PC. I then want to build and app using SQL that would download that information into a table view on an app. Is this what the above codes for? Thanks again. – UKPixie Oct 25 '12 at 16:15
  • @PiranSmith : The code above can be used to get data from a server. How you want to process the data is up to you. String response = EntityUtils.toString(entity); will contain the information that you print out on the screen on your webpage (for example if you perform a SELECT on the database and print it out). So you can for example print out the data in a XML-structured way. Then the respons string will contain a XML-string which you can parse using a XML-parser. You can also go for a JSON-approach if you like, or (the hardest way in my opinion) you can create your own type of parser. – Araw Oct 25 '12 at 16:51
  • Do I need any imports with this? Errors with the HTTPclient, post etc. – UKPixie Oct 26 '12 at 14:13