java - android片段中的Volley请求队列,不在 TextView 中设置json数组数据

标签 java json parsing android-fragments android-volley

我正在尝试将从 json 数组解析的值插入到 TextView 中。当我在调试器上运行应用程序时,似乎我想要的所有信息都在那里,但它没有在 TextView 中设置( TextView 返回空白)。

我认为这可能是因为我试图在片段中运行 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;,并在JsonObjectRequestfor循环内赋值,并在循环外赋值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);
   }
 }

关于java - android片段中的Volley请求队列,不在 TextView 中设置json数组数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51644082/

相关文章:

java - 由于 JPanel 大小固定,GridBagLayout 忽略权重分布

java - 学习 EJB 的资源

ios - 使用 Flickr API 时的 Alamofire AFError

java - 如何在 JAX-RS 中自定义关系 OneToMany 的 JSON 输出

c++ - 如何使用 clang API 解析函数指针的 typedef 以获取函数指针名称?

python - 使用 BeautifulSoup 在 Python 中抓取缺货通知程序

swift - 如何在 MacOS (Swift) 上解析 iBeacon

java - 带有 Java 8 Streams 的 Spring Data JPA 存储库

Java ArrayList.add() 方法对于纯并行添加线程安全吗?

javascript - 两个不同的表,具有 2 个不同的 AJAX JSON 响应数据