0
訪問JSON對象,當我嘗試使用訪問JSON對象下面的代碼「空指針異常」使用URL
http://epubreader.XXXXXXX.net/public/json/books.json
try
{
InputStream in = openHttpConnection("http://epubreader.feathersoft.net
/public/json/books.json");
Data=new byte[in.available()];
in.read(Data);
}
catch(IOException e)
{
Log.e("url", e.getLocalizedMessage()+e.getCause()+e.getStackTrace());
}
}
private InputStream openHttpConnection(String url_send) throws IOException
{
InputStream in = null;
int response = -1;
URL url = new URL(url_send);
URLConnection conn = url.openConnection();
if (!(conn instanceof HttpURLConnection))
throw new IOException("Not an HTTP connection");
try {
HttpURLConnection httpConn = (HttpURLConnection) conn;
httpConn.setAllowUserInteraction(false);
httpConn.setInstanceFollowRedirects(true);
httpConn.setRequestMethod("GET");
httpConn.connect();
response = httpConn.getResponseCode();
if (response == HttpURLConnection.HTTP_OK) {
in = httpConn.getInputStream();
}
} catch (Exception ex) {
throw new IOException("Error connecting");
}
return in;
}
}
然後我得到空指針例外,我無法弄清楚它是什麼,請幫助我
感謝您的時間
感謝您的快速回復。我嘗試使用DefaultHttpClient.I得到錯誤 - :(和warnig「顯示狀態在非活動InputConnection」.Null指針異常對於輸入流 – DroidBot 2011-02-11 13:29:44