java - Parsing JSON Object to Restful web service -
i developing android registration system communicates mysql database through json. restful web service written php , slim library. have tested web service using advanced rest client app parsing json payload data , works fine result shown below. @ moment trying parse user data android app server , shows error (error parsing data org.json.jsonexception: value <html><head><title>slim of type java.lang.string cannot converted jsonobject)
. tell me doing wrong coz feel error jsonparser class , userfunction. in advance.
here test result
{"tag":"signup","success":1,"error":0,"uid":"547d92b0480711.90973742",
"result":{"firstname":"mish","lastname":"harry","email":"mishael19@gmail.com"}}
here jsonparser
public class jsonparser2 { static inputstream = null; static jsonobject jobj = null; static string json = ""; // constructor public jsonparser2() { } public jsonobject makehttprequest(string url, string method, list<namevaluepair> params){ try{ if(method.equals("post")){ defaulthttpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost(url); httppost.setentity(new urlencodedformentity(params)); httpresponse httpresponse = httpclient.execute(httppost); httpentity httpentity = httpresponse.getentity(); = httpentity.getcontent(); }else if(method == "get"){ defaulthttpclient httpclient = new defaulthttpclient(); string paramstring = urlencodedutils.format(params, "utf-8"); url += "?" + paramstring; httpget httpget = new httpget(url); httpresponse httpresponse = httpclient.execute(httpget); httpentity httpentity = httpresponse.getentity(); = 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 string json object try { jobj = new jsonobject(json); } catch (jsonexception e) { log.e("json parser", "error parsing data " + e.tostring()); } // return json string return jobj; } }
here userfunction
public class userfunctions { private jsonparser2 jsonparser2; private static string signupurl = "http://globalpadtutorials.globalpad.info/loginsignup/v1/signup"; private static string signup_tag = "signup"; private static string postmethod = "post"; public userfunctions(){ jsonparser2 = new jsonparser2(); } public jsonobject signupuser(string fname, string lname, string email, string pwd){ list<namevaluepair> params = new arraylist<namevaluepair>(); params.add(new basicnamevaluepair("tag", signup_tag)); params.add(new basicnamevaluepair("firstname", fname)); params.add(new basicnamevaluepair("lastname", lname)); params.add(new basicnamevaluepair("email", email)); params.add(new basicnamevaluepair("password", pwd)); jsonobject json = jsonparser2.makehttprequest(signupurl, postmethod, params); log.e("json", json.tostring()); return json; } }
error stack
12-02 11:04:19.681: e/json parser(10024): error parsing data org.json.jsonexception: value <html> <head><title>slim of type java.lang.string cannot converted jsonobject
php code
function createuser(){ $request = \slim\slim::getinstance()->request(); $resp = \slim\slim::getinstance()->response(); $resp['content-type'] = 'application/json; charset=utf-8'; $body = $request->getbody(); $users = json_decode($body); $dbhandler = new dbhandler(); $tag = $users->tag; $fname = $users->firstname; $lname = $users->lastname; $email = $users->email; $pwd = $users->password; $response = array("tag" => $tag, "success" => 0, "error" => 0); if($tag == 'signup') { if ($dbhandler->userexisted($email)) { $response["error"] = 2; $response["error_msg"] = "user existed"; echo json_encode($response); } else { $result = $dbhandler->adduser($fname, $lname, $email, $pwd); if ($result != false) { $response["success"] = 1; var_dump($result); $response["uid"] = $result["user_id"]; $response["result"]["firstname"] = $result["firstname"]; $response["result"]["lastname"] = $result["lastname"]; $response["result"]["email"] = $result["email"]; echo json_encode($response); } else { $response["error"] = 1; $response["error_msg"] = "an error occured during sign up"; echo json_encode($response); } } } else{ $response["error"] = 1; $response["error_msg"] = "invalid tag request: ".$tag; echo json_encode($response); } }
html error
slim application errorbody{margin:0;padding:30px;font:12px/1.5 helvetica,arial,verdana,sans-serif;}h1{margin:0;font-size:48px;font-weight:normal;line-height:48px;}strong{display:inline-block;width:65px;}
slim application error
the application not run because of following error:
details
type: errorexceptioncode: 8message: trying property of non-objectfile: /home/globa390/public_html/globalpadtutorials/loginsignup/v1/index.phpline: 63trace
#0 /home/globa390/public_html/globalpadtutorials/loginsignup/v1/index.php(63): slim\slim::handleerrors(8, 'trying p...', '/home/globa390/...', 63, array) 12-02 11:55:50.366: e/json(27492): #1 [internal function]: createuser() 12-02 11:55:50.366: e/json(27492): #2 /home/globa390/public_html/globalpadtutorials/loginsignup/library/slim/route.php(462): call_user_func_array('createuser', array) 12-02 11:55:50.366: e/json(27492): #3 /home/globa390/public_html/globalpadtutorials/loginsignup/library/slim/slim.php(1326): slim\route->dispatch() 12-02 11:55:50.366: e/json(27492): #4 /home/globa390/public_html/globalpadtutorials/loginsignup/library/slim/middleware/flash.php(85): slim\slim->call() 12-02 11:55:50.366: e/json(27492): #5 /home/globa390/public_html/globalpadtutorials/loginsignup/library/slim/middleware/methodoverride.php(92): slim\middleware\flash->call() 12-02 11:55:50.366: e/json(27492): #6 /home/globa390/public_html/globalpadtutorials/loginsignup/library/slim/middleware/prettyexceptions.php(67): slim\middleware\methodoverride->call() 12-02 11:55:50.366: e/json(27492): #7 /home/globa390/public_html/globalpadtutorials/loginsignup/library/slim/slim.php(1271): slim\middleware\prettyexceptions->call() 12-02 11:55:50.366: e/json(27492): #8 /home/globa390/public_html/globalpadtutorials/loginsignup/v1/index.php(12): slim\slim->run() 12-02 11:55:50.366: e/json(27492): #9 {main}
public jsonobject signupuser(string fname, string lname, string email, string pwd){ list<namevaluepair> params = new arraylist<namevaluepair>(5); params.add(new basicnamevaluepair("tag", signup_tag)); params.add(new basicnamevaluepair("firstname", fname)); params.add(new basicnamevaluepair("lastname", lname)); params.add(new basicnamevaluepair("email", email)); params.add(new basicnamevaluepair("password", pwd)); string jsonstr = jsonparser2.makehttprequest(signupurl, postmethod, params); //cast string jsonobject jsonobject json = new jsonobject(jsonstr); return json;
}
Comments
Post a Comment