java - Getting an error when connecting from android to PHP server -
i have android application project connect php server write in codeigniter.
my jsonparser is:
package com.example.com.tourism; 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() { } // function json url // making http post or method public jsonobject makehttprequest(string url, string method, list<namevaluepair> params) { // making http request try { // 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(); } } 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; } }
and activity java code connect server is
package com.example.com.tourism; 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.view.view.onclicklistener; import android.widget.button; import android.widget.edittext; public class signup extends activity { edittext first,last,birth ,pass; private progressdialog pdialog; private static string url_create_user = "http://10.0.2.2/tourism/index.php/site/register"; jsonparser jsonparser = new jsonparser(); private static final string tag_success = "success"; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.sign_up); button signup=(button)findviewbyid(r.id.sign_up); first =(edittext) findviewbyid(r.id.edfname); last =(edittext) findviewbyid(r.id.edlname); pass =(edittext) findviewbyid(r.id.edpass); signup.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub new createnewuser().execute(); } }); } class createnewuser extends asynctask<string, string, string> { /** * before starting background thread show progress dialog * */ @override protected void onpreexecute() { super.onpreexecute(); pdialog = new progressdialog(signup.this); pdialog.setmessage("creating user.."); pdialog.setindeterminate(false); pdialog.setcancelable(true); pdialog.show(); } /** * creating product * */ protected string doinbackground(string... args) { string name = first.gettext().tostring(); string price = last.gettext().tostring(); string description = pass.gettext().tostring(); // building parameters list<namevaluepair> params = new arraylist<namevaluepair>(); params.add(new basicnamevaluepair("first_name", name)); params.add(new basicnamevaluepair("last_name", price)); params.add(new basicnamevaluepair("password", description)); // getting json object // note create product url accepts post method jsonobject json = jsonparser.makehttprequest(url_create_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 intent = new intent(getapplicationcontext(), mainactivity.class); startactivity(i); // closing screen finish(); } 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(); } }
and error in log cat
07-07 11:30:44.159: e/json parser(20514): error parsing data org.json.jsonexception: value <!doctype of type java.lang.string cannot converted jsonobject 07-07 11:30:44.189: w/dalvikvm(20514): threadid=11: thread exiting uncaught exception (group=0x40a71930) 07-07 11:30:44.229: e/androidruntime(20514): fatal exception: asynctask #1 07-07 11:30:44.229: e/androidruntime(20514): java.lang.runtimeexception: error occured while executing doinbackground() 07-07 11:30:44.229: e/androidruntime(20514): @ android.os.asynctask$3.done(asynctask.java:299) 07-07 11:30:44.229: e/androidruntime(20514): @ java.util.concurrent.futuretask.finishcompletion(futuretask.java:352) 07-07 11:30:44.229: e/androidruntime(20514): @ java.util.concurrent.futuretask.setexception(futuretask.java:219) 07-07 11:30:44.229: e/androidruntime(20514): @ java.util.concurrent.futuretask.run(futuretask.java:239) 07-07 11:30:44.229: e/androidruntime(20514): @ android.os.asynctask$serialexecutor$1.run(asynctask.java:230) 07-07 11:30:44.229: e/androidruntime(20514): @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1080) 07-07 11:30:44.229: e/androidruntime(20514): @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:573) 07-07 11:30:44.229: e/androidruntime(20514): @ java.lang.thread.run(thread.java:856) 07-07 11:30:44.229: e/androidruntime(20514): caused by: java.lang.nullpointerexception 07-07 11:30:44.229: e/androidruntime(20514): @ com.example.com.tourism.signup$createnewuser.doinbackground(signup.java:98) 07-07 11:30:44.229: e/androidruntime(20514): @ com.example.com.tourism.signup$createnewuser.doinbackground(signup.java:1) 07-07 11:30:44.229: e/androidruntime(20514): @ android.os.asynctask$2.call(asynctask.java:287) 07-07 11:30:44.229: e/androidruntime(20514): @ java.util.concurrent.futuretask.run(futuretask.java:234) 07-07 11:30:44.229: e/androidruntime(20514): ... 4 more 07-07 11:30:44.429: i/choreographer(20514): skipped 49 frames! application may doing work on main thread. 07-07 11:30:44.779: i/choreographer(20514): skipped 222 frames! application may doing work on main thread. 07-07 11:30:45.069: i/choreographer(20514): skipped 149 frames! application may doing work on main thread. 07-07 11:30:45.369: i/choreographer(20514): skipped 194 frames! application may doing work on main thread. 07-07 11:30:45.459: i/choreographer(20514): skipped 50 frames! application may doing work on main thread. 07-07 11:30:46.019: e/windowmanager(20514): activity com.example.com.tourism.signup has leaked window com.android.internal.policy.impl.phonewindow$decorview{40d743a8 v.e..... r.....id 0,0-304,96} added here 07-07 11:30:46.019: e/windowmanager(20514): android.view.windowleaked: activity com.example.com.tourism.signup has leaked window com.android.internal.policy.impl.phonewindow$decorview{40d743a8 v.e..... r.....id 0,0-304,96} added here 07-07 11:30:46.019: e/windowmanager(20514): @ android.view.viewrootimpl.<init>(viewrootimpl.java:354) 07-07 11:30:46.019: e/windowmanager(20514): @ android.view.windowmanagerglobal.addview(windowmanagerglobal.java:216) 07-07 11:30:46.019: e/windowmanager(20514): @ android.view.windowmanagerimpl.addview(windowmanagerimpl.java:69) 07-07 11:30:46.019: e/windowmanager(20514): @ android.app.dialog.show(dialog.java:281) 07-07 11:30:46.019: e/windowmanager(20514): @ com.example.com.tourism.signup$createnewuser.onpreexecute(signup.java:75) 07-07 11:30:46.019: e/windowmanager(20514): @ android.os.asynctask.executeonexecutor(asynctask.java:586) 07-07 11:30:46.019: e/windowmanager(20514): @ android.os.asynctask.execute(asynctask.java:534) 07-07 11:30:46.019: e/windowmanager(20514): @ com.example.com.tourism.signup$1.onclick(signup.java:57) 07-07 11:30:46.019: e/windowmanager(20514): @ android.view.view.performclick(view.java:4204) 07-07 11:30:46.019: e/windowmanager(20514): @ android.view.view$performclick.run(view.java:17355) 07-07 11:30:46.019: e/windowmanager(20514): @ android.os.handler.handlecallback(handler.java:725) 07-07 11:30:46.019: e/windowmanager(20514): @ android.os.handler.dispatchmessage(handler.java:92) 07-07 11:30:46.019: e/windowmanager(20514): @ android.os.looper.loop(looper.java:137) 07-07 11:30:46.019: e/windowmanager(20514): @ android.app.activitythread.main(activitythread.java:5041) 07-07 11:30:46.019: e/windowmanager(20514): @ java.lang.reflect.method.invokenative(native method) 07-07 11:30:46.019: e/windowmanager(20514): @ java.lang.reflect.method.invoke(method.java:511) 07-07 11:30:46.019: e/windowmanager(20514): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:793) 07-07 11:30:46.019: e/windowmanager(20514): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:560) 07-07 11:30:46.019: e/windowmanager(20514): @ dalvik.system.nativestart.main(native method)
i don't know problem is, please me code incorrect ??? if 1 have correct code please give me using codeigniter php , code call above
function register_get() { $json = array('status' => false ); if($this->input->post()==null){ $this -> response($json, 200); } $firstname = $this->post("first_name"); $lastname = $this->post("last_name"); $password = $this->post("password"); if(!$firstname || !$lastname || !$password){ $json['status'] = "wrong insert"; $this -> response($json, 200); } $this->load->model('data_model'); $result = $this->data_model->search($firstname, $lastname); if($result) { $this->data_model->insert($firstname,$lastname,$password); $json['status'] = true; } // here if false.. $this -> response($json, 200); }
there issues php. not passing json. need change script encode array json.
$result = json_encode($json)
returning actual json help. make sure not returning html either. java trying parse json response, responding <!doctype
...
Comments
Post a Comment