当前位置: 首页 > 知识库问答 >
问题:

Android改造无法解析数据

景昊焜
2023-03-14

无法解析以下json数据。获得成功响应但返回的正文为空以下是我通过Web浏览器获得的json响应。

 {
    "atcocode": "0100BRA10919",
    "smscode": "bstmgtw",
    "request_time": "2018-09-18T09:32:06+01:00",
    "name": "Broomhill Road",
    "stop_name": "Broomhill Road",
    "bearing": "S",
    "indicator": "S-bound",
    "locality": "Brislington, Bristol",
    "location": {
        "type": "Point",
        "coordinates": [-2.54203,
            51.44156
        ]
    },
    "departures": {
        "all": [{
                "mode": "bus",
                "line": "513",
                "line_name": "513",
                "direction": "Brislington",
                "operator": "GB",
                "date": "2018-09-18",
                "expected_departure_date": "2018-09-18",
                "aimed_departure_time": "09:46",
                "expected_departure_time": "09:48",
                "best_departure_estimate": "09:48",
                "source": "NextBuses",
                "dir": "outbound",
                "id": "https://transportapi.com/v3/uk/bus/route/GB/513/outbound/0100BRA10919/2018-09-18/09:46/timetable.json?app_id=1669c89f&app_key=3687b13f180fd5bf688d7df8ea250b0f",
                "operator_name": "Stagecoach West"
            },
            {
                "mode": "bus",
                "line": "513",
                "line_name": "513",
                "direction": "Brislington, Brislington Square",
                "operator": "GB",
                "date": "2018-09-18",
                "expected_departure_date": null,
                "aimed_departure_time": "10:46",
                "expected_departure_time": null,
                "best_departure_estimate": "10:46",
                "source": "NextBuses",
                "dir": "outbound",
                "id": "https://transportapi.com/v3/uk/bus/route/GB/513/outbound/0100BRA10919/2018-09-18/10:46/timetable.json?app_id=1669c89f&app_key=3687b13f180fd5bf688d7df8ea250b0f",
                "operator_name": "Stagecoach West"
            },
            {
                "mode": "bus",
                "line": "513",
                "line_name": "513",
                "direction": "Brislington, Brislington Square",
                "operator": "GB",
                "date": "2018-09-18",
                "expected_departure_date": null,
                "aimed_departure_time": "11:46",
                "expected_departure_time": null,
                "best_departure_estimate": "11:46",
                "source": "NextBuses",
                "dir": "outbound",
                "id": "https://transportapi.com/v3/uk/bus/route/GB/513/outbound/0100BRA10919/2018-09-18/11:46/timetable.json?app_id=1669c89f&app_key=3687b13f180fd5bf688d7df8ea250b0f",
                "operator_name": "Stagecoach West"
            },
            {
                "mode": "bus",
                "line": "513",
                "line_name": "513",
                "direction": "Brislington, Brislington Square",
                "operator": "GB",
                "date": "2018-09-18",
                "expected_departure_date": null,
                "aimed_departure_time": "12:46",
                "expected_departure_time": null,
                "best_departure_estimate": "12:46",
                "source": "NextBuses",
                "dir": "outbound",
                "id": "https://transportapi.com/v3/uk/bus/route/GB/513/outbound/0100BRA10919/2018-09-18/12:46/timetable.json?app_id=1669c89f&app_key=3687b13f180fd5bf688d7df8ea250b0f",
                "operator_name": "Stagecoach West"
            },
            {
                "mode": "bus",
                "line": "513",
                "line_name": "513",
                "direction": "Brislington, Brislington Square",
                "operator": "GB",
                "date": "2018-09-18",
                "expected_departure_date": null,
                "aimed_departure_time": "13:46",
                "expected_departure_time": null,
                "best_departure_estimate": "13:46",
                "source": "NextBuses",
                "dir": "outbound",
                "id": "https://transportapi.com/v3/uk/bus/route/GB/513/outbound/0100BRA10919/2018-09-18/13:46/timetable.json?app_id=1669c89f&app_key=3687b13f180fd5bf688d7df8ea250b0f",
                "operator_name": "Stagecoach West"
            }
        ]
    },
    "source": "NextBuses"
}

