我正在使用来自mySQL的数据来填充android studio上的列表视图。我希望根据将数据上传到mysql的用户来过滤此数据,这样用户只能看到该特定用户添加的播放器。
以下php和android studio代码不会向列表视图返回任何内容。
<?php
include 'conn.php';
$people= array();
$organise= $_GET['user_id'];
$sql= "SELECT name FROM panel WHERE user_id='$organise'";
$stmt= $conn->prepare($sql);
$stmt->execute();
$stmt->bind_result($name);
while ($stmt->fetch()) {
$temp= [
'name'=>$name
];
array_push($people,$temp);
}
echo json_encode($people);
?>
以下代码包含在Panel.java类中
public class Panel extends AppCompatActivity {
TextView etPlayerName, etPlayerTel, etPlayerEmail;
Button btnPlayerAdd;
ListView lvNames;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_panel);
etPlayerName = findViewById(R.id.etPlayerName);
etPlayerTel = findViewById(R.id.etPlayerTel);
etPlayerEmail = findViewById(R.id.etPlayerEmail);
btnPlayerAdd = findViewById(R.id.etPlayerAdd);
lvNames=(ListView) findViewById(R.id.lvNames);
getJSON("http://channan06.lampt.eeecs.qub.ac.uk/summer/crap.php");
}
//this method is actually fetching the json string
private void getJSON(final String urlWebService) {
class GetJSON extends AsyncTask<Void, Void, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
try {
loadIntoListView(s);
}catch (JSONException e){
e.printStackTrace();
}
}
@Override
protected String doInBackground(Void... voids) {
try {
//creating a URL
URL url = new URL(urlWebService);
//Opening the URL using HttpURLConnection
HttpURLConnection con = (HttpURLConnection) url.openConnection();
//StringBuilder object to read the string from the service
StringBuilder sb = new StringBuilder();
//We will use a buffered reader to read the string from service
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
//A simple string to read values from each line
String json;
//reading until we don't find null
while ((json = bufferedReader.readLine()) != null) {
//appending it to string builder
sb.append(json + "\n");
}
//finally returning the read string
return sb.toString().trim();
} catch (Exception e) {
return null;
}
}
}
//creating asynctask object and executing it
GetJSON getJSON = new GetJSON();
getJSON.execute();
}
private void loadIntoListView(String json) throws JSONException{
JSONArray jsonArray= new JSONArray(json);
String [] people= new String[jsonArray.length()];
for(int i=0;i<jsonArray.length();i++){
JSONObject obj= jsonArray.getJSONObject(i);
people[i]=obj.getString("name");
}
ArrayAdapter<String> arrayAdapter= new ArrayAdapter<>(this, android.R.layout.simple_list_item_1,people);
lvNames.setAdapter(arrayAdapter);
}
public void onAddPlayer(View v) {
if (etPlayerName.getText().toString().isEmpty() || etPlayerTel.getText().toString().isEmpty()
|| etPlayerEmail.getText().toString().isEmpty()) {
Toast.makeText(Panel.this, "Please enter all details", Toast.LENGTH_SHORT).show();
} else {
String str_name = etPlayerName.getText().toString();
String str_phone = etPlayerTel.getText().toString();
String str_email = etPlayerEmail.getText().toString();
String type = "addplayer";
BackgroundWorker backgroundWorker = new BackgroundWorker(this);
backgroundWorker.execute(type, str_name, str_phone, str_email);
Toast.makeText(Panel.this, "Player added to database", Toast.LENGTH_SHORT).show();
}
}
}
答案 0 :(得分:0)
您可以使用准备好的方法,但不能使用参数化的查询方法,这是不好的方法。好,看看下面的代码片段。
//I supposed you are using pdo
$organise= $_GET['user_id'];
$stmt = $conn->prepare("SELECT name FROM panel WHERE user_id=:user_id");
$stmt->execute(['user_id' => $organise]);
$data = $stmt->fetchAll();
echo json_encode($data);