functionality in iphone app searches twitter text. code straightforward, pass url string twitter api, , parse result. although half time json parser fails errors unescaped control character '0x0'. code , full error message below.
- (void)grabdata:(nsstring *)searchterm { nsstring *urlstring = [nsstring stringwithformat:@"http://search.twitter.com/search.json?q=%@&rpp=25",searchterm]; nsurl *url = [nsurl urlwithstring:urlstring]; nslog(@"created url:%@",url); //setup , start async download nsurlrequest *request = [[nsurlrequest alloc] initwithurl:url]; nsurlconnection *connection = [[nsurlconnection alloc] initwithrequest:request delegate:self]; [connection release]; [request release]; } - (void)connection:(nsurlconnection *)connection didreceivedata:(nsdata *)data { // store incoming data string nsstring *jsonstring = [[nsstring alloc] initwithdata:data encoding:nsutf8stringencoding]; //nslog(@"did receive data %@",jsonstring); //create dictionary json string nsdictionary *results = [jsonstring jsonvalue]; // build array dictionary easy access each entry nsdictionary *tweets = [results objectforkey:@"results"]; // loop through each entry in dictionary for(nsdictionary *tweet in tweets) { // user string tweet nsstring *tuser = [tweet objectforkey:@"from_user"]; nslog(@"tweet %@",tuser); } }
error message console, 50% of time, other 50% works expected.
2010-12-20 21:22:02.022 twittersearch[47362:207] created url:http://search.twitter.com/search.json?q=red&rpp=25 2010-12-20 21:22:02.361 twittersearch[47362:207] -jsonvalue failed. error trace is: ( "error domain=org.brautaset.json.errordomain code=5 \"unescaped control character '0x0'\" userinfo=0x4d6a130 {nslocalizeddescription=unescaped control character '0x0'}", "error domain=org.brautaset.json.errordomain code=3 \"object value expected key: profile_image_url\" userinfo=0x4d6a200 {nsunderlyingerror=0x4d6a170 \"unescaped control character '0x0'\", nslocalizeddescription=object value expected key: profile_image_url}", "error domain=org.brautaset.json.errordomain code=3 \"expected value while parsing array\" userinfo=0x4d6a240 {nsunderlyingerror=0x4d6a1e0 \"object value expected key: profile_image_url\", nslocalizeddescription=expected value while parsing array}", "error domain=org.brautaset.json.errordomain code=3 \"object value expected key: results\" userinfo=0x4d6a310 {nsunderlyingerror=0x4d6a2d0 \"expected value while parsing array\", nslocalizeddescription=object value expected key: results}" )
the didreceivedata
method can called multiple times data delivered. in method, should append each incoming chunk nsmutabledata class variable (not process it).
the processing of complete data should done in connectiondidfinishloading
method.
the error happening because trying parse partial block of data.
Comments
Post a Comment