首页 > 解决方案 > 我在我的 Android 中得到“无法解析方法 .getEntity()”?

问题描述

我在 JsonArrayRequest 中编写以下代码:

String jsonString = EntityUtils.toString(response.getEntity());

我通过添加库 useLibrary 'org.apache.http.legacy' 得到“EntityUtils”,但在我的 Android 中得到“无法解析方法 .getEntity()”?如何解决问题?

以下是我的 Android 代码的一部分:

JsonObjectRequest jsonArrayRequest = new JsonObjectRequest(Request.Method.GET, HttpUrl,null,

                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {

                        String jsonString = EntityUtils.toString(response.getEntity());

我无法在我的代码中添加“.getEntity()”,它显示“无法解析方法 .getEntity() ...

标签: android

解决方案


我认为您在两个库之间感到困惑。您说您正在使用org.apache.http.legacy,但看起来您正在使用Volley库。

现在在您的onResponse()方法中,您的response对象JSONObject没有该getEntity()方法。看起来您只是想以字符串形式获得响应。

为此,只需更换

String jsonString = EntityUtils.toString(response.getEntity());

 String jsonString = response.toString();

如果您实际使用,org.apache.http.legacy那么您的响应将是HttpResponse该库中的类型,那么您的response对象将拥有该getEntity()方法。

祝你好运。


推荐阅读