1

I'm developing an android application which is to collect data and then send it to a web directory.

So lets say a want to collect an array of data on the phone, and then after clicking a button send it all to the online directory as a file or stream. It does not even need to get a response - although in the future a confirmation would be handy.

Here is a guess at the sort of order of things...

dir = "someurl.com/data/files_received";
Array data;
sendDataSomehow(dir, data); //obv the difficult bit!

I am in very early stages of developing for Android although I have a lot of experience coding web so that bit will be fine.

I have found suggestions for things such as JSON, Google GSON, HTTP POST and GET - do these sound like the right track?

I hope I have been clear enough.

2 Answers 2

2

Yep, JSON would be a good solution for this.

Encode your array as JSON and then send it to your web server as the body of an HTTP POST request. If you have an hour to kill, here's a really good video from Google IO last year explaining how to implement a REST client on Android (what you're doing isn't strictly REST-ful, but the calls you make to the server are very similer): http://www.google.com/events/io/2010/sessions/developing-RESTful-android-apps.html

Sign up to request clarification or add additional context in comments.

Comments

1

Right, just wanted to do a quick thank for putting me on the right track. Just had one of those THE CODE WORKS EUREKA moments, very happy. I haven't used JSON but I have managed to pass a variable from Android to SQL through a HTTP-POST and little bit of PHP.

I'm sure this is not the recommended ideology for many reasons although for prototype and presentation it will do just fine!

Here is the code for android:

try {
        URL url = new URL("http://www.yourwebsite.com/php_script.php");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("POST");
        conn.setDoOutput(true);
        conn.setDoInput(true);
        conn.setUseCaches(false);
        conn.setAllowUserInteraction(false);
        conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

        OutputStream out = conn.getOutputStream();
        Writer writer = new OutputStreamWriter(out, "UTF-8");
        writer.write("stringToPass=I'd like to pass this");
        writer.close();
        out.close();

        if(conn.getResponseCode() != 200)
        {
            throw new IOException(conn.getResponseMessage());
        }

        BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        StringBuilder sb = new StringBuilder();
        String line;
        while ((line = rd.readLine()) != null)
        {
            sb.append(line);
        }
        rd.close();

        conn.disconnect();
    } catch (MalformedURLException e1) {
        textBox.setText(e1.toString());
    } catch (IOException e2) {
        textBox.setText(e2.toString());
    }

And here is the code for the PHP:

$conn = mysql_connect("localhost","web108-table","********") or die (mysql_error());
mysql_select_db("web108-table",$conn) or die (mysql_error());

$str = $_POST['stringToPass'];

mysql_query("INSERT INTO table(field) VALUES ($str)");

This code works, very simple. Next tests will be to find out if it is suitable for a large number of strings.

I hope this is helpful to somebody else.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.