我的代码有问题。我已经创建了一个从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”}]}
但是当我打开应用程序时,微调器不会显示数据。我不知道为什么。以下是我的代码
爪哇岛
公共类 MainActivity 扩展 AppCompatActivity 实现 Spinner.OnItemSelectedListener{
private Spinner spinner;
private ArrayList<String> students;
//JSON Array
private JSONArray result;
private TextView textViewName;
private TextView textViewCourse;
private TextView textViewSession;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Initializing the ArrayList
students = new ArrayList<String>();
spinner = findViewById(R.id.spinner);
spinner.setOnItemSelectedListener(this);
textViewName = findViewById(R.id.textViewName);
textViewCourse = findViewById(R.id.textViewCourse);
textViewSession = findViewById(R.id.textViewSession);
getData();
}
private void getData(){
StringRequest stringRequest = new StringRequest(Config.DATA_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
JSONObject j = null;
try {
j = new JSONObject(response);
result = j.getJSONArray(Config.JSON_ARRAY);
getStudents(result);
} catch (JSONException e) {
e.printStackTrace();
}
}
慕姐8265434