php - getting Error converting result java.lang.NullPointerException & Error parsing data org.json.JSONException: End of input at character 0 of -
php - getting Error converting result java.lang.NullPointerException & Error parsing data org.json.JSONException: End of input at character 0 of -
hello new android , developing simple login , registration android app using mysql, php , json. have created database , tables in xampp(local server). whenever tried register getting ton of errors. there similar questions answers didn't help me out. in advance.
here code
jsonparser
import java.io.bufferedreader; import java.io.ioexception; import java.io.inputstream; import java.io.inputstreamreader; import java.io.unsupportedencodingexception; import java.util.list; import org.apache.http.httpentity; import org.apache.http.httpresponse; import org.apache.http.namevaluepair; import org.apache.http.client.clientprotocolexception; import org.apache.http.client.entity.urlencodedformentity; import org.apache.http.client.methods.httpget; import org.apache.http.client.methods.httppost; import org.apache.http.client.utils.urlencodedutils; import org.apache.http.impl.client.defaulthttpclient; import org.json.jsonexception; import org.json.jsonobject; import android.util.log; public class jsonparser { static inputstream = null; static jsonobject jobj = null; static string json = ""; // constructor public jsonparser() { } public jsonobject getjsonfromurl(string url) { // making http request seek { // defaulthttpclient defaulthttpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost(url); httpresponse httpresponse = httpclient.execute(httppost); httpentity httpentity = httpresponse.getentity(); = httpentity.getcontent(); } grab (unsupportedencodingexception e) { e.printstacktrace(); } grab (clientprotocolexception e) { e.printstacktrace(); } grab (ioexception e) { e.printstacktrace(); } seek { 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(); } grab (exception e) { log.e("buffer error", "error converting result " + e.tostring()); } // seek parse string json object seek { jobj = new jsonobject(json); } grab (jsonexception e) { log.e("json parser", "error parsing info " + e.tostring()); } // homecoming json string homecoming jobj; } // function json url // making http post or mehtod public jsonobject makehttprequest(string url, string method, list<namevaluepair> params) { // making http request seek { // check request method if(method == "post"){ // request method post // defaulthttpclient 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"){ // request method 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(); } } grab (unsupportedencodingexception e) { e.printstacktrace(); } grab (clientprotocolexception e) { e.printstacktrace(); } grab (ioexception e) { e.printstacktrace(); } seek { 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(); } grab (exception e) { log.e("buffer error", "error converting result " + e.tostring()); } // seek parse string json object seek { jobj = new jsonobject(json); } grab (jsonexception e) { log.e("json parser", "error parsing info " + e.tostring()); } // homecoming json string homecoming jobj; } }
register.java
import android.app.activity; import android.os.bundle; 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.progressdialog; import android.content.intent; import android.os.asynctask; import android.util.log; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.edittext; import android.widget.toast; // creating java class public class register extends activity implements onclicklistener{ // creating variables username , password... private edittext user,pass; private button register; // creating progressdialog... private progressdialog pdialog; // creating reference jsonparser... jsonparser jsonparser = new jsonparser(); // getting url run php register script.. // here using localserver since dont have access real server.. private static final string login_url = "http://http://127.0.0.1/webservice/register.php"; // creating ids messages... private static final string tag_success = "success"; private static final string tag_message = "message"; // creating oncreate method... @override protected void oncreate(bundle savedinstancestate) { // todo auto-generated method stub super.oncreate(savedinstancestate); setcontentview(r.layout.register); // getting references username,password , button elements user = (edittext) findviewbyid(r.id.get_username); pass = (edittext) findviewbyid(r.id.get_password); register = (button) findviewbyid(r.id.submit); // creating onclicklistener button... register.setonclicklistener(this); } @override public void onclick(view v) { // getting method create new user... new createuser().execute(); intent intent = new intent(this, usersuccess.class); } // creating new class called createuser... class createuser extends asynctask<string, string, string>{ /** * before starting background thread show progress dialog * */ boolean failure = false; @override protected void onpreexecute() { super.onpreexecute(); pdialog = new progressdialog(register.this); pdialog.setmessage("creating user..."); pdialog.setindeterminate(false); pdialog.setcancelable(true); pdialog.show(); } @override protected string doinbackground(string... params) { // check success tag int success; string username = user.gettext().tostring(); string password = pass.gettext().tostring(); seek { // building parameters list<namevaluepair> params1 = new arraylist<namevaluepair>(); params1.add(new basicnamevaluepair("username", username)); params1.add(new basicnamevaluepair("password", password)); log.d("request!", "starting"); //posting user info script jsonobject json = jsonparser.makehttprequest( login_url, "post", params1); // total json response log.d("login attempt", json.tostring()); // json success element success = json.getint(tag_success); if (success == 1) { log.d("user created!", json.tostring()); finish(); homecoming json.getstring(tag_message); }else{ log.d("login failure!", json.getstring(tag_message)); homecoming json.getstring(tag_message); } } grab (jsonexception e) { e.printstacktrace(); } homecoming null; } /** * after completing background task dismiss progress dialog * **/ protected void onpostexecute(string file_url) { // dismiss dialog 1 time product deleted pdialog.dismiss(); if (file_url != null){ toast.maketext(register.this, file_url, toast.length_long).show(); } } } }
login.java
import android.app.activity; import android.content.intent; import android.os.bundle; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; //creating class extends activity class public class loginscreen extends activity implements onclicklistener{ @override protected void oncreate(bundle savedinstancestate) { // todo auto-generated method stub super.oncreate(savedinstancestate); setcontentview(r.layout.login_screen); button signup = (button) findviewbyid(r.id.signup); signup.setonclicklistener(this); } @override public void onclick(view v) { // creating intent signup process.. intent = new intent(loginscreen.this,register.class); startactivity(i); } }
this logcat:
class="snippet-code-html lang-html prettyprint-override">10-31 12:45:28.441: d/request!(359): starting 10-31 12:45:30.041: w/system.err(359): java.net.unknownhostexception: http 10-31 12:45:30.041: w/system.err(359): at java.net.inetaddress.lookuphostbyname(inetaddress.java:506) 10-31 12:45:30.051: w/system.err(359): at java.net.inetaddress.getallbynameimpl(inetaddress.java:294) 10-31 12:45:30.051: w/system.err(359): at java.net.inetaddress.getallbyname(inetaddress.java:256) 10-31 12:45:30.051: w/system.err(359): at org.apache.http.impl.conn.defaultclientconnectionoperator.openconnection(defaultclientconnectionoperator.java:136) 10-31 12:45:30.051: w/system.err(359): at org.apache.http.impl.conn.abstractpoolentry.open(abstractpoolentry.java:164) 10-31 12:45:30.061: w/system.err(359): at org.apache.http.impl.conn.abstractpooledconnadapter.open(abstractpooledconnadapter.java:119) 10-31 12:45:30.061: w/system.err(359): at org.apache.http.impl.client.defaultrequestdirector.execute(defaultrequestdirector.java:359) 10-31 12:45:30.071: w/system.err(359): at org.apache.http.impl.client.abstracthttpclient.execute(abstracthttpclient.java:555) 10-31 12:45:30.071: w/system.err(359): at org.apache.http.impl.client.abstracthttpclient.execute(abstracthttpclient.java:487) 10-31 12:45:30.080: w/system.err(359): at org.apache.http.impl.client.abstracthttpclient.execute(abstracthttpclient.java:465) 10-31 12:45:30.080: w/system.err(359): at com.augustasoftsol.gmoapp.jsonparser.makehttprequest(jsonparser.java:96) 10-31 12:45:30.080: w/system.err(359): at com.augustasoftsol.gmoapp.register$createuser.doinbackground(register.java:99) 10-31 12:45:30.080: w/system.err(359): at com.augustasoftsol.gmoapp.register$createuser.doinbackground(register.java:1) 10-31 12:45:30.091: w/system.err(359): at android.os.asynctask$2.call(asynctask.java:185) 10-31 12:45:30.101: w/system.err(359): at java.util.concurrent.futuretask$sync.innerrun(futuretask.java:306) 10-31 12:45:30.101: w/system.err(359): at java.util.concurrent.futuretask.run(futuretask.java:138) 10-31 12:45:30.101: w/system.err(359): at java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1088) 10-31 12:45:30.132: w/system.err(359): at java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:581) 10-31 12:45:30.132: w/system.err(359): at java.lang.thread.run(thread.java:1019) 10-31 12:45:30.132: e/buffer error(359): error converting result java.lang.nullpointerexception 10-31 12:45:30.141: e/json parser(359): error parsing info org.json.jsonexception: end of input @ character 0 of 10-31 12:45:30.151: w/dalvikvm(359): threadid=9: thread exiting uncaught exception (group=0x40015560) 10-31 12:45:30.171: e/androidruntime(359): fatal exception: asynctask #1 10-31 12:45:30.171: e/androidruntime(359): java.lang.runtimeexception: error occured while executing doinbackground() 10-31 12:45:30.171: e/androidruntime(359): at android.os.asynctask$3.done(asynctask.java:200) 10-31 12:45:30.171: e/androidruntime(359): at java.util.concurrent.futuretask$sync.innersetexception(futuretask.java:274) 10-31 12:45:30.171: e/androidruntime(359): at java.util.concurrent.futuretask.setexception(futuretask.java:125) 10-31 12:45:30.171: e/androidruntime(359): at java.util.concurrent.futuretask$sync.innerrun(futuretask.java:308) 10-31 12:45:30.171: e/androidruntime(359): at java.util.concurrent.futuretask.run(futuretask.java:138) 10-31 12:45:30.171: e/androidruntime(359): at java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1088) 10-31 12:45:30.171: e/androidruntime(359): at java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:581) 10-31 12:45:30.171: e/androidruntime(359): at java.lang.thread.run(thread.java:1019) 10-31 12:45:30.171: e/androidruntime(359): caused by: java.lang.nullpointerexception 10-31 12:45:30.171: e/androidruntime(359): at com.augustasoftsol.gmoapp.register$createuser.doinbackground(register.java:103) 10-31 12:45:30.171: e/androidruntime(359): at com.augustasoftsol.gmoapp.register$createuser.doinbackground(register.java:1) 10-31 12:45:30.171: e/androidruntime(359): at android.os.asynctask$2.call(asynctask.java:185) 10-31 12:45:30.171: e/androidruntime(359): at java.util.concurrent.futuretask$sync.innerrun(futuretask.java:306) 10-31 12:45:30.171: e/androidruntime(359): ... 4 more 10-31 12:45:32.121: e/windowmanager(359): activity com.augustasoftsol.gmoapp.register has leaked window com.android.internal.policy.impl.phonewindow$decorview@405a3fe0 added here 10-31 12:45:32.121: e/windowmanager(359): android.view.windowleaked: activity com.augustasoftsol.gmoapp.register has leaked window com.android.internal.policy.impl.phonewindow$decorview@405a3fe0 added here 10-31 12:45:32.121: e/windowmanager(359): at android.view.viewroot.<init>(viewroot.java:258) 10-31 12:45:32.121: e/windowmanager(359): at android.view.windowmanagerimpl.addview(windowmanagerimpl.java:148) 10-31 12:45:32.121: e/windowmanager(359): at android.view.windowmanagerimpl.addview(windowmanagerimpl.java:91) 10-31 12:45:32.121: e/windowmanager(359): at android.view.window$localwindowmanager.addview(window.java:424) 10-31 12:45:32.121: e/windowmanager(359): at android.app.dialog.show(dialog.java:241) 10-31 12:45:32.121: e/windowmanager(359): at com.augustasoftsol.gmoapp.register$createuser.onpreexecute(register.java:82) 10-31 12:45:32.121: e/windowmanager(359): at android.os.asynctask.execute(asynctask.java:391) 10-31 12:45:32.121: e/windowmanager(359): at com.augustasoftsol.gmoapp.register.onclick(register.java:63) 10-31 12:45:32.121: e/windowmanager(359): at android.view.view.performclick(view.java:2485) 10-31 12:45:32.121: e/windowmanager(359): at android.view.view$performclick.run(view.java:9080) 10-31 12:45:32.121: e/windowmanager(359): at android.os.handler.handlecallback(handler.java:587) 10-31 12:45:32.121: e/windowmanager(359): at android.os.handler.dispatchmessage(handler.java:92) 10-31 12:45:32.121: e/windowmanager(359): at android.os.looper.loop(looper.java:123) 10-31 12:45:32.121: e/windowmanager(359): at android.app.activitythread.main(activitythread.java:3683) 10-31 12:45:32.121: e/windowmanager(359): at java.lang.reflect.method.invokenative(native method) 10-31 12:45:32.121: e/windowmanager(359): at java.lang.reflect.method.invoke(method.java:507) 10-31 12:45:32.121: e/windowmanager(359): at com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:839) 10-31 12:45:32.121: e/windowmanager(359): at com.android.internal.os.zygoteinit.main(zygoteinit.java:597) 10-31 12:45:32.121: e/windowmanager(359): at dalvik.system.nativestart.main(native method) 10-31 12:45:35.381: i/process(359): sending signal. pid: 359 sig: 9
when using local server , testing on emulator
use,
private static final string login_url = "http://10.0.2.2/webservice/register.php";
and if testing application on real device, utilize ip of computer hosting local server.
i hope helps!
java php android mysql json
Comments
Post a Comment