Created
October 11, 2012 12:29
-
-
Save alterakey/3871986 to your computer and use it in GitHub Desktop.
Simple JPP Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// {"desc":"Description", "url":"http://host/path", "price":9999, "source":"XXX Shoppers"} | |
// -> Item (desc (String), url (String), price (long), source (String)) | |
package com.gmail.altakey.xxx; | |
import android.util.Log; | |
import java.io.*; | |
import java.util.*; | |
import net.vvakame.util.jsonpullparser.*; | |
public class ItemStreamParser { | |
private JsonPullParser parser; | |
public ItemStreamParser(InputStream is) { | |
this.parser = JsonPullParser.newParser(is); | |
} | |
public void parse(List<Item> into) { | |
try { | |
while (true) { | |
switch (this.parser.getEventType()) { | |
case START_HASH: | |
into.add(parseItem()); | |
break; | |
case END_ARRAY: | |
return; | |
} | |
} | |
} | |
catch (JsonFormatException e) { | |
Log.d("ISP.pI", String.format("invalid JSON data: %s", e.getMessage())); | |
} | |
catch (IOException e) { | |
Log.d("ISP.pI", String.format("IOException: %s", e.getMessage())); | |
} | |
} | |
private Item parseItem() throws JsonFormatException, IOException { | |
Item item = new Item(); | |
while (true) { | |
switch (this.parser.getEventType()) { | |
case END_HASH: | |
return item; | |
case KEY: | |
String key = this.parser.getValueString(); | |
// Read the value | |
this.parser.getEventType(); | |
if (key.equals("source")) { | |
item.source = this.parser.getValueString(); | |
} | |
if (key.equals("url")) { | |
item.url = this.parser.getValueString(); | |
} | |
if (key.equals("desc")) { | |
item.desc = this.parser.getValueString(); | |
} | |
if (key.equals("price")) { | |
item.price = (int)this.parser.getValueLong(); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment