我是 Java 编码的初学者,目前正在编写一个 Android 项目。现在,我面临一个问题。检索到的数据未显示在文本视图中。
我的计划是使用意图中的车辆编号检索详细信息。之后,详细信息将显示在文本视图中。我已经检查过了,php 显示了我需要的信息,这意味着它是有问题的 java。但是,我无法发现错误。请给我建议或提供一些例子。谢谢你。
package com.example.maptesting;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class NextActivity extends Activity {
TextView vehicle_no;
TextView resultSessionID;
TextView resultPartial;
RequestQueue requestQueue;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_next);
vehicle_no = (TextView) findViewById(R.id.vehicle_no);
Intent intent = getIntent();
String veh_no = intent.getStringExtra("vehicleno");
vehicle_no.setText(veh_no);
requestQueue = Volley.newRequestQueue(getApplicationContext());
String url = ("http://mp60.bas-mp.bus/testingtesting/showPartialDetails.php?vehicle_no=" + vehicle_no.getText().toString());
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray students = response.getJSONArray("students");
for (int i = 0; i < students.length(); i++) {
JSONObject parkingsession = students.getJSONObject(i);
String parkingsession_id = parkingsession.getString("parkingsession_id");
String datetime_start = parkingsession.getString("datetime_start");
String datetime_end = parkingsession.getString("datetime_end");
String duration = parkingsession.getString("duration");
String charges = parkingsession.getString("charges");
resultSessionID = (TextView) findViewById(R.id.parkingsessionID);
resultSessionID.append(parkingsession_id);
resultPartial = (TextView) findViewById(R.id.details1);
resultPartial.append("Session Start:" + datetime_start + "\n"+
"Session End:" + datetime_end + "\n"+
"Duration:" + duration + "\n"+
"Total Charges:" + charges + "\n");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
requestQueue.add(jsonObjectRequest);
}
}