java - Please help a newbie having trouble with the following: String cannot be converted to JSON object -
i using facebook sdk eclipse send requests friends, want include additional info application in "data" field. maintain getting error java.lang.string cannot converted json object, no matter try. i've tried retrieving info in jsonarray , retrieving specific values in jsonobjects, no avail. i'd appreciate if tell me i'm doing wrong.
i've included relevant code below. badge of awesomeness , social karma values placeholders, copied facebook tutorial here: https://developers.facebook.com/docs/android/send-requests
the request-sending code, straight illustration (apart message field):
private void sendrequestdialog() { bundle params = new bundle(); params.putstring("title", "send request"); params.putstring("message", application.getuserid()+" has invited event!"); string idholder = ids.get(0); (int = 1; < ids.size(); i++) { idholder = idholder+"," + ids.get(i); // for-loop selected // user ids in recipients // list (starting @ 1 // prevent comma placement // issues) } params.putstring("to", idholder); // comma seperated list of facebook // ids preset recipients. params.putstring("data", "{\"badge_of_awesomeness\":\"1\"," + "\"social_karma\":\"5\"}"); webdialog requestsdialog = ( // initializes web dialog, sends // requests parameters stored // ids, displays errors in sending new webdialog.requestsdialogbuilder(this, session.getactivesession(), params)).setoncompletelistener(new oncompletelistener() { @override public void oncomplete(bundle values, facebookexception error) { if (error != null) { if (error instanceof facebookoperationcanceledexception) { toast.maketext(getapplicationcontext(), "request cancelled", toast.length_short).show(); } else { toast.maketext(getapplicationcontext(), "network error", toast.length_short).show(); } } else { final string requestid = values.getstring("request"); if (requestid != null) { toast.maketext(getapplicationcontext(), "request sent", toast.length_short).show(); } else { toast.maketext(getapplicationcontext(), "request cancelled", toast.length_short).show(); } } } }).build(); requestsdialog.show(); } followed code on receiving end (the toast(badge_of_awesomeness) there testing purposes):
private void getrequestdata(final string inrequestid) { // create new request http // request id graph path. request request = new request(session.getactivesession(), inrequestid, null, httpmethod.get, new request.callback() { @suppresslint("newapi") @override public void oncompleted(response response) { // process returned response graphobject graphobject = response.getgraphobject(); facebookrequesterror error = response.geterror(); // default message string message = "incoming request"; if (graphobject != null) { // check if there info if (graphobject.getproperty("data") != null) { seek { // data, parse info key/value info jsonobject dataobject = new jsonobject((string)graphobject.getproperty("data")); toast.maketext(getapplicationcontext(), dataobject.getstring("badge_of_awesomeness"), 500).show(); // new jsonobject((string)graphobject.getproperty("data")); // value key - badge_of_awesomeness string badge = dataobject.getstring("badge_of_awesomeness"); // value key - social_karma string karma = dataobject.getstring("social_karma"); // sender's name jsonobject fromobject = (jsonobject) graphobject.getproperty("from"); string sender = fromobject.getstring("name"); string title = sender+" sent gift"; // create text alert based on sender // , info message = title + "\n\n" + "badge: " + badge + " karma: " + karma; } grab (jsonexception e) { message = "error getting request infojson"; log.e("!", "!", e); } } else if (error != null) { message = "error getting request info"; } } toast.maketext(getapplicationcontext(), message, toast.length_long).show(); } }); // execute request asynchronously. request.executebatchasync(request); } the json:
{ "id": "493703870648580", "application": { "name": "send requests how to", "id": "403223126407920" }, "to": { "name": "chris abe colm", "id": "100003086810435" }, "from": { "name": "christine abernathy", "id": "1424840234" }, "data": "{\"badge_of_awesomeness\":\"1\",\"social_karma\":\"5\"}", "message": "learn how create android apps social", "created_time": "2012-10-07t17:29:57+0000" } the logcat:
06-20 17:33:47.413: e/!(31515): ! 06-20 17:33:47.413: e/!(31515): org.json.jsonexception: value nullandroid.widget.edittext@4164a490android.widget.edittext@414d9928 of type java.lang.string cannot converted jsonobject 06-20 17:33:47.413: e/!(31515): @ org.json.json.typemismatch(json.java:111) 06-20 17:33:47.413: e/!(31515): @ org.json.jsonobject.<init>(jsonobject.java:159) 06-20 17:33:47.413: e/!(31515): @ org.json.jsonobject.<init>(jsonobject.java:172) 06-20 17:33:47.413: e/!(31515): @ hro.informatica.gameplanner.gameplannermain$10.oncompleted(gameplannermain.java:482) 06-20 17:33:47.413: e/!(31515): @ com.facebook.request$4.run(request.java:1725) 06-20 17:33:47.413: e/!(31515): @ android.os.handler.handlecallback(handler.java:733) 06-20 17:33:47.413: e/!(31515): @ android.os.handler.dispatchmessage(handler.java:95) 06-20 17:33:47.413: e/!(31515): @ android.os.looper.loop(looper.java:212) 06-20 17:33:47.413: e/!(31515): @ android.app.activitythread.main(activitythread.java:5151) 06-20 17:33:47.413: e/!(31515): @ java.lang.reflect.method.invokenative(native method) 06-20 17:33:47.413: e/!(31515): @ java.lang.reflect.method.invoke(method.java:515) 06-20 17:33:47.413: e/!(31515): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:878) 06-20 17:33:47.413: e/!(31515): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:694) 06-20 17:33:47.413: e/!(31515): @ dalvik.system.nativestart.main(native method) 06-20 17:33:47.423: w/resourcetype(31515): creating string cache of 44 bytes i don't i'm doing wrong here, i've spent day (well, afternoon) pouring on stackoverflow trying different solutions. i'm making obvious error somewhere, can't find it. stuck, please help me out!
1.do know on particular line receive exception?
on receiving-end code, see seek json in 2 different ways:
jsonobject dataobject = new jsonobject((string)graphobject.getproperty("data")); ... jsonobject fromobject = (jsonobject) graphobject.getproperty("from"); the first right way convert string json, sec not.
can please try:
jsonobject fromobject = new jsonobject((string)graphobject.getproperty("from")); instead of:
jsonobject fromobject = (jsonobject) graphobject.getproperty("from"); 2.also on sending-end code: don't see anywhere call:
params.putstring("from", "{...json stuff...}"); to set "from" anywhere. maybe forgot include that?
java android json facebook jsonexception
No comments:
Post a Comment