android - HTTP Post not working JAVA -
this question has answer here:
the problem
whenever click login button on mobile app, app force closes. there ton of errors fill console..
basically i'm trying send user has typed in 2 text boxes .php page , session key back, used let app know user logged in.
however, these errors have no idea how fix, , don't know why force closing.
activity_main.xml
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:background="#004f00"> <linearlayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerhorizontal="true" android:layout_centervertical="true" android:orientation="vertical" > <imageview android:id="@+id/imageview1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/logo" android:contentdescription="@string/login_logo_desc" android:layout_marginbottom="10dp"/> <edittext android:id="@+id/username" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/username" android:inputtype="text" android:padding="5dp" android:textcolor="#ffffff" android:textcolorhint="#ffffff" android:layout_marginbottom="10dp"/> <edittext android:id="@+id/password" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/password" android:inputtype="textpassword" android:padding="5dp" android:textcolor="#ffffff" android:textcolorhint="#ffffff" android:layout_marginbottom="10dp"/> <button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical|center_horizontal" android:text="@string/button_login" android:textcolor="#ffffff" android:textcolorhint="#ffffff" android:onclick="login"/> </linearlayout>
mainactivity.java
public class mainactivity extends activity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main, menu); return true; } public void login(view v){ //create new httpclient , post header httpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost("http://sociobubble.com/m/mobilogin.php"); list<basicnamevaluepair> namevaluepairs = new arraylist<basicnamevaluepair>(4); try { edittext username = (edittext)findviewbyid(r.id.username); string username = username.gettext().tostring(); edittext password = (edittext)findviewbyid(r.id.password); string password = password.gettext().tostring(); // add data namevaluepairs.add(new basicnamevaluepair("username", username)); namevaluepairs.add(new basicnamevaluepair("password", password)); namevaluepairs.add(new basicnamevaluepair("client", "mobile")); httppost.setentity(new urlencodedformentity(namevaluepairs)); // execute http post request httpresponse response = httpclient.execute(httppost); httpentity entity = response.getentity(); inputstream = entity.getcontent(); log.i("postdata", response.getstatusline().tostring()); } catch (ioexception e) { log.e("log_tag", "error in http connection "+e.tostring()); } }
}
the errors
07-23 18:03:23.833: e/androidruntime(27330): fatal exception: main 07-23 18:03:23.833: e/androidruntime(27330): java.lang.illegalstateexception: not execute method of activity 07-23 18:03:23.833: e/androidruntime(27330): @ android.view.view$1.onclick(view.java:3600) 07-23 18:03:23.833: e/androidruntime(27330): @ android.view.view.performclick(view.java:4106) 07-23 18:03:23.833: e/androidruntime(27330): @ android.view.view$performclick.run(view.java:17052) 07-23 18:03:23.833: e/androidruntime(27330): @ android.os.handler.handlecallback(handler.java:615) 07-23 18:03:23.833: e/androidruntime(27330): @ android.os.handler.dispatchmessage(handler.java:92) 07-23 18:03:23.833: e/androidruntime(27330): @ android.os.looper.loop(looper.java:137) 07-23 18:03:23.833: e/androidruntime(27330): @ android.app.activitythread.main(activitythread.java:5059) 07-23 18:03:23.833: e/androidruntime(27330): @ java.lang.reflect.method.invokenative(native method) 07-23 18:03:23.833: e/androidruntime(27330): @ java.lang.reflect.method.invoke(method.java:511) 07-23 18:03:23.833: e/androidruntime(27330): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:792) 07-23 18:03:23.833: e/androidruntime(27330): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:555) 07-23 18:03:23.833: e/androidruntime(27330): @ dalvik.system.nativestart.main(native method) 07-23 18:03:23.833: e/androidruntime(27330): caused by: java.lang.reflect.invocationtargetexception 07-23 18:03:23.833: e/androidruntime(27330): @ java.lang.reflect.method.invokenative(native method) 07-23 18:03:23.833: e/androidruntime(27330): @ java.lang.reflect.method.invoke(method.java:511) 07-23 18:03:23.833: e/androidruntime(27330): @ android.view.view$1.onclick(view.java:3595) 07-23 18:03:23.833: e/androidruntime(27330): ... 11 more 07-23 18:03:23.833: e/androidruntime(27330): caused by: android.os.networkonmainthreadexception 07-23 18:03:23.833: e/androidruntime(27330): @ android.os.strictmode$androidblockguardpolicy.onnetwork(strictmode.java:1131) 07-23 18:03:23.833: e/androidruntime(27330): @ java.net.inetaddress.lookuphostbyname(inetaddress.java:385) 07-23 18:03:23.833: e/androidruntime(27330): @ java.net.inetaddress.getallbynameimpl(inetaddress.java:236) 07-23 18:03:23.833: e/androidruntime(27330): @ java.net.inetaddress.getallbyname(inetaddress.java:214) 07-23 18:03:23.833: e/androidruntime(27330): @ org.apache.http.impl.conn.defaultclientconnectionoperator.openconnection(defaultclientconnectionoperator.java:137) 07-23 18:03:23.833: e/androidruntime(27330): @ org.apache.http.impl.conn.abstractpoolentry.open(abstractpoolentry.java:164) 07-23 18:03:23.833: e/androidruntime(27330): @ org.apache.http.impl.conn.abstractpooledconnadapter.open(abstractpooledconnadapter.java:119) 07-23 18:03:23.833: e/androidruntime(27330): @ org.apache.http.impl.client.defaultrequestdirector.execute(defaultrequestdirector.java:360) 07-23 18:03:23.833: e/androidruntime(27330): @ org.apache.http.impl.client.abstracthttpclient.execute(abstracthttpclient.java:555) 07-23 18:03:23.833: e/androidruntime(27330): @ org.apache.http.impl.client.abstracthttpclient.execute(abstracthttpclient.java:487) 07-23 18:03:23.833: e/androidruntime(27330): @ org.apache.http.impl.client.abstracthttpclient.execute(abstracthttpclient.java:465) 07-23 18:03:23.833: e/androidruntime(27330): @ com.sociobubble.app.mainactivity.login(mainactivity.java:64) 07-23 18:03:23.833: e/androidruntime(27330): ... 14 more
you can not make network calls on main thread. please read exception stacktrace , google networkonmainthreadexception
Comments
Post a Comment