android 片段中的 Volley 请求队列,未在文本视图中设置 json 数组数据
Volley request Queue in android fragment, not setting json array data in text view
我正在尝试将从 json 数组解析的值插入到文本视图中。当我 运行 调试器上的应用程序时,似乎我想要的所有信息都在那里,但它没有在文本视图中设置(文本视图返回空白)。
我认为这可能是因为我试图 运行 片段中的 Volley 请求队列,同时错误地调用了上下文。
mQueue = Volley.newRequestQueue(getActivity().getApplicationContext());
但是我还在学习,所以我不确定如何修改它。下面是完整的 class 代码。
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);
}
}
任何关于我哪里出错的反馈都将不胜感激,我在这里很迷茫。
全局声明 String parsed;
并在 JsonObjectRequest
的 for
循环内分配值,在循环外分配 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);
}
}
我正在尝试将从 json 数组解析的值插入到文本视图中。当我 运行 调试器上的应用程序时,似乎我想要的所有信息都在那里,但它没有在文本视图中设置(文本视图返回空白)。
我认为这可能是因为我试图 运行 片段中的 Volley 请求队列,同时错误地调用了上下文。
mQueue = Volley.newRequestQueue(getActivity().getApplicationContext());
但是我还在学习,所以我不确定如何修改它。下面是完整的 class 代码。
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);
}
}
任何关于我哪里出错的反馈都将不胜感激,我在这里很迷茫。
全局声明 String parsed;
并在 JsonObjectRequest
的 for
循环内分配值,在循环外分配 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);
}
}