java - the app force close after submitting the data into mysql -
i tried internet tutorial inserting data mysql android in activity_main.xml, there button register new user.
put information such firstname, etc.
when user clicks submit button, should redirect activity_main.xml.
when click submit, app forced close, , data not inserted mysql database.
my question is, wrong in code ?
please give me advice. in code same had used android application. providing log traces here. appreciated...
regactivity.java
package com.example.estate; import java.util.arraylist; import java.util.list; import org.apache.http.namevaluepair; import org.apache.http.message.basicnamevaluepair; import org.json.jsonexception; import org.json.jsonobject; import android.app.activity; import android.app.progressdialog; import android.content.intent; import android.os.asynctask; import android.os.bundle; import android.util.log; import android.view.view; import android.widget.button; import android.widget.edittext; public class regactivity extends activity { // progress dialog private progressdialog pdialog; jsonparser jsonparser = new jsonparser(); edittext fname; edittext lname; edittext username; edittext password; edittext location; edittext contact; button btnreg; button btncancel; // url create new product private static string url_new_user = "http://192.168.43.236/estate_conny/new_user.php"; // json node names private static final string tag_success = "success"; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.reg_activity); // edit text fname = (edittext) findviewbyid(r.id.fname); lname = (edittext) findviewbyid(r.id.lname); username = (edittext) findviewbyid(r.id.uname); password = (edittext) findviewbyid(r.id.pass); location = (edittext) findviewbyid(r.id.addr); contact = (edittext) findviewbyid(r.id.contact); // create button btnreg = (button) findviewbyid(r.id.btnreg); btncancel = (button) findviewbyid(r.id.btncancel); // button click event btnreg.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { // creating new product in background thread new createnewproduct().execute(); } }); btncancel.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { finish(); intent = new intent(getapplicationcontext(), mainactivity.class); startactivity(i); } }); } /** * background async task create new product * */ class createnewproduct extends asynctask<string, string, string> { /** * before starting background thread show progress dialog * */ @override protected void onpreexecute() { super.onpreexecute(); pdialog = new progressdialog(regactivity.this); pdialog.setmessage("registering new user.."); pdialog.setindeterminate(false); pdialog.setcancelable(true); pdialog.show(); } /** * creating product * */ protected string doinbackground(string... args) { string firstname = fname.gettext().tostring(); string lastname = lname.gettext().tostring(); string username = username.gettext().tostring(); string password = password.gettext().tostring(); string address = location.gettext().tostring(); string contact = contact.gettext().tostring(); // building parameters list<namevaluepair> params = new arraylist<namevaluepair>(); params.add(new basicnamevaluepair("fname", firstname)); params.add(new basicnamevaluepair("lname", lastname)); params.add(new basicnamevaluepair("username", username)); params.add(new basicnamevaluepair("password", password)); params.add(new basicnamevaluepair("location", address)); params.add(new basicnamevaluepair("contact", contact)); // getting json object // note create product url accepts post method jsonobject json = jsonparser.makehttprequest(url_new_user, "post", params); // check log cat fro response log.d("create response", json.tostring()); // check success tag try { int success = json.getint(tag_success); if (success == 1) { // created product finish(); intent = new intent(getapplicationcontext(), mainactivity.class); startactivity(i); } else { // failed create product } } catch (jsonexception e) { e.printstacktrace(); } return null; } /** * after completing background task dismiss progress dialog * **/ protected void onpostexecute(string file_url) { // dismiss dialog once done pdialog.dismiss(); } } }
mainactivity.java
package com.example.estate; import java.util.arraylist; import java.util.list; import org.apache.http.namevaluepair; import org.apache.http.message.basicnamevaluepair; import org.json.jsonexception; import org.json.jsonobject; import com.example.estate.regactivity; //import com.example.androidhive.newproductactivity; import com.example.estate.r; import com.example.estate.jsonparser; import com.example.estate.mainactivity; //import com.example.estate.readcomments; import com.example.estate.regactivity; //import com.example.estate.login.attemptlogin; import android.os.asynctask; import android.os.bundle; import android.app.activity; import android.app.progressdialog; import android.content.intent; import android.util.log; import android.view.menu; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.edittext; import android.widget.toast; public class mainactivity extends activity implements onclicklistener{ button btnlogin; button btnreg; button btncancel; edittext username; edittext password; private progressdialog pdialog; jsonparser jsonparser = new jsonparser(); private static final string login_url = "http://192.168.43.101/estate_conny/login.php"; private static final string tag_success = "success"; private static final string tag_message = "message"; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); // buttons btnlogin = (button) findviewbyid(r.id.btnlogin ); btnreg = (button) findviewbyid(r.id.btncreate); btncancel = (button) findviewbyid(r.id.btnquit); username = (edittext) findviewbyid(r.id.inputname); password = (edittext) findviewbyid(r.id.inputpassword); btnreg.setonclicklistener(this); btnlogin.setonclicklistener(this); btncancel.setonclicklistener(this); } @override public void onclick(view v) { // todo auto-generated method stub switch (v.getid()) { case r.id.btnlogin: new attemptlogin().execute(); break; case r.id.btncreate: intent = new intent(this, regactivity.class); startactivity(i); break; case r.id.btnquit: finish(); break; default: break; } } class attemptlogin extends asynctask<string, string, string> { /** * before starting background thread show progress dialog * */ boolean failure = false; @override protected void onpreexecute() { super.onpreexecute(); pdialog = new progressdialog(mainactivity.this); pdialog.setmessage("logging in user..."); pdialog.setindeterminate(false); pdialog.setcancelable(true); pdialog.show(); } @override protected string doinbackground(string... args) { // todo auto-generated method stub // check success tag int success; string user = username.gettext().tostring(); string pass = password.gettext().tostring(); try { // building parameters list<namevaluepair> params = new arraylist<namevaluepair>(); params.add(new basicnamevaluepair("username", user)); params.add(new basicnamevaluepair("password", pass)); log.d("request!", "starting"); // getting product details making http request jsonobject json = jsonparser.makehttprequest( login_url, "post", params); // check log json response log.d("login attempt", json.tostring()); // json success tag success = json.getint(tag_success); if (success == 1) { log.d("login successful!", json.tostring()); intent = new intent(mainactivity.this, screenactivity.class); finish(); startactivity(i); return json.getstring(tag_message); }else{ log.d("login failure!", json.getstring(tag_message)); return json.getstring(tag_message); } } catch (jsonexception e) { e.printstacktrace(); } return null; } /** * after completing background task dismiss progress dialog * **/ protected void onpostexecute(string file_url) { // dismiss dialog once product deleted pdialog.dismiss(); if (file_url != null){ toast.maketext(mainactivity.this, file_url, toast.length_long).show(); } } } }
new_user.php
<?php /* * following code create new product row * product details read http post request */ // array json response $response = array(); / check required fields if (isset($_post['fname']) && isset($_post['lname']) && isset($_post['username']) && isset($_post['password']) && isset($_post['location']) && isset($_post['contact'])) { $fname = $_post['fname']; $lname = $_post['lname']; $username = $_post['username']; $password = $_post['password']; $location = $_post['location']; $contact = $_post['contact']; // include db connect class require_once __dir__ . '/db_connect.php'; // connecting db $db = new db_connect(); // mysql inserting new row $result = mysql_query("insert user(firstname, lastname, username, password, location, contact) values('$fname', '$lname', '$username','$password','$location','$contact')"); // check if row inserted or not if ($result) { // inserted database $response["success"] = 1; $response["message"] = "user registered."; // echoing json response echo json_encode($response); } else { // failed insert row $response["success"] = 0; $response["message"] = "oops! error occurred."; // echoing json response echo json_encode($response); } } else { // required field missing $response["success"] = 0; $response["message"] = "required field(s) missing"; // echoing json response echo json_encode($response); } ?>
log_traces:
process: agrawal.trial.server, pid: 21767 java.lang.runtimeexception: error occured while executing doinbackground() @ android.os.asynctask$3.done(asynctask.java:300) @ java.util.concurrent.futuretask.finishcompletion(futuretask.java:355) @ java.util.concurrent.futuretask.setexception(futuretask.java:222) @ java.util.concurrent.futuretask.run(futuretask.java:242) @ android.os.asynctask$serialexecutor$1.run(asynctask.java:231) @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1112) @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:587) @ java.lang.thread.run(thread.java:841) caused by: java.lang.runtimeexception: can't create handler inside thread has not called looper.prepare() @ android.os.handler.<init>(handler.java:200) @ android.os.handler.<init>(handler.java:114) @ android.widget.toast$tn.<init>(toast.java:345) @ android.widget.toast.<init>(toast.java:100) @ android.widget.toast.maketext(toast.java:256) @ agrawal.trial.server.regactivity$createnewproduct.doinbackground(regactivity.java:116) @ agrawal.trial.server.regactivity$createnewproduct.doinbackground(regactivity.java:89) @ android.os.asynctask$2.call(asynctask.java:288) @ java.util.concurrent.futuretask.run(futuretask.java:237) @ android.os.asynctask$serialexecutor$1.run(asynctask.java:231) @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1112) @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:587) @ java.lang.thread.run(thread.java:841) java.lang.illegalargumentexception: http entity may not null @ org.apache.http.util.entityutils.tostring(entityutils.java:110) @ org.apache.http.util.entityutils.tostring(entityutils.java:146) @ miui.util.errorreport.b(sourcefile:363) @ miui.util.errorreport.sendreportrequest(sourcefile:320) @ miui.util.errorreport$2.a(sourcefile:336) @ miui.util.errorreport$2.doinbackground(sourcefile:333) @ android.os.asynctask$2.call(asynctask.java:288) @ java.util.concurrent.futuretask.run(futuretask.java:237) @ android.os.asynctask$serialexecutor$1.run(asynctask.java:231) @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1112) @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:587) @ java.lang.thread.run(thread.java:841) 12-01 20:31:48.771 1057-1225/? w/contextimpl﹕ calling method in system process without qualified user: android.app.contextimpl.bindservice:1585 android.content.contextwrapper.bindservice:517 miui.os.dropboxmanager.b:361 miui.os.dropboxmanager.a:350 miui.os.dropboxmanager.addtext:314 12-01 20:31:48.801 1138-1150/? w/messagequeue﹕ handler (com.miui.internal.server.dropboxmanagerservice$2) {428282d8} sending message handler on dead thread
if (success == 1) { // created product intent = new intent(getapplicationcontext(), mainactivity.class); startactivity(i); finish(); //replace position of } else { // failed create product }
put in onpostexecute block remove in ground
Comments
Post a Comment