androidstudio中的java对象,通过调用restapi构造

brtdzjyr  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(391)

我正在androidstudio中编写一个应用程序,通知用户火车时刻。我有一个名为journey的类,我的想法(不确定这是否是正确的方法,因为我对java和android开发不熟悉)是基于api返回的响应设置属性。对api的调用正在工作,我可以设置属性,但是如何才能使调用类侦听,直到它完成对api的调用并完全加载属性为止。
目前我掌握的代码如下:

public Journey(String startCode, String endCode, Activity activity) {
    //Connect to the API and set up the journey object

updateJourney(startCode,endCode,activity);

}

public void updateJourney(String startCode, final String endCode, Activity activity) {
    queue = Volley.newRequestQueue(activity);
    final Journey journeyClass = this;

JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.GET,URL_LEFT + URL_START + startCode + URL_DEST + endCode,null,
        new Response.Listener<JSONArray>() {
            @Override
            public void onResponse(JSONArray response) {
                Log.d("Response", response.toString());

                if (response.length() != 0) {
                    try {
                        JSONObject returnedJourney = response.getJSONObject(0);
                        journeyClass.setStartCode(returnedJourney.getString("station"));
                        journeyClass.setEndCode(endCode);

                        JSONArray services = returnedJourney.getJSONArray("services");
                        JSONObject firstService = services.getJSONObject(0);
                        journeyClass.setDepartTime(firstService.getString("depfriendly"));
                        journeyClass.setArriveTime(firstService.getString("arrfriendly"));
                        journeyClass.setStatus(firstService.getString("schedule_status"));

                        Log.d("journey arrival",journeyClass.getArriveTime());

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } else {
                    //Returned response is empty - deal wth
                }
            }
        },
        new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                // TODO: Handle error
                //Log.d("URL", URL_LEFT + URL_START + startCode + URL_DEST + endCode);
                VolleyLog.d("error", error.getMessage());
                error.printStackTrace();
            }
        });

queue.add(jsonArrayRequest);

}

这件事可能有固定的处理方式,但就像我说的,我对这件事很陌生。我已经在网上搜索过了,但不太清楚如何在java中实现这一点。我不确定设置类的示例是否正确,但我认为这是从内部类设置属性的一种方法。
更新:
我在侦听器中使用下面的命令,这似乎是可行的。我将需要整理所有的代码,并会回来后,如果这作为解决方案。接下来的8个小时我得去上班。

getActivity().runOnUiThread(new Runnable() {
    @Override
    public void run() {
        TextView station = getActivity().findViewById(stationFieldId);
        station.setText(stationName);
        TextView timeField = getActivity().findViewById(timeFieldId);
        timeField.setText(journey.getDepartTime());
    }
});
uubf1zoe

uubf1zoe1#

这是我最后使用的代码,对我很有用。我已经编辑了它对ui元素所做的很多工作,以包括它设置textview的部分

private void getJourneyAndDisplay(String startCode, final String destCode, final String stationName, final int stationFieldId, final int timeFieldId, final int statusFieldId, final boolean out) {
    queue = Volley.newRequestQueue(getActivity());
    journey = new Journey();

    JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.GET, Journey.URL_LEFT + Journey.URL_START + startCode + Journey.URL_DEST + destCode, null,
            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {

                    if (response.length() != 0) {
                        try {
                            //Make panel visible as it has returned results
                            panel.setVisibility(View.VISIBLE);

                            JSONArray services = returnedJourney.getJSONArray("services");
                            if (services.length() > 0) {
                     //Journey object is configured

                                getActivity().runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        Activity act = getActivity();
                                        TextView station = act.findViewById(stationFieldId);
                                        station.setText(stationName);
                                        TextView timeField = act.findViewById(timeFieldId);
                                        timeField.setText(journey.getDepartTime());
                                        TextView statusField = act.findViewById(statusFieldId);
                                        statusField.setText(journey.getStatus());
                                    }
                                });

                            } else {
                 //Deal iwth no joruney
                            }
                        } catch (JSONException e) {
                            //Display message to inform user that the data cannot be retrieved
                        }
                    } else {
                        //Returned response is empty
                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
        //Handle reponse
                }
            });
    queue.add(jsonArrayRequest);
}

相关问题