Flickr的JSON解析Android中 [英] Flickr JSON Parsing in Android

查看:244
本文介绍了Flickr的JSON解析Android中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从这个网址( HTTP解析JSON:// api.flickr.com/services/feeds/photos_public.gne?format=json ),但实际上文件返回一个非有效的JSON。见

I want to parse the JSON from this url (http://api.flickr.com/services/feeds/photos_public.gne?format=json) but the document actually returns a non valid JSON. See

       jsonFlickrFeed({
        "title": "Uploads from everyone",
        "link": "http://www.flickr.com/photos/",
        "description": "",
        "modified": "2012-11-26T18:27:41Z",
        "generator": "http://www.flickr.com/",
        "items": [
       {
            "title": "2012 Thanksgiving Holiday Weekend",
            "link": "http://www.flickr.com/photos/agape_boarding_school/8220697785/",
            "media": {"m":"http://farm9.staticflickr.com/8478/8220697785_bb298ac5b3_m.jpg"},
            "date_taken": "2012-11-24T14:19:21-08:00",
            "description": " <p><a href=\"http://www.flickr.com/people/agape_boarding_school/\">Agape Boarding School<\/a> posted a photo:<\/p> <p><a href=\"http://www.flickr.com/photos/agape_boarding_school/8220697785/\" title=\"2012 Thanksgiving Holiday Weekend\"><img src=\"http://farm9.staticflickr.com/8478/8220697785_bb298ac5b3_m.jpg\" width=\"240\" height=\"159\" alt=\"2012 Thanksgiving Holiday Weekend\" /><\/a><\/p> <p>Great Day at Agape Boarding School<\/p>",
            "published": "2012-11-26T18:27:41Z",
            "author": "nobody@flickr.com (Agape Boarding School)",
            "author_id": "36563683@N07",
            "tags": "school boarding agape"
       },  ...

我想让我的解析器会尽可能通用这样有什么更好的方式来删除jsonFlickrFeed(下称文件的一部分,并只与JSON它的自我工作的?

I want to let my parser be as general as possible so what is the better way to remove that "jsonFlickrFeed(" part of the document and work only with the JSON it self?

public class JSONParser extends AsyncTask <String, Void, JSONObject> {

static InputStream is = null;
static JSONObject jObj = null;
static String json = "";

// constructor
public JSONParser() {

}

@Override
protected JSONObject doInBackground(String... params) {
    String url=params[0];

    // Making HTTP request
    try {
        // defaultHttpClient
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);

        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        is = httpEntity.getContent();           

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                is, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();
        json = sb.toString();
    } catch (Exception e) {
        Log.e("Buffer Error", "Error converting result " + e.toString());
    }

    // try parse the string to a JSON object
    try {
        jObj = new JSONObject(json);
    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    }

    // return JSON String
    return jObj;
}

}

推荐答案

我已经让位解决了这个问题。

I have solved this issue by given way.

Flickr的旧的Web API 是:<一href=\"http://api.flickr.com/services/feeds/photos_public.gne?id=xxxxxxxx&lang=en-us&format=json\" rel=\"nofollow\">http://api.flickr.com/services/feeds/photos_public.gne?id=xxxxxxxx&lang=en-us&format=json

的Flickr最新的Web API 是:<一href=\"http://api.flickr.com/services/feeds/photos_public.gne?id=xxxxxxxx&lang=en-us&format=json&nojsoncallback=1\" rel=\"nofollow\">http://api.flickr.com/services/feeds/photos_public.gne?id=xxxxxxxx&lang=en-us&format=json&nojsoncallback=1

使用最新一期的Web API

替换您的用户ID 而不是 XXXXXXXX

希望它能够对您有所帮助。

Hope it will helpful to you.

这篇关于Flickr的JSON解析Android中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