【发布时间】:2013-08-04 16:39:41
【问题描述】:
我在我的http get请求中使用了下面的代码,但是我从return得到的是一个null。我不知道为什么。
public static String getResponseFromGetUrl(String url) throws Exception {
StringBuffer sb = new StringBuffer();
try {
HttpResponse httpResponse = httpclient.execute(httpRequest);
String inputLine = "";
if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
InputStreamReader is = new InputStreamReader(httpResponse
.getEntity().getContent());
BufferedReader in = new BufferedReader(is);
while ((inputLine = in.readLine()) != null) {
sb.append(inputLine);
}
in.close();
}
} catch (Exception e) {
e.printStackTrace();
return "net_error";
} finally {
httpclient.getConnectionManager().shutdown();
}
return sb.toString();
}
而我使用的功能是
String json_str = HttpUtils.getResponseFromGetUrl("www.xxx.com/start");
if ((json_str == null)) Log.d("Chen", "lastestTimestap----" + "json_str == null");
有时会打印日志。并非总是如此,实际上就像 1%。但我不知道为什么会导致它。
【问题讨论】:
-
我们能看到更多代码吗?你如何确定它是空的?
-
也许你不小心做了
sb.append("null");之类的事情。 -
@dystroy 我已经编辑了代码
-
@johnchen902 我编辑代码
-
显示的方法
getResponseFromGetUrl在任何情况下都不能返回null。要么您没有在问题中显示真实代码,要么调用它的函数实际上调用了不同的方法(例如,在不同的类中)。
标签: java android stringbuffer