我正在尝试更多地了解 MySQL 并使用 Java(在 Android 上)从我的 WAMS 服务器上的数据库中访问和检索信息。我的应用程序的设置方式是它有一个初始登录屏幕,它还获取正在登录的用户名的“uid”(来自不同的表)并存储它。
登录后(这是功能性的 - 我设置了一个 toast 通知,显示检索到的用户登录用户名和 uid),它进入一个新屏幕(dashboard.xml),其中设置了一个 TextView 字段来显示检索到的数据(来自下面发布的表)与存储的“uid”相关联。这是我试图从中提取数据的表:
现在,我已经设置了一个 PHP 文件,该文件在我的数据库中查询与特定“uid”关联的行。我已经使用 HTML 表单测试了这个文件。
$connect = mysql_connect($dbhost, $dbuser, $dbpass) or die("connection error");
mysql_select_db($dbdb)or die("database selection error");
//Retrieve the User ID
$uid = $_POST['uid'];
//Query
$query = mysql_query("SELECT * FROM node WHERE uid='$uid' AND type='goal'");
//store # of rows returned
$num_rows = mysql_num_rows($query);
if ($num_rows >= 1) {
while($results=mysql_fetch_assoc($query)) {
//Store the returned data into a variable
$output = $results;
//encode the returned data in JSON format
echo json_encode($output);
}
mysql_close();
}
我通过使用 uid 值为 1 测试 PHP 文件得到的结果是:
{"nid":"1","vid":"1","type":"goal","language":"","title":"test","uid":"1","status ":"1","created":"1342894493","changed":"1342894493","comment":"2","promote":"1","moderate":"0","sticky": "1","tnid":"0","翻译":"0"}
{"nid":"2","vid":"2","type":"goal","language":"","title":"test2","uid":"1","status ":"1","created":"1342894529","changed":"1342894529","comment":"2","promote":"1","moderate":"0","sticky": "1","tnid":"0","翻译":"0"}
{"nid":"5","vid":"5","type":"goal","language":"","title":"run","uid":"1","status ":"1","created":"1343506987","changed":"1343506987","comment":"2","promote":"1","moderate":"0","sticky": "1","tnid":"0","翻译":"0"}
{"nid":"9","vid":"9","type":"goal","language":"","title":"跑到山上","uid":"1" “状态”:“1”,“已创建”:“1343604338”,“已更改”:“1343605100”,“评论”:“2”,“提升”:“0”,“中等”:“0”,粘性":"0","tnid":"0","翻译":"0"}
现在,我已经编写了一些 android 代码来设置 httppost 并应该在我的数据库表中检索“标题”。我知道这是错误的(显然因为它不起作用),但我对下一步该做什么感到困惑。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class Dashboard extends Activity implements OnClickListener {
// variable declarations
String uid = "1";
// create textview to display retrieved data
TextView display;
HttpClient httpclient;
HttpPost httppost;
HttpResponse httpresponse;
HttpEntity httpentity;
ArrayList<NameValuePair> resultArray;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dashboard);
display = (TextView) findViewById(R.id.test);
// initialize HttpClient
httpclient = new DefaultHttpClient();
// initialize HttpPost
httppost = new HttpPost("http://192.168.1.112/android/fetch.php");
try {
// Create new List
List<NameValuePair> resultList = new ArrayList<NameValuePair>();
resultList.add(new BasicNameValuePair("uid", uid));
httppost.setEntity(new UrlEncodedFormEntity(resultList));
httpresponse = httpclient.execute(httppost);
httpentity = httpresponse.getEntity();
InputStream instream = entity.getContent();
try {
// store incoming stream in an array
JSONArray jArray = new JSONArray(streamToString(instream));
JSONObject jData = null;
for (int i = 0; i < jArray.length(); i++) {
jData = jArray.getJSONObject(i);
String goals = jData.getString("title");
display.setText(goals);
}
//} catch (JSONException e) {
//Toast.makeText(this, "No entries found", Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(this, e.toString(), Toast.LENGTH_LONG)
.show();
}
} catch (Exception e) {
e.printStackTrace();
Notifications error = new Notifications();
error.userPassErrorDialog();
}
}
private static String streamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
在 Android 模拟器中测试时出现以下错误:
任何帮助或建议将不胜感激。