我正在製作一個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());
}
}
}
}
這墜毀行後happends:
HttpResponse response = client.execute(request);
它後來乾脆跳到finally
部分和所有我可以從異常消息得到的是某事是null
。這就是它所說的。
任何人都知道問題可能是什麼?
截圖異常說明:
什麼是完整的異常消息? – 2012-07-18 12:47:56
@Vakimshaar添加了錯誤截圖 – w00 2012-07-18 12:55:18
您仍然看不到整個堆棧跟蹤 – 2012-07-18 12:58:51