對,只是想做一個快速的感謝把我放在正確的軌道上。剛剛有一個代碼工程EUREKA時刻,非常高興。我沒有使用過JSON,但是我已經設法通過HTTP-POST和一點PHP將Android中的變量傳遞給SQL。
我敢肯定這不是推薦意識形態的原因很多,雖然原型和演示文稿,它會做得很好!
下面是針對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());
}
這裏是爲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)");
此代碼的工作,非常簡單。接下來的測試將會發現它是否適合大量的字符串。
我希望這對其他人有幫助。
謝謝,我現在就開始研究它。大講座......現在看... – 2011-05-29 08:45:17