我想從使用xampp mysql創建的localhost數據庫中檢索數據。Url連接android到mysql數據庫
public class JASONUseActivity extends Activity {
EditText byear; // To take birthyear as input from user
Button submit;
TextView tv; // TextView to show the result of MySQL query
String returnString; // to store the result of MySQL query after decoding JSON
/** Called when the activity is first created. */
@Override
protected void onCreate(Bundle savedInstanceState) {
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
.detectDiskReads().detectDiskWrites().detectNetwork() // StrictMode is most commonly used to catch accidental disk or network access on the application's main thread
.penaltyLog().build());
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
byear = (EditText) findViewById(R.id.editText1);
submit = (Button) findViewById(R.id.submitbutton);
tv = (TextView) findViewById(R.id.showresult);
// define the action when user clicks on submit button
submit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// declare parameters that are passed to PHP script i.e. the name birth year and its value submitted by user
ArrayList <NameValuePair> postParameters = new ArrayList <NameValuePair>();
// define the parameter
postParameters.add(new BasicNameValuePair("birthyear", byear.getText().toString()));
String response = null;
// call executeHttpPost method passing necessary parameters
try {
response = CustomHttpClient.executeHttpPost("http://localhost/jasonscript.php", postParameters);
// store the result returned by PHP script that runs MySQL query
String result = response.toString();
我無法在textview中查看數據。網址是否正確?
這裏是位於XAMPP的jasonscript.php/htdocs目錄
<?php
$db_host = "localhost";
$db_uid = "root";
$db_pass = "";
$db_name = "";
$db_con = mysql_connect($db_host, $db_uid, $db_pass) or die('could not connect');
mysql_select_db($db_name);
$sql = "SELECT * FROM people WHERE birthyear > '" . $_POST["birthyear"] . "'";
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result))
$output[] = $row;
print(json_encode($output));
mysql_close();
?>
我已經給網絡許可清單文件。
在此先感謝。
我認爲Android沒有錯誤?你能否確認(可能你沒有得到強制關閉,因爲你有一個try/catch)。如果Anrdoi沒有錯誤 - 檢查你的服務器是否出錯 - 右鍵點擊並轉到apache - 錯誤日誌。如果有任何錯誤,請運行您的應用程序,並從當時的日誌中獲取最後一行。 – 2013-05-08 06:38:14
連接電腦的IP不是本地主機 – 2013-05-08 06:46:00
沒有該應用程序不強制關閉。在Apache錯誤日誌中沒有錯誤。我在logcat中遇到了嚴格的模式錯誤。 – 2013-05-08 08:53:31