我正在尝试从使用 xampp mysql 创建的 localhost 数据库中检索数据。
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/htdocs 中的 jasonscript.php
<?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();
?>
我已在清单文件中授予 Internet 权限。
提前致谢。