android - why this Error is Comming -
i want simplily android app connect php mysql, read many tutorial of php , mysql connect php android app every 1 running on emmulor, have no real device,so use emmulator testing.
why error commingfrom code
my java file
import java.io.bufferedreader; import java.io.inputstream; import java.io.inputstreamreader; import java.util.arraylist; import org.apache.http.httpentity; import org.apache.http.httpresponse; import org.apache.http.namevaluepair; import org.apache.http.client.httpclient; import org.apache.http.client.entity.urlencodedformentity; import org.apache.http.client.methods.httppost; import org.apache.http.impl.client.defaulthttpclient; import org.apache.http.message.basicnamevaluepair; import org.json.jsonobject; import android.support.v7.app.actionbaractivity; import android.support.v7.app.actionbar; import android.support.v4.app.fragment; import android.os.bundle; import android.util.log; import android.view.layoutinflater; import android.view.menu; import android.view.menuitem; import android.view.view; import android.view.viewgroup; import android.widget.button; import android.widget.edittext; import android.widget.toast; import android.os.build; public class mainactivity extends actionbaractivity { string name; string id; inputstream is=null; string result=null; string line=null; int code; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); final edittext e_id=(edittext) findviewbyid(r.id.edittext1); final edittext e_name=(edittext) findviewbyid(r.id.edittext2); button insert=(button) findviewbyid(r.id.button1); insert.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub id = e_id.gettext().tostring(); name = e_name.gettext().tostring(); insert(); } }); } public void insert() { arraylist<namevaluepair> namevaluepairs = new arraylist<namevaluepair> (); namevaluepairs.add(new basicnamevaluepair("id",id)); namevaluepairs.add(new basicnamevaluepair("name",name)); try { httpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost("http://127.0.0.1/android/insert.php"); httppost.setentity(new urlencodedformentity(namevaluepairs)); httpresponse response = httpclient.execute(httppost); httpentity entity = response.getentity(); = entity.getcontent(); log.e("pass 1", "connection success "); } catch(exception e) { log.e("fail 1", e.tostring()); toast.maketext(getapplicationcontext(), "invalid ip address", toast.length_long).show(); } try { bufferedreader reader = new bufferedreader (new inputstreamreader(is,"iso-8859-1"),8); stringbuilder sb = new stringbuilder(); while ((line = reader.readline()) != null) { sb.append(line + "\n"); } is.close(); result = sb.tostring(); log.e("pass 2", "connection success "); } catch(exception e) { log.e("fail 2", e.tostring()); } try { jsonobject json_data = new jsonobject(result); code=(json_data.getint("code")); if(code==1) { toast.maketext(getbasecontext(), "inserted successfully", toast.length_short).show(); } else { toast.maketext(getbasecontext(), "sorry, try again", toast.length_long).show(); } } catch(exception e) { log.e("fail 3", e.tostring()); } } @override public boolean oncreateoptionsmenu(menu menu) { getmenuinflater().inflate(r.menu.main, menu); return true; }
}
this php file
<?php $host="127.0.0.1"; $db="android"; $uname="root"; $pwd=""; $con = mysqli_connect("$host","$uname","$pwd") or die("connection failed"); mysqli_select_db($con,$db) or die("db selection failed"); $id=(isset($_request['id']) )?$_request['id']: ''; $name=(isset($_request['name']) ) ? $_reqest['name']: ''; $flag['code']=0; if($r=mysqli_query("insert sample values('$id','$name') ,$con")) { $flag['code']=1; echo"hi"; } print(json_encode($flag)); mysqli_close($con); ?>
and logcat window
09-30 03:35:16.800: e/fail 1(1064): android.os.networkonmainthreadexception 09-30 03:35:16.870: e/fail 2(1064): java.lang.nullpointerexception: lock == null 09-30 03:35:16.870: e/fail 3(1064): java.lang.nullpointerexception
this exception thrown when application attempts perform networking operation on main thread. run code in asynctask
:
private class networktask extends asynctask<string, void, void> { protected void doinbackground(string... urls) { try { <!--your network stuff goes here --> } catch (exception e) { } } protected void onpostexecute(void resut) { // todo: feed } }
Comments
Post a Comment