猿问

Android / MySQL:微调器不填充来自 MySQL 的数据

我的代码有问题。我已经创建了一个从 MySQL 数据库填充的微调器。PHP 代码似乎完全没有问题,因为我运行链接“localhost/latihan1/menu/php”,将显示 json 字符串。

json显示如下:

{"result":[{"username":"haha","name":"Bus Bisnis","course":"math","session":"20119"},{"username":"hihi", "name":"Bus Ace","course":"fizik","session":"12817"},{"username":"m_ridwan","name":"Ridwan","course":"Komputer" ,"session":"1920"},{"username":"m_iqbal","name":"Iqbal","course":"Sains","session":"2021"}]}

但是当我打开应用程序时,微调器不显示数据。我不知道为什么。下面是我的代码

JAVA

公共类 MainActivity 扩展 AppCompatActivity 实现 Spinner.OnItemSelectedListener{

<?php 


    require_once "koneksi.php";


    $query = mysqli_query($con, "SELECT * FROM kendaraan ORDER BY id ASC");


    $students = array();    


    while($row = mysqli_fetch_array($query)){

        array_push($students,array(

            'username'=>$row['username'],

            'name'=>$row['name'],

            'course'=>$row['course'],

            'session'=>$row['session']

        ));

    }


    echo json_encode(array('result'=>$students));

    mysqli_close($con);

?>


哆啦的时光机
浏览 131回答 1
1回答

呼如林

我在我的 android studio 中执行了你的代码。我看起来您的网络调用无法获取数据(结果仍然为空)。检查我的代码以供参考。我用静态数据替换了您的网络调用。package com.attiq.testapp;import android.os.Bundle;import android.view.View;import android.widget.AdapterView;import android.widget.ArrayAdapter;import android.widget.Spinner;import android.widget.TextView;import androidx.appcompat.app.AppCompatActivity;import com.google.gson.Gson;import com.google.gson.JsonArray;import com.google.gson.JsonObject;import org.json.JSONArray;import org.json.JSONException;import org.json.JSONObject;import java.util.ArrayList;public class Main5Activity extends AppCompatActivity implements Spinner.OnItemSelectedListener {private Spinner spinner;private ArrayList<String> students;//JSON Arrayprivate JSONArray result;private TextView textViewName;private TextView textViewCourse;private TextView textViewSession;@Overrideprotected void onCreate(Bundle savedInstanceState) {&nbsp; &nbsp; super.onCreate(savedInstanceState);&nbsp; &nbsp; setContentView(R.layout.activity_main5);&nbsp; &nbsp; //Initializing the ArrayList&nbsp; &nbsp; students = new ArrayList<String>();&nbsp; &nbsp; spinner = findViewById(R.id.spinner);&nbsp; &nbsp; spinner.setOnItemSelectedListener(this);&nbsp; &nbsp; textViewName = findViewById(R.id.textViewName);&nbsp; &nbsp; textViewCourse = findViewById(R.id.textViewCourse);&nbsp; &nbsp; textViewSession = findViewById(R.id.textViewSession);&nbsp; &nbsp; getData();}private void getData(){&nbsp; &nbsp; String json= "{\"result\":[{\"username\":\"haha\",\"name\":\"Bus Bisnis\",\"course\":\"math\",\"session\":\"20119\"},{\"username\":\"hihi\",\"name\":\"Bus Ace\",\"course\":\"fizik\",\"session\":\"12817\"},{\"username\":\"m_ridwan\",\"name\":\"Ridwan\",\"course\":\"Komputer\",\"session\":\"1920\"},{\"username\":\"m_iqbal\",\"name\":\"Iqbal\",\"course\":\"Sains\",\"session\":\"2021\"}]}";&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; JSONObject jsonObject = new JSONObject(json);&nbsp; &nbsp; &nbsp; &nbsp; result= jsonObject.getJSONArray("result");&nbsp; &nbsp; &nbsp; &nbsp; getStudents(result);&nbsp; &nbsp; } catch (Exception e){&nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; }}private void getStudents(JSONArray j) {&nbsp; &nbsp; for (int i = 0; i < j.length(); i++) {&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JSONObject json = j.getJSONObject(i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; students.add(json.getString(Config.TAG_USERNAME));&nbsp; &nbsp; &nbsp; &nbsp; } catch (JSONException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; spinner.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, students));}private String getName(int position) {&nbsp; &nbsp; String name = "";&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; JSONObject json = result.getJSONObject(position);&nbsp; &nbsp; &nbsp; &nbsp; name = json.getString(Config.TAG_NAME);&nbsp; &nbsp; } catch (JSONException e) {&nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; }&nbsp; &nbsp; return name;}private String getCourse(int position) {&nbsp; &nbsp; String course = "";&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; JSONObject json = result.getJSONObject(position);&nbsp; &nbsp; &nbsp; &nbsp; course = json.getString(Config.TAG_COURSE);&nbsp; &nbsp; } catch (JSONException e) {&nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; }&nbsp; &nbsp; return course;}private String getSession(int position) {&nbsp; &nbsp; String session = "";&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; JSONObject json = result.getJSONObject(position);&nbsp; &nbsp; &nbsp; &nbsp; session = json.getString(Config.TAG_SESSION);&nbsp; &nbsp; } catch (JSONException e) {&nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; }&nbsp; &nbsp; return session;}@Overridepublic void onItemSelected(AdapterView<?> parent, View view, int position, long id) {&nbsp; &nbsp; //Setting the values to textviews for a selected item&nbsp; &nbsp; textViewName.setText(getName(position));&nbsp; &nbsp; textViewCourse.setText(getCourse(position));&nbsp; &nbsp; textViewSession.setText(getSession(position));}@Overridepublic void onNothingSelected(AdapterView<?> parent) {&nbsp; &nbsp; textViewName.setText("");&nbsp; &nbsp; textViewCourse.setText("");&nbsp; &nbsp; textViewSession.setText("");}}
随时随地看视频慕课网APP
我要回答