Java android片段中的截击请求队列,未在文本视图中设置json数组数据
我试图将从json数组解析的值插入到文本视图中。当我在调试器上运行应用程序时,似乎所有我想要的信息都在那里,但是它在文本视图中没有设置(文本视图返回空白) 我想这可能是因为我试图在一个片段中运行截击请求队列,同时错误地调用了上下文Java android片段中的截击请求队列,未在文本视图中设置json数组数据,java,json,parsing,android-fragments,android-volley,Java,Json,Parsing,Android Fragments,Android Volley,我试图将从json数组解析的值插入到文本视图中。当我在调试器上运行应用程序时,似乎所有我想要的信息都在那里,但是它在文本视图中没有设置(文本视图返回空白) 我想这可能是因为我试图在一个片段中运行截击请求队列,同时错误地调用了上下文 mQueue = Volley.newRequestQueue(getActivity().getApplicationContext()); 然而,我仍在学习,所以我不确定如何解决这个问题。下面是完整的课程代码 public class BookingHistor
mQueue = Volley.newRequestQueue(getActivity().getApplicationContext());
然而,我仍在学习,所以我不确定如何解决这个问题。下面是完整的课程代码
public class BookingHistoryFragment extends Fragment {
private TextView bookingHistoryTV;
private RequestQueue mQueue;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_booking_history, container, false);
bookingHistoryTV = view.findViewById(R.id.bookingHistoryTV);
mQueue = Volley.newRequestQueue(getActivity().getApplicationContext());
jsonParse();
return inflater.inflate(R.layout.fragment_booking_history, container, false);
}
private void jsonParse() {
String url = "http://178.128.166.68/getBookingHistory.php";
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("bookingHistory");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject bookingHistory = jsonArray.getJSONObject(i);
String fromLocality = bookingHistory.getString("fromLocality");
String toLocality = bookingHistory.getString("toLocality");
double cost = bookingHistory.getDouble("cost");
String date = bookingHistory.getString("date");
String parsed = fromLocality + " | " + toLocality + " | " + String.valueOf(cost) + " | " + date + "\n\n";
bookingHistoryTV.append(parsed);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
mQueue.add(request);
}
}
公共类BookingHistoryFragment扩展片段{
私人文本视图预订历史电视;
专用请求队列MQUE;
@可空
@凌驾
创建视图时的公共视图(LayoutFlater充气机、@Nullable ViewGroup容器、@Nullable Bundle savedInstanceState){
视图=充气机。充气(R.layout.fragment\u booking\u history,container,false);
bookingHistoryTV=view.findViewById(R.id.bookingHistoryTV);
mQueue=Volley.newRequestQueue(getActivity().getApplicationContext());
jsonParse();
返回充气机。充气(R.layout.fragment\u booking\u history,container,false);
}
私有void jsonParse(){
字符串url=”http://178.128.166.68/getBookingHistory.php";
JsonObjectRequest=新的JsonObjectRequest(request.Method.GET,url,null,
新的Response.Listener(){
@凌驾
公共void onResponse(JSONObject响应){
试一试{
JSONArray JSONArray=response.getJSONArray(“bookingHistory”);
for(int i=0;i
任何关于我哪里出错的反馈都将不胜感激,因为我在这里迷路了。声明
字符串已解析全局编码>并为JsonObjectRequest
的循环内的值和setText
循环外的值赋值,如下所示
public class BookingHistoryFragment extends Fragment {
String parsed; //Defined Globally
private TextView bookingHistoryTV;
private RequestQueue mQueue;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_booking_history, container, false);
bookingHistoryTV = view.findViewById(R.id.bookingHistoryTV);
jsonParse();
return inflater.inflate(R.layout.fragment_booking_history, container, false);
}
private void jsonParse() {
String url = "http://178.128.166.68/getBookingHistory.php";
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("bookingHistory");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject bookingHistory = jsonArray.getJSONObject(i);
String fromLocality =
bookingHistory.getString("fromLocality");
String toLocality = bookingHistory.getString("toLocality");
double cost = bookingHistory.getDouble("cost");
String date = bookingHistory.getString("date");
parsed = fromLocality + " | " + toLocality + " | " + String.valueOf(cost) + " | " + date + "\n\n";
}
bookingHistoryTV.setText(parsed); //setText outside of For Loop
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
//creating a request queue
RequestQueue requestQueue = Volley.newRequestQueue(this);
//adding the string request to request queue
mQueue.add(request);
}
}
公共类BookingHistoryFragment扩展片段{
已解析字符串;//全局定义
私人文本视图预订历史电视;
专用请求队列MQUE;
@可空
@凌驾
创建视图上的公共视图(LAYOUTINGER充气机,@Nullable ViewGroup container,
@可为空的捆绑包savedInstanceState){
视图=充气机。充气(R.layout.fragment\u booking\u history,container,false);
bookingHistoryTV=view.findViewById(R.id.bookingHistoryTV);
jsonParse();
返回充气机。充气(R.layout.fragment\u booking\u history,container,false);
}
私有void jsonParse(){
字符串url=”http://178.128.166.68/getBookingHistory.php";
JsonObjectRequest=新的JsonObjectRequest(request.Method.GET,url,null,
新的Response.Listener(){
@凌驾
公共void onResponse(JSONObject响应){
试一试{
JSONArray JSONArray=response.getJSONArray(“bookingHistory”);
for(int i=0;i
Try,在添加mQueue
之前调用RequestQueue
使其工作,这是将其设置在建议的错误位置和错误返回视图的组合。非常感谢你的帮助。