Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>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.</p> <p>I'm sure this is not the recommended ideology for many reasons although for prototype and presentation it will do just fine!</p> <p>Here is the code for android:</p> <pre><code>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()); } </code></pre> <p>And here is the code for the PHP:</p> <pre><code>$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)"); </code></pre> <p>This code works, very simple. Next tests will be to find out if it is suitable for a large number of strings. </p> <p>I hope this is helpful to somebody else.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload