- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
protected String doInBackground(String... urls) {
String url = urls[0];
String result = "";
HttpResponse response = doResponse(url);
if (response == null) {
return result;
} else {
try {
result = inputStreamToString(response.getEntity().getContent());
} catch (IllegalStateException e) {
Log.e(TAG, e.getLocalizedMessage(), e);
} catch (IOException e) {
Log.e(TAG, e.getLocalizedMessage(), e);
}
}
return result;
}
我将把数据发布到下面的数据库中,我附上了剩余的代码,所以请检查并解决我的问题
private HttpResponse doResponse(String url) {
HttpClient httpclient = new DefaultHttpClient(getHttpParams());
HttpResponse response = null;
try {
switch (taskType) {
case POST_TASK:
HttpPost httppost = new HttpPost(url);
// Add parameters
httppost.setEntity(new UrlEncodedFormEntity(params));
response = httpclient.execute(httppost);
break;
case GET_TASK:
HttpGet httpget = new HttpGet(url);
response = httpclient.execute(httpget);
break;
}
} catch (Exception e) {
Log.e(TAG, e.getLocalizedMessage(), e);
}
return response;
}
private String inputStreamToString(InputStream is) {
String line = "";
StringBuilder total = new StringBuilder();
// Wrap a BufferedReader around the InputStream
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
try {
// Read response until the end
while ((line = rd.readLine()) != null) {
total.append(line);
}
} catch (IOException e) {
Log.e(TAG, e.getLocalizedMessage(), e);
}
// Return full string
return total.toString();
}
我在下一行收到NullPointerException
,
结果 = inputStreamToString(response.getEntity().getContent());
。
我不明白实体和内容。有人可以帮助我吗?
最佳答案
您的 HTTP 响应没有实体。这就是 getEntity()
返回 null
的原因。
getEntity() 的 JavaDoc表明可以返回 null
值。因此,您应该始终检查这一点。
例如,而不是:
result = inputStreamToString(response.getEntity().getContent());
你可以这样做:
final HttpEntity entity = response.getEntity();
if (entity == null) {
Log.w(TAG, "The response has no entity.");
// NOTE: this method will return "" in this case, so we must check for that in onPostExecute().
// Do whatever is necessary here...
} else {
result = inputStreamToString(entity.getContent());
}
关于java - 我收到 HttpResponse 但 responce.getEntity().getContent() 显示 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23117263/
我写了一个脚本作为 Ubuntu 启动服务(位于/etc/init.d 并在启动时运行)有一条线 HOMEDIR=`getent passwd username1 | cut -d: -f6` 问题是
我有一些关于 getent group 的问题。 getent group A从哪里得到A组的信息? 是否只能从 /etc/group 获取? 是否有可能我可以通过 getent group A 找到
public InputStream getInputStream() { AndroidHttpClient client = AndroidHttpClient.newInstance(U
我正在使用 Jersey Client 进行 REST 服务调用。现在,当我收到响应时,我想记录 json 响应,并且我还想让实体填充到我的响应 bean 中。 Client client = Cli
我有以下问题: 在我的带有 Hibernate 的 Java Spring Boot 应用程序中,我有 Service 实现类。它看起来像这样: package eu.barz.familykurse
我想检查某个用户是否存在于特定组中。 getent passwd user_name &> /dev/null 上面的命令检查用户是否在那里。但我还想看看它是否属于一个名为 example suppo
我正在使用 jq 来尝试将 bash 命令输出转换为 json。但是,转换失败了。 使用这一行: hostname && getent passwd | egrep -v '/s?bin/(nolog
我在使用 jeresy ClientRespone.getEntity 反序列化时遇到问题 我尝试遵循一些教程和问题,包括: http://jersey.576304.n2.nabble.com/Ho
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: NetworkOnMainThreadException 很长一段时间以来,我一直在使用在 AsyncTas
我正在使用 getent group 命令获取 groups 以及 linux 中的用户名。但它没有显示某些我知道存在的 groups 的任何 usernames。 我需要这个信息,还有其他办法吗?
在我的逻辑应用中,第一步从 Azure 表存储中获取一些已筛选的实体。过滤器由两个条件组成: 一个字段必须等于某个常量值 其他字段(日期时间)必须小于或等于当前时间减 10 分钟 它一直工作正常,直到
在 Symfony 中创建 Doctrine postPersist EventListener 后,我的 IDE 指向 $args->getEntity() 行;由于已被弃用,我不知道如何解决这个问
在配置 LDAP 身份验证时,我遇到了以下问题。 我已经在 nsswitch.conf 文件中配置了 nss,如下所示: 密码:ldap文件 组:文件 阴影:文件 当我给出以下命令时:获取密码列出所有
我想知道为什么一方面从 id 和 group 得到不同的结果,另一方面从 getent group 得到不同的结果。重现步骤: $ sudo usermod -a -G libvirt eric $
我想使用 javax.ws.rs.core.Response 来发送和接收 Card 实体对象。但我不知道如何将内容转换回 Card 对象。 我的 testCreate() 方法应该执行 create
我的代码 /* * To change this template, choose Tools | Templates * and open the template in the editor.
我需要 IP 地址的前 3 个八位字节和 myhosts 名称,而我尝试通过 cut 命令进行但无法加入主机名 $ getent hosts myhosts 172.10.2.32 myhost
当我调用下一个代码时: Response response = target.request(MediaType.APPLICATION_JSON_TYPE) .pos
我有这个异常(exception)。 我的对象看起来像这样: @XmlRootElement @XmlAccessorType(XmlAccessType.FIELD) public class Ob
当我向特定 URL 发出 get 请求时,将下载一个文件。我可以从两种方式获取InputStream。 方法一 使用java.net包中的URL类。 java.net.URL url = new UR
我是一名优秀的程序员,十分优秀!