当我解析网址时如何获取内容/字符串值?

时间:2012-07-17 09:36:14

标签: android xml-parsing

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值。请帮忙吗?

2 个答案:

答案 0 :(得分:0)

你可以使用String concat函数解析文件... parse json string

答案 1 :(得分: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();