下面是用于解析所用响应类的模型类

    public class All {

    @SerializedName("mode")
    private String mode;
    @SerializedName("line")
    private String line;
    @SerializedName("line_name")

    private String lineName;
    @SerializedName("direction")

    private String direction;
    @SerializedName("operator")

    private String operator;
    @SerializedName("date")

    private String date;
    @SerializedName("expected_departure_date")

    private String expectedDepartureDate;
    @SerializedName("aimed_departure_time")

    private String aimedDepartureTime;
    @SerializedName("expected_departure_time")

    private String expectedDepartureTime;
    @SerializedName("best_departure_estimate")

    private String bestDepartureEstimate;
    @SerializedName("source")

    private String source;
    @SerializedName("dir")

    private String dir;
    @SerializedName("id")

    private String id;
    @SerializedName("operator_name")

    private String operatorName;

    public String getMode() {
        return mode;
    }

    public void setMode(String mode) {
        this.mode = mode;
    }

    public String getLine() {
        return line;
    }

    public void setLine(String line) {
        this.line = line;
    }

    public String getLineName() {
        return lineName;
    }

    public void setLineName(String lineName) {
        this.lineName = lineName;
    }

    public String getDirection() {
        return direction;
    }

    public void setDirection(String direction) {
        this.direction = direction;
    }

    public String getOperator() {
        return operator;
    }

    public void setOperator(String operator) {
        this.operator = operator;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public String getExpectedDepartureDate() {
        return expectedDepartureDate;
    }

    public void setExpectedDepartureDate(String expectedDepartureDate) {
        this.expectedDepartureDate = expectedDepartureDate;
    }

    public String getAimedDepartureTime() {
        return aimedDepartureTime;
    }

    public void setAimedDepartureTime(String aimedDepartureTime) {
        this.aimedDepartureTime = aimedDepartureTime;
    }

    public String getExpectedDepartureTime() {
        return expectedDepartureTime;
    }

    public void setExpectedDepartureTime(String expectedDepartureTime) {
        this.expectedDepartureTime = expectedDepartureTime;
    }

    public String getBestDepartureEstimate() {
        return bestDepartureEstimate;
    }

    public void setBestDepartureEstimate(String bestDepartureEstimate) {
        this.bestDepartureEstimate = bestDepartureEstimate;
    }

    public String getSource() {
        return source;
    }

    public void setSource(String source) {
        this.source = source;
    }

    public String getDir() {
        return dir;
    }

    public void setDir(String dir) {
        this.dir = dir;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getOperatorName() {
        return operatorName;
    }

    public void setOperatorName(String operatorName) {
        this.operatorName = operatorName;
    }
}

Departure.java

 public class Departures {

        @SerializedName("all")
        @Expose
        private List<All> all = null;

        public List<All> getAll() {
            return all;
        }

        public void setAll(List<All> all) {
            this.all = all;
        }

    }

调用api

public void getBusList(String atcocode) {
    ApiService service = RetrofitClient.getRetrofitInstance().create(ApiService.class);

    String API_KEY = mContext.getResources().getString(R.string.api_key);
    String APP_ID = mContext.getResources().getString(R.string.api_id);
    Call<Departures> call = service.getBusList(atcocode, APP_ID, API_KEY, "no");
    call.enqueue(new Callback<Departures>() {

        @Override
        public void onResponse(@NonNull Call<Departures> call, @NonNull Response<Departures> response) {

            mBusListView.populateBusInfo(response.body().getAll());
        }

        @Override
        public void onFailure(@NonNull Call<Departures> call, @NonNull Throwable t) {

        }
    });
}


@GET("bus/stop/{atcocode}/live.json")
Call<Departures> getBusList(@Path("atcocode") String atcocode, @Query("app_id") String appId, @Query("api_key") String apiKey, @Query("group") String group);

我从浏览器中得到了正确的响应。但当通过改装库调用时,获得成功响应(200),但正文为空。

我从浏览器中得到了正确的响应。但当通过改装库调用时,获得成功响应(200),但正文为空。

共有1个答案

楮星鹏
2023-03-14

这是因为背离是JSON中另一个实体的子实体。

尝试使用另一个包含偏差的实体调用getBusList,如下面的实体。

public class Foo {

   private Departures departures;

   // getters and setters

}

<代码>呼叫

这应该分析结果。

 类似资料:
  • 我是翻新和json的新手,我不知道如何解析下一个JSON字符串: } 我只想要权重数组中的“权重”和“日期”。我根据一些例子创建了一个pojo类,但是它不起作用。同样,当我在pojo类中尝试使用< code >时,我无法获得字符串形式的“weight ”(我将把它用作double)。string()。 (我知道使用<代码>。toString()显示类似“com.myPackage.MyPojo@x

  • 此问题是由打字错误或无法再复制的问题引起的。虽然这里可能有类似的问题,但这一问题的解决方式不太可能对未来的读者有所帮助。 在我以前版本的应用程序中,我使用了Apache客户端,所有的东西都工作了,而且仍然工作。但由于性能有所提高,我们决定进行改装。问题是每次我都会遇到“无法解析主机”xxxxxx:没有与主机名关联的地址”。 我把这个放在舱单里了 我还忘了什么吗? 这是url:https://www

  • 问题内容: 大家好,我正在尝试在选项卡中列出文本数据,这是我的方法 无法解析构造函数ArrayAdapter 的方法,这是我的主要活动。任何帮助将不胜感激,我仍然是一个noobie 。 问题答案: 更改 至 你可以在使用。

  • 问题内容: 有人可以帮我这段代码。当前它将在第4行进行投诉:webDriver =新的FirefoxDriver(ff_ep_profiles)说它无法解析构造函数。我需要加载扩展程序,因此我正在创建配置文件 问题答案: 在使用 Selenium v​​3.11.x , GeckoDriver v0.20.0 和 Firefox Quantum v59.0.2时 ,可以使用不同的选项来调用新的/现

  • 我在这里尽量说得具体一点。所以我已经创建了最基本的代码,只是为了测试它,但我仍然无法获得数据,请帮助!这是我的代码: 这是本地服务器上的json输出: [{“id”:1,“子类别”:“MudriPevci”,“标题”:“Mujo i haso”,“描述”:“Krenuli do Grada”,“作者”:“luka”,“日期”:“2016-06-13”},{“id”:3,“子类别”:“MudriPe

  • 我的布局名称是。我还看到了Android-DataBinding-绑定类将如何和何时生成?但这帮不了我。