android - JSON parsing and AsyncTask error -
i trying register , login function there problem json , asynctask. here logcat error.
02-06 04:18:08.857: e/json(1160): <!doctype html public "-//w3c//dtd html 4.01//en""http://www.w3.org/tr/html4/strict.dtd">n<html><head><title>not found</title>n<meta http-equiv="content-type" content="text/html; charset=us-ascii"></head>n<body><h2>not found</h2>n<hr><p>http error 404. requested resource not found.</p>n</body></html>n 02-06 04:18:08.857: e/json parser(1160): error parsing data org.json.jsonexception: value <!doctype of type java.lang.string cannot converted jsonobject 02-06 04:18:08.887: w/dalvikvm(1160): threadid=11: thread exiting uncaught exception (group=0x41465700) 02-06 04:18:08.937: e/androidruntime(1160): fatal exception: asynctask #1 02-06 04:18:08.937: e/androidruntime(1160): java.lang.runtimeexception: error occured while executing doinbackground() 02-06 04:18:08.937: e/androidruntime(1160): @ android.os.asynctask$3.done(asynctask.java:299) 02-06 04:18:08.937: e/androidruntime(1160): @ java.util.concurrent.futuretask.finishcompletion(futuretask.java:352) 02-06 04:18:08.937: e/androidruntime(1160): @ java.util.concurrent.futuretask.setexception(futuretask.java:219) 02-06 04:18:08.937: e/androidruntime(1160): @ java.util.concurrent.futuretask.run(futuretask.java:239) 02-06 04:18:08.937: e/androidruntime(1160): @ android.os.asynctask$serialexecutor$1.run(asynctask.java:230) 02-06 04:18:08.937: e/androidruntime(1160): @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1080) 02-06 04:18:08.937: e/androidruntime(1160): @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:573) 02-06 04:18:08.937: e/androidruntime(1160): @ java.lang.thread.run(thread.java:841) 02-06 04:18:08.937: e/androidruntime(1160): caused by: java.lang.nullpointerexception 02-06 04:18:08.937: e/androidruntime(1160): @ my.fyp.inticlassifieds.registertask.doinbackground(registertask.java:76) 02-06 04:18:08.937: e/androidruntime(1160): @ my.fyp.inticlassifieds.registertask.doinbackground(registertask.java:1) 02-06 04:18:08.937: e/androidruntime(1160): @ android.os.asynctask$2.call(asynctask.java:287) 02-06 04:18:08.937: e/androidruntime(1160): @ java.util.concurrent.futuretask.run(futuretask.java:234) 02-06 04:18:08.937: e/androidruntime(1160): ... 4 more
this class asynctask used:
public class registertask extends asynctask<string, void, integer> { private progressdialog progressdialog; private registeractivity activity; private int id = -1; private jsonparser jsonparser; private static string loginurl = "http://10.2.2.0/project/index.php"; private static string registerurl = "http://10.2.2.0/project/index.php"; private static string key_success = "success"; private static string key_error = "error"; private static string key_error_msg = "error_msg"; private static string key_uid = "uid"; private static string key_name = "name"; private static string key_email = "email"; private static string key_created_at = "created_at"; private int responsecode = 0; public registertask(registeractivity activity, progressdialog progressdialog) { this.activity = activity; this.progressdialog = progressdialog; } @override protected void onpreexecute() { progressdialog.show(); progressdialog.dismiss(); } @override protected integer doinbackground(string... arg0) { edittext username = (edittext) activity .findviewbyid(r.id.registeremail); edittext passwordedit = (edittext) activity .findviewbyid(r.id.registerpassword); edittext usercontact = (edittext) activity .findviewbyid(r.id.registercontact); string contact = usercontact.gettext().tostring(); string email = username.gettext().tostring(); string password = passwordedit.gettext().tostring(); log.v(email, password); userfunctions userfunction = new userfunctions(); jsonobject json = userfunction.registeruser(email, password, contact); // check login response try { if (json.getstring(key_success) != null) { // registererrormsg.settext(""); string res = json.getstring(key_success); if (integer.parseint(res) == 1) { // user registred // store user details in sqlite database databasehandler db = new databasehandler( activity.getapplicationcontext()); jsonobject json_user = json.getjsonobject("user"); // clear previous data in database userfunction.logoutuser(activity.getapplicationcontext()); db.adduser(json_user.getstring(key_email), json.getstring(key_uid), json_user.getstring(key_created_at)); // successful registration responsecode = 1; } else { // error in registration responsecode = 0; } } } catch (jsonexception e) { e.printstacktrace(); } return responsecode; } @override protected void onpostexecute(integer responsecode) { edittext username = (edittext) activity .findviewbyid(r.id.registeremail); edittext passwordedit = (edittext) activity .findviewbyid(r.id.registerpassword); string s = username.gettext().tostring(); if (responsecode == 1) { progressdialog.dismiss(); activity.registerreport(responsecode); username.settext(""); passwordedit.settext(""); } if (responsecode == 0) { progressdialog.dismiss(); activity.registerreport(responsecode); } } }
or error possibly php itself?
<?php /** * file handle api requests * accepts , post * * each request identified tag * response json data /** * check post request */ if (isset($_post['tag']) && $_post['tag'] != '') { // tag $tag = $_post['tag']; // include db handler require_once 'include/db_functions.php'; $db = new db_functions(); // response array $response = array("tag" => $tag, "success" => 0, "error" => 0); // check tag type if ($tag == 'login') { // request type check login $email = $_post['email']; $password = $_post['password']; // check user $user = $db->getuserbyemailandpassword($email, $password); if ($user != false) { // user found // echo json success = 1 $response["success"] = 1; $response["user"]["email"] = $user["email"]; $response["user"]["contact"] = $user["contact_no"]; $response["user"]["created_at"] = $user["year_joined"]; echo json_encode($response); } else { // user not found // echo json error = 1 $response["error"] = 1; $response["error_msg"] = "incorrect email or password!"; echo json_encode($response); } } else if ($tag == 'register') { // request type register new user $email = $_post['email']; $password = $_post['password']; $contact = $_post['contact']; // check if user existed if ($db->isuserexisted($email)) { // user existed - error response $response["error"] = 2; $response["error_msg"] = "user existed"; echo json_encode($response); } else { // store user $user = $db->storeuser($email, $contact_no, $password); if ($user) { // user stored $response["success"] = 1; $response["user"]["email"] = $user["email"]; $response["user"]["contact"] = $user["contact_no"]; $response["user"]["created_at"] = $user["year_joined"]; echo json_encode($response); } else { // user failed store $response["error"] = 1; $response["error_msg"] = "error occured in registartion"; echo json_encode($response); } } } else { echo "invalid request"; } } else { echo "access denied"; } ?>
change link , have error.
02-06 09:45:18.168: e/json(1697): <br />n<font size='1'><table class='xdebug-error' dir='ltr' border='1' cellspacing='0' cellpadding='1'>n<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> warning: require_once(include/db_functions.php) [<a href='function.require-once'>function.require-once</a>]: failed open stream: no such file or directory in d:\softwares\wamp\www\project\index.php on line <i>17</i></th></tr>n<tr><th align='left' bgcolor='#e9b96e' colspan='5'>call stack</th></tr>n<tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>time</th><th align='left' bgcolor='#eeeeec'>memory</th><th align='left' bgcolor='#eeeeec'>function</th><th align='left' bgcolor='#eeeeec'>location</th></tr>n<tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0009</td><td bgcolor='#eeeeec' align='right'>692600</td><td bgcolor='#eeeeec'>{main}( )</td><td title='d:\softwares\wamp\www\project\index.php' bgcolor='#eeeeec'>..\index.php<b>:</b>0</td></tr>n</table></font>n<br />n<font size='1'><table class='xdebug-error' dir='ltr' border='1' cellspacing='0' cellpadding='1'>n<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> fatal error: require_once() [<a href='function.require'>function.require</a>]: failed opening required 'include/db_functions.php' (include_path='.;c:\php\pear') in d:\softwares\wamp\www\project\index.php on line <i>17</i></th></tr>n<tr><th align='left' bgcolor='#e9b96e' colspan='5'>call stack</th></tr>n<tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>time</th><th align='left' bgcolor='#eeeeec'>memory</th><th align='left' bgcolor='#eeeeec'>function</th><th align='left' bgcolor='#eeeeec'>location</th></tr>n<tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0009</td><td bgcolor='#eeeeec' align='right'>692600</td><td bgcolor='#eeeeec'>{main}( )</td><td title='d:\softwares\wamp\www\project\index.php' bgcolor='#eeeeec'>..\index.php<b>:</b>0</td></tr>n</table></font>n
using wrong url. getting 404 error page reponse, , trying parse html json.
Comments
Post a Comment