【问题标题】:Converting XML to JSON in Android gives Error在 Android 中将 XML 转换为 JSON 会出错
【发布时间】:2021-07-20 01:06:38
【问题描述】:

我正在尝试从Earthquake API 获取数据。

我正在使用改造。数据采用XML 格式。我在 XML 中得到了很好的数据。我正在使用 XML-JSON 转换器库将其转换为 JSON。当我尝试使用简单的 xml 字符串(例如

)时,该库工作得很好

<title>Hello</title>.

该库确实将其转换为 JSON。

但是在转换从 api 获取的数据时,它给了我这个错误:

java.lang.NoSuchMethodError: No virtual method end()Z in class Lorg/json/XMLTokener

这个 xml 肯定有一些东西。请先检查 API 响应。

这些是我的文件: MainActivity.Java

public class MainActivity extends AppCompatActivity {

  private static final String TAG = "sagar";
  private ApiInterface apiInterface;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    apiInterface = ApiClient.getClient().create(ApiInterface.class);

    apiInterface.getTodos().enqueue(new Callback < ResponseBody > () {
      @Override
      public void onResponse(Call < ResponseBody > call, Response < ResponseBody > response) {
        try {
          String xmlStr = response.body().string();
          System.out.println("Response is => " + xmlStr.trim());
          JSONObject jsonObject = XML.toJSONObject(xmlStr);
          System.out.println("Response -> " + jsonObject);

        } catch (JSONException | IOException e) {
          e.printStackTrace();
        }
        //                System.out.println("Response -> " + response.body());
      }

      @Override
      public void onFailure(Call < ResponseBody > call, Throwable t) {
        System.out.println("Failure -> " + t.getMessage());

      }
    });
  }
}

APIClient.java

public class ApiClient {

  private static final String BASE_URL = "https://earthquake.usgs.gov/";
  private static Retrofit retrofit = null;

  public static Retrofit getClient() {
    if (retrofit == null) {
      retrofit = new Retrofit.Builder()
        .baseUrl(BASE_URL)
        .addConverterFactory(GsonConverterFactory.create())
        .build();
    }
    return retrofit;
  }

}

ApiInterface.java

public interface ApiInterface {

  @GET("earthquakes/feed/v1.0/summary/significant_day.atom")
  Call < ResponseBody > getTodos();

}

【问题讨论】:

  • 这听起来像是 JSON 库中的一个错误。如果您可以使用最新版本重现它,我建议您向他们的错误跟踪器提交一个最小示例。

标签: android json xml retrofit


【解决方案1】:

只需将网址中的.atom 替换为.geojson

https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/significant_day.geojson

输出:

{
    "type": "FeatureCollection",
    "metadata": {
        "generated": 1619456055000,
        "url":"https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/significant_day.geojson",
        "title": "USGS Significant Earthquakes, Past Day",
        "status": 200,
        "api": "1.10.3",
        "count": 1
    },
    "features": [
        {
            "type": "Feature",
            "properties": {
                "mag": 6.4,
                "place": "200 km WSW of Haveluloto, Tonga",
                "time": 1619389680681,
                "updated": 1619449698040,
                "tz": null,
                "url": "https://earthquake.usgs.gov/earthquakes/eventpage/us6000e4rl",
                "detail": "https://earthquake.usgs.gov/earthquakes/feed/v1.0/detail/us6000e4rl.geojson",
                "felt": 1,
                "cdi": 2.7,
                "mmi": 3.689,
                "alert": "green",
                "status": "reviewed",
                "tsunami": 0,
                "sig": 630,
                "net": "us",
                "code": "6000e4rl",
                "ids": ",us6000e4rl,",
                "sources": ",us,",
                "types": ",dyfi,losspager,moment-tensor,origin,phase-data,shakemap,",
                "nst": null,
                "dmin": 6.009,
                "rms": 1.12,
                "gap": 21,
                "magType": "mww",
                "type": "earthquake",
                "title": "M 6.4 - 200 km WSW of Haveluloto, Tonga"
            },
            "geometry": {
                "type": "Point",
                "coordinates": [-177.0771,-21.6472,234.29]
            },
            "id": "us6000e4rl"
        }
    ]
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-05-30
    • 2011-10-08
    • 1970-01-01
    • 2020-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多