2012-07-17 52 views
0
String url="http://graph.facebook.com/GHost"; 
URL objUrl = new URL(url); 
InputStream is= objUrl.openStream(); 
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
DocumentBuilder db = dbf.newDocumentBuilder(); 
Document doc = db.parse(new InputSource(is)); 
String str=doc.getTextContent();<<<<<<<<<----------- getting null value 
JSONObject object = new JSONObject(str);<<<<---------making exception error 
String id = object.getString("id"); 
if do open the url the content is like below 
{ 
    "id": "3965568744555", 
    "name": "GHost", 
    "picture": "http://profile.ak.fbcdn.net/hprofile-ak-snc4/373042_396504583708761_1625984_s.jpg", 
    "link": "http://www.facebook.com/GHost", 
    "likes": 70, 
    "cover": { 
    "cover_id": 493579484270, 
    "source": "http://a3.sphotos.ak.fbcdn.net/hphotos-ak-snc7/s720x720/396014_493579484001270_177795_n.jpg", 
    "offset_y": 0 
}, 

我該如何在String str =?編寫代碼?最後我想獲得id值。請任何幫助?如何獲取內容/字符串值,當我解析的網址?

+0

你在清單中給出的INTERNET權限? – MAC 2012-07-17 09:37:45

+0

是的,我給了權限 – 2012-07-17 09:38:52

+1

你試圖解析DOM XML格式的JSON數據.. – user370305 2012-07-17 09:40:40

回答

0

可以解析該文件......與使用concat功能..下面的代碼 parse json string

+0

任何refernce鏈接? – 2012-07-17 09:39:27

+0

post updated .. – 2012-07-17 09:42:19

0

使用

String line; 
BufferedReader r = new BufferedReader(new InputStreamReader(inputStream)); 
StringBuilder total = new StringBuilder(); 
while ((line= r.readLine()) != null) { 
    total.append(line); 
} 

String str = total.toString(); 

,而不是

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
DocumentBuilder db = dbf.newDocumentBuilder(); 
Document doc = db.parse(new InputSource(is)); 
String str=doc.getTextContent(); 
+0

我希望inputStream的值不爲null。 – 2012-07-17 09:55:12