2

我有以下 json,我很难在 android 中解析它。

{"Events":[
{"RecID":"0", "ItemDetail":[ 
    {"Title": "THRIC 2013"},
    {"Short_Detail" : "9TH ANNUAL TOURISM AND HOSPITALITY RESEARCH IN IRELAND CONFERENCE (THRIC)"},
    {"Long_Detail" : "The Tourism and Hospitality Research in Ireland Conference (THRIC) is an opportunity to gather industry, academia and students together and offers a forum to discuss their research. The 9th Annual Tourism and Hospitality Research in Ireland Conference (THRIC) will take place in Galway-Mayo Institute of Technology on Thursday 6th and Friday 7th June, 2013."},
    {"Programm":[ 
        {"EventDate":"06/06/2013", "Detail" :[
            {"ID" : "0", "Start_Time" : "08:30", "End_Time": "09:15", "Programme" : "Registration", "Speaker" : "9999", "Location" : "Theatre 1000 Foyer", "Map_Entry" : "53.277801, -9.010624"},
            {"ID" : "1", "Start_Time" : "09:15", "End_Time": "10:15", "Programme" : "Conference Welcome & Official Opening", "Speaker" : "", "Location" : "Theatre 1000", "Map_Entry" : "53.277801, -9.010624"},
            {"ID" : "2", "Start_Time" : "10:15", "End_Time": "11:00", "Programme" : "Keynote Speaker 1", "Speaker" : "0", "Location" : "Theatre 1000", "Map_Entry" : "53.277801, -9.010624"},
            {"ID" : "3", "Start_Time" : "11:00", "End_Time": "11:30", "Programme" : "Morning Refreshments", "Speaker" : "9999", "Location" : "Theatre 1000 Foyer", "Map_Entry" : "53.277801, -9.010624"},
            {"ID" : "4", "Start_Time" : "11:30", "End_Time": "13:00", "Programme" : "Parallel Session A", "Speaker" : "9999", "Location" : "Rooms 1041/1039/1033", "Map_Entry" : "53.277801, -9.010624"},
            {"ID" : "5", "Start_Time" : "13:00", "End_Time": "14:15", "Programme" : "Networking Buffet Lunch", "Speaker" : "9999", "Location" : "Connemara Training Restaurant", "Map_Entry" : "53.277801, -9.010624"},
            {"ID" : "6", "Start_Time" : "14:15", "End_Time": "15:00", "Programme" : "Keynote Speaker 2", "Speaker" : "1", "Location" : "Theatre 1000", "Map_Entry" : "53.277801, -9.010624"},
            {"ID" : "7", "Start_Time" : "15:00", "End_Time": "15:15", "Programme" : "Afternoon Refreshments", "Speaker" : "9999", "Location" : "Theatre 1000 Foyer", "Map_Entry" : "53.277801, -9.010624"},
            {"ID" : "8", "Start_Time" : "15:15", "End_Time": "16:45", "Programme" : "Parallel Session B", "Speaker" : "9999", "Location" : "Rooms 1041/1039/1033", "Map_Entry" : "53.277801, -9.010624"},
            {"ID" : "9", "Start_Time" : "19:00", "End_Time": "00:00", "Programme" : "Drinks Reception", "Speaker" : "9999", "Location" : "The G Hotel, Wellpark, Galway", "Map_Entry" : "53.281464, -9.032167"},
            {"ID" : "10", "Start_Time" : "19:30", "End_Time": "00:00", "Programme" : "Conference Dinner", "Speaker" : "9999", "Location" : "The G Hotel, Wellpark, Galway", "Map_Entry" : "53.281464, -9.032167"}
        ]},
        {"EventDate":"07/06/2013", "ItemDetail":[
            {"ID" : "0", "Start_Time" : "08:30", "End_Time": "09:30", "Programme" : "Registration", "Speaker" : "9999", "Location" : "Theatre 1000 Foyer", "Map_Entry" : "53.277801, -9.010624"},
            {"ID" : "1", "Start_Time" : "09:30", "End_Time": "11:00", "Programme" : "Parallel Session C", "Speaker" : "9999", "Location" : "Rooms 1041/1039/1033", "Map_Entry" : "53.277801, -9.010624"},
            {"ID" : "2", "Start_Time" : "11:00", "End_Time": "11:30", "Programme" : "Morning Refreshments", "Speaker" : "9999", "Location" : "Theatre 1000 Foyer", "Map_Entry" : "53.277801, -9.010624"},
            {"ID" : "3", "Start_Time" : "11:30", "End_Time": "12:45", "Programme" : "Panel Discussion", "Speaker" : "2,3,4,5,6", "Location" : "Rooms 939/Theatre 1000", "Map_Entry" : "53.277801, -9.010624"},
            {"ID" : "4", "Start_Time" : "12:45", "End_Time": "13:15", "Programme" : "Reflections on Conference\nClosing Address\nInvitation to THRIC 2014", "Speaker" : "9999", "Location" : "Room 939/Theatre 1000", "Map_Entry" : "53.277801, -9.010624"},
            {"ID" : "5", "Start_Time" : "13:15", "End_Time": "14:15", "Programme" : "Networking Buffet Lunch", "Speaker" : "9999", "Location" : "Connemara Training Restaurant", "Map_Entry" : "53.277801, -9.010624"},
            {"ID" : "6", "Start_Time" : "14:15", "End_Time": "14:45", "Programme" : "ATHRI Committee Meeting", "Speaker" : "9999", "Location" : "1031", "Map_Entry" : "53.277801, -9.010624"}
        ]}
    ]},
    {"Speakers":[ 
        {"ID" : "0", "Person_Name" : "Jim Murphy", "Title" : "CEO Prem Group", "Info" : "Jim Murphy is the CEO and founder of PREM Group,one of Irelands leading Hotel Management companies. A graduate of the Block Release Hotel Management Programme at GMIT, his association with what was to become PREM Group began in 1989 when he was appointed General Manager of Stephens Hall Hotel, Irelands first all-suite hotel. Prior to this appointment Jim had built up an admirable career gaining valuable management experience in renowned hotels such as Scotland’s Gleneagles Hotel, The Intercontinental Hotel Frankfurt, Marlfield House Hotel, Co. Wexford, Ireland, The Park Hotel, Co. Kerry, Ireland and the Gresham Hotel in Dublin’s city centre.\nIn 1996 Jim and fellow investors bought Stephens Hall Hotel and Jim was appointed Managing Director. This purchase marks the foundation of PREM Group. Today PREM Group operates over 40 properties in Ireland, England, Belgium, France and Poland. It is one of Ireland’s largest hotel management companies and is working on a pipeline of new projects across Europe. PREM Group specialize in the management of hotels and serviced apartments under a number of major international brands such as, Crowne Plaza, Holiday Inn, Ramada Plaza, Days Hotels and Express by Holiday Inn. It also owns and operates its own brands under the name of Premier Apartments, Aspect Hotels and Leopold Hotels\nTrinity Purchasing, a wholly owned subsidiary of PREM Group, purchases goods and services to the value of €350m per annum. Trinity works with over 120 independent hotels delivering savings across a wide range of products.\nPREM Group recently secured a significant private equity investment into the company. This investment will be used to ramp up its growth strategy over the next few years.\nIn addition to growing PREM Group Jim has had the honour of holding the presidency of the Irish Hotels Federation from 2002 to 2004 and has been awarded a Fellowship of the Irish Hospitality Institute.\nFor more information see www.premgroup.com"},
        {"ID" : "1", "Person_Name" : "Mark Nagurski", "Title" : "Director Culture Tech Festival, Derry", "Info" : "Based in Derry, N. Ireland, Mark Nagurski is the Director of CultureTECH, a week-long festival of digital media, technology and music. CultureTECH features over 100 individual events with a total expected audience of over 30,000 people, making it one of the biggest digital events in the country. In addition to the festival, CultureTECH also manages a year-round programme of events and activities designed to foster innovation at the crossroads of culture and technology. Prior to launching CultureTECH, Mark served as Derry-Londonderry's first Digital Champion and currently sits on multiple boards and advisory groups across both technology and the arts."},
        {"ID" : "2", "Person_Name" : "John Concannon", "Title" : "Failte Ireland", "Info" : ""},
        {"ID" : "3", "Person_Name" : "Michael Vaughan", "Title" : "Irish Hotel Federation", "Info" : ""},
        {"ID" : "4", "Person_Name" : "Eammon McKeon", "Title" : "Irish Tourist Industry Confederation", "Info" : ""},
        {"ID" : "5", "Person_Name" : "John Crumlish", "Title" : "Galway Arts Festival", "Info" : ""},
        {"ID" : "6", "Person_Name" : "Tony Candon", "Title" : "National Museum of Ireland - Country Life", "Info" : ""}
    ]},
    {"eMail": "thric@gmit.ie"}, {"Post" : "THRIC Committee\nCollege of Tourism and Arts\nGalway-Mayo Institute of Technology\nDublin Road\nGalway\nIreland"}, {"Web" : "www.gmit.ie/thric"},{"Twitter":"@thric2013"}, {"GPS": "53.277801, -9.010624"}
    ]}
]}

我正在尝试使用 Jackson 库对其进行解析...我遇到的麻烦是当我使用 stringBuilder 将 BufferReader 转换为字符串时。一半的数据被遗漏了。我根本无法理解原因。我的代码如下......

private boolean callWebService()
{

    URL url;
    try {
        url = new URL("http://10.0.2.2/thric/thric.php");
    } catch (MalformedURLException e1) {
        // TODO Auto-generated catch block
        Log.i("Error", e1.getMessage());
        return true;
    }
    URLConnection urlc;
    try {
        urlc = url.openConnection();
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        Log.i("Error", e1.getMessage());
        return true;
    }
    try {
        BufferedReader reader=new BufferedReader(new InputStreamReader(urlc.getInputStream()));

        StringBuffer sb = new StringBuffer();
        String line = null;

        try {
            while ((line = reader.readLine()) != null)
            {
                Log.i("data", line.toString());
                sb.append(line + "\n");
                sb.ensureCapacity(1000000);
            }
        } catch (IOException e) {
            Log.i("Error", e.getMessage());
            return true;
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    Log.i("Error", e.getMessage());
                    return true;
                }
            }
        }

        String result = sb.toString();

        Log.i("Data", result.toString());

        ObjectMapper mapper = new ObjectMapper(); // can reuse, share globally
        try {

            AppData.getHandle().appData = mapper.readValue(result, Results.class);
            //AppData.getHandle().appData = mapper.readValue(result, Results.class);
        } catch (JsonParseException e) {
            // TODO Auto-generated catch block
            Log.i("Error", e.getMessage());
            return true;
        } catch (JsonMappingException e) {
            // TODO Auto-generated catch block
            Log.i("Error", e.getMessage());
            return true;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            Log.i("Error", e.getMessage());
            return true;
        }
        return false;


    } catch (IOException e1) {
        // TODO Auto-generated catch block
        Log.i("Error", e1.getMessage());
        return true;
    }
}

我可以在日志信息中看到数据存在于 bufferreader 中,但其中一半消失了......当将行附加到 stringbuilder/stringbuffer 时。请帮忙。

logCat 信息显示结果被截断如下:

{"Events":[
{"RecID":"0", "ItemDetail":[ 
    {"Title": "THRIC 2013"},
    {"Short_Detail" : "9TH ANNUAL TOURISM AND HOSPITALITY RESEARCH IN IRELAND CONFERENCE (THRIC)"},
    {"Long_Detail" : "The Tourism and Hospitality Research in Ireland Conference (THRIC) is an opportunity to gather industry, academia and students together and offers a forum to discuss their research. The 9th Annual Tourism and Hospitality Research in Ireland Conference (THRIC) will take place in Galway-Mayo Institute of Technology on Thursday 6th and Friday 7th June, 2013."},
    {"Programm":[ 
        {"EventDate":"06/06/2013", "Detail" :[
            {"ID" : "0", "Start_Time" : "08:30", "End_Time": "09:15", "Programme" : "Registration", "Speaker" : "9999", "Location" : "Theatre 1000 Foyer", "Map_Entry" : "53.277801, -9.010624"},
            {"ID" : "1", "Start_Time" : "09:15", "End_Time": "10:15", "Programme" : "Conference Welcome & Official Opening", "Speaker" : "", "Location" : "Theatre 1000", "Map_Entry" : "53.277801, -9.010624"},
            {"ID" : "2", "Start_Time" : "10:15", "End_Time": "11:00", "Programme" : "Keynote Speaker 1", "Speaker" : "0", "Location" : "Theatre 1000", "Map_Entry" : "53.277801, -9.010624"},
            {"ID" : "3", "Start_Time" : "11:00", "End_Time": "11:30", "Programme" : "Morning Refreshments", "Speaker" : "9999", "Location" : "Theatre 1000 Foyer", "Map_Entry" : "53.277801, -9.010624"},
            {"ID" : "4", "Start_Time" : "11:30", "End_Time": "13:00", "Programme" : "Parallel Session A", "Speaker" : "9999", "Location" : "Rooms 1041/1039/1033", "Map_Entry" : "53.277801, -9.010624"},
            {"ID" : "5", "Start_Time" : "13:00", "End_Time": "14:15", "Programme" : "Networking Buffet Lunch", "Speaker" : "9999", "Location" : "Connemara Training Restaurant", "Map_Entry" : "53.277801, -9.010624"},
            {"ID" : "6", "Start_Time" : "14:15", "End_Time": "15:00", "Programme" : "Keynote Speaker 2", "Speaker" : "1", "Location" : "Theatre 1000", "Map_Entry" : "53.277801, -9.010624"},
            {"ID" : "7", "Start_Time" : "15:00", "End_Time": "15:15", "Programme" : "Afternoon Refreshments", "Speaker" : "9999", "Location" : "Theatre 1000 Foyer", "Map_Entry" : "53.277801, -9.010624"},
            {"ID" : "8", "Start_Time" : "15:15", "End_Time": "16:45", "Programme" : "Parallel Session B", "Speaker" : "9999", "Location" : "Rooms 1041/1039/1033", "Map_Entry" : "53.277801, -9.010624"},
            {"ID" : "9", "Start_Time" : "19:00", "End_Time": "00:00", "Programme" : "Drinks Reception", "Speaker" : "9999", "Location" : "The G Hotel, Wellpark, Galway", "Map_Entry" : "53.281464, -9.032167"},
            {"ID" : "10", "Start_Time" : "19:30", "End_Time": "00:00", "Programme" : "Conference Dinner", "Speaker" : "9999", "Location" : "The G Hotel, Wellpark, Galway", "Map_Entry" : "53.281464, -9.032167"}
        ]},
        {"EventDate":"07/06/2013", "ItemDetail":[
            {"ID" : "0", "Start_Time" : "08:30", "End_Time": "09:30", "Programme" : "Registration", "Speaker" : "9999", "Location" : "Theatre 1000 Foyer", "Map_Entry" : "53.277801, -9.010624"},
            {"ID" : "1", "Start_Time" : "09:30", "End_Time": "11:00", "Programme" : "Parallel Session C", "Speaker" : "9999", "Location" : "Rooms 1041/1039/1033", "Map_Entry" : "53.277801, -9.010624"},
            {"ID" : "2", "Start_Time" : "11:00", "End_Time": "11:30", "Programme" : "Morning Refreshments", "Speaker" : "9999", "Location" : "Theatre 1000 Foyer", "Map_Entry" : "53.277801, -9.010624"},
            {"ID" : "3", "Start_Time" : "11:30", "End_Time": "12:45", "Programme" : "Panel Discussion", "Speaker" : "2,3,4,5,6", "Location" : "Rooms 939/Theatre 1000", "Map_Entry" : "53.277801, -9.010624"},
            {"ID" : "4", "Start_Time" : "12:45", "End_Time": "13:15", "Programme" : "Reflections on Conference\nClosing Address\nInvitation to THRIC 2014", "Speaker" : "9999", "Location" : "Room 939/Theatre 1000", "Map_Entry" : "53.277801, -9.010624"},
            {"ID" : "5", "Start_Time" : "13:15", "End_Time": "14:15", "Programme" : "Networking Buffet Lunch", "Speaker" : "9999", "Location" : "Connemara Training Restaurant", "Map_Entry" : "53.277801, -9.010624"},
            {"ID" : "6", "St
4

1 回答 1

2

您的appData变量可能会在某处重新初始化为空值。

为防止这种情况,请appData在您的onCreate()方法之前声明如下:

private Results appData = new Results();

然后在您的callWebService()方法上,像这样传递 JSON 映射:

AppData.getHandle().setAppData(mapper.readValue(result, Results.class));

祝你好运^^

于 2013-05-28T17:22:31.163 回答