我是Java Coding的初学者,目前正在编写Android项目代码。现在,我正面临着一个问题。检索到的数据未显示在textview中。
我的计划是使用意图中的车辆号码检索详细信息。之后,详细信息将显示在textviews中。我已经检查过,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);
}
}
答案 0 :(得分:2)
而不是像
那样使用追加
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");
使用设置文字
resultSessionID = (TextView) findViewById(R.id.parkingsessionID);
resultSessionID.setText(parkingsession_id);
resultPartial = (TextView) findViewById(R.id.details1);
resultPartial.setText("Session Start:" + datetime_start + "\n"+
"Session End:" + datetime_end + "\n"+
"Duration:" + duration + "\n"+
"Total Charges:" + charges + "\n");
尝试将数据(即您的vehicle_no)作为地图值发送。因此,您可以通过updateVehicleInfo()vehicle_no作为额外的意图。
public void updateVehicleInfo(final String veh_number)
{
StringRequest stringRequest = new StringRequest(Request.Method.POST, URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Toast.makeText(getContext(), "Update Response"+response, Toast.LENGTH_LONG).show();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String,String> params = new HashMap<>();
params.put(KEY_VEHICLENO, veh_number);
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(getContext());
requestQueue.add(stringRequest);
}