android - 我在我的 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() ...
解决方案
我认为您在两个库之间感到困惑。您说您正在使用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()
方法。
祝你好运。
推荐阅读
- react-native - React Native:任务':app:compileDebugJavaWithJavac'的执行失败
- asp.net-identity - IdentityServer4 System.ArgumentNullException:值不能为空。参数名称:尝试刷新令牌时的类型
- sql - SQL: How to select pass rows from student table in case of re-sit exam in postgresql?
- java - 错误:java.lang.SecurityException:签名者信息与同一包中其他类的签名者信息不匹配
- c++ - 如何使用 libpq 库和 C++ 将 NULL 值绑定到准备好的语句?
- swift - UICollectionViewCell 协议不起作用
- c# - 如何模拟httpclient
- python-2.7 - Pyspark toDF() 使用 collect() 抛出 IllegalStateException
- prolog - 检查二维数组中每一行和每一列中的重复值
- python - 如何防止小部件在 Tkinter 中超出窗口?