![](/img/trans.png)
[英]Volley Request Queue in Android Fragment (getApplicationContext may produce NullPointerException)
[英]Volley request Queue in android fragment, not setting json array data in text view
我正在尝试将从json数组解析的值插入文本视图。 当我在调试器上运行该应用程序时,似乎我想要的所有信息均已存在,但是未在文本视图中设置(文本视图返回空白)。
我认为可能是因为我试图在一个片段中运行Volley请求队列,同时错误地调用了上下文。
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);
}
}
对于我要去哪里的任何反馈,我们将不胜感激,不胜感激。
声明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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.