我正在制作一个 Android 应用程序,为此我必须从 URL(由一些 python 脚本输出)获取 JSON 内容。
我在 SO 上找到了一种方法,但我无法让它工作。这是代码:
public String webGet(String URL) throws Exception
{
BufferedReader in = null;
try
{
HttpClient client = new DefaultHttpClient();
client.getParams().setParameter(CoreProtocolPNames.USER_AGENT, "android");
HttpGet request = new HttpGet();
request.setHeader("Content-Type", "text/html; charset=utf-8");
request.setURI(new URI(URL));
// Crashes after executing this line
HttpResponse response = client.execute(request);
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String line = "";
String NL = System.getProperty("line.separator");
while ((line = in.readLine()) != null)
{
sb.append(line + NL);
}
in.close();
String page = sb.toString();
//System.out.println(page);
return page;
}
finally
{
if (in != null)
{
try
{
in.close();
}
catch (IOException e)
{
Log.d("BBB", e.toString());
}
}
}
}
此崩溃发生在此行之后:
HttpResponse response = client.execute(request);
然后它只是跳转到该finally
部分,我可以从异常消息中得到的只是某事是null
. 这就是它所说的。
有人知道问题可能是什么吗?
异常说明截图: