java - Registration activity freezing on phones -
i'm stuck @ registration page of app. i've tested on avd , works fine there noted logcat heavily stormed system logs.
the scene right when test app on phone login activity works fine when go registration page activity freezes & have no option other force closing myself or wait phone ask me if want force close it. here code of registration page.
the activity name enroll.java
package com.example.dell.iblood; import android.content.intent; import android.os.bundle; import android.os.strictmode; import android.support.v7.app.actionbaractivity; import android.util.log; import android.view.menu; import android.view.menuitem; import android.view.view; import android.widget.adapterview; import android.widget.arrayadapter; import android.widget.button; import android.widget.edittext; import android.widget.spinner; import android.widget.toast; 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.jsonarray; import org.json.jsonobject; import java.io.bufferedreader; import java.io.inputstream; import java.io.inputstreamreader; import java.util.arraylist; import java.util.list; public class enroll extends actionbaractivity implements view.onclicklistener,adapterview.onitemselectedlistener { edittext name, passwordone, passwordtwo, age, address,phone, email; button register, reset; spinner states,city,bloodgroup; string val = ""; inputstream = null; string ip = "http://iblood.byethost3.com/spdemo.php"; string line = null; string result = null; list<string> list,list1; string name; string paso; string past; string age; string bgroup; int code; string add; string state; string city; string phone; string email; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_enroll); strictmode.threadpolicy policy = new strictmode.threadpolicy.builder().permitall().build(); strictmode.setthreadpolicy(policy); register = (button) findviewbyid(r.id.regbutton); reset = (button) findviewbyid(r.id.resbutton); name = (edittext) findviewbyid(r.id.nameu); passwordone = (edittext) findviewbyid(r.id.passuone); passwordtwo = (edittext) findviewbyid(r.id.passutwo); age = (edittext) findviewbyid(r.id.ageu); bloodgroup = (spinner) findviewbyid(r.id.bgroupu); address = (edittext) findviewbyid(r.id.addressu); states = (spinner) findviewbyid(r.id.stateu); city = (spinner) findviewbyid(r.id.cityu); states = (spinner) findviewbyid(r.id.stateu); phone = (edittext) findviewbyid(r.id.mobu); email = (edittext) findviewbyid(r.id.mailu); bloodgroup.setonitemselectedlistener(this); states.setonitemselectedlistener(this); city.setonitemselectedlistener(this); selectall(); arrayadapter<string> adp = new arrayadapter<string>(this, android.r.layout.simple_list_item_1, list); // set list arrayadapter states.setadapter(adp); // set selectbg(); arrayadapter<string> adp3 = new arrayadapter<string>(this, android.r.layout.simple_list_item_1, list); // set list arrayadapter bloodgroup.setadapter(adp3); // set register.setonclicklistener(this); reset.setonclicklistener(this); } @override public void onitemselected(adapterview<?> adapterview, view view, int i, long l) { val = states.getselecteditem().tostring(); selectcity(); arrayadapter<string> adp1 = new arrayadapter<string>(this, android.r.layout.simple_list_item_1, list1); // set list arrayadapter city.setadapter(adp1); } @override public void onnothingselected(adapterview<?> adapterview) { } private void selectall() { // todo auto-generated method stub try { httpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost(ip); httpresponse response = httpclient.execute(httppost); httpentity entity = response.getentity(); = entity.getcontent(); } catch (exception e) { log.e("webservice 1", e.tostring()); } 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(); } catch (exception e) { log.e("webservice 2", e.tostring()); } try { jsonarray ja = new jsonarray(result); jsonobject jo = null; list = new arraylist<string>(); (int = 0; < ja.length(); i++) { jo = ja.getjsonobject(i); list.add(jo.getstring("nameofstate")); } } catch (exception e) { log.e("webservice 3", e.tostring()); } } private void selectcity() { arraylist<namevaluepair> namevaluepairs = new arraylist<namevaluepair>(); namevaluepairs.add(new basicnamevaluepair("city",val)); try { httpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost("http://iblood.byethost3.com/cities.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 { jsonarray ja = new jsonarray(result); jsonobject jo = null; list1 = new arraylist<string>(); (int = 0; < ja.length(); i++) { jo = ja.getjsonobject(i); list1.add(jo.getstring("cityname")); } } catch (exception e) { log.e("webservice 13", e.tostring()); } } private void selectbg() { // todo auto-generated method stub try { httpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost("http://iblood.byethost3.com/bloodgroup.php"); httpresponse response = httpclient.execute(httppost); httpentity entity = response.getentity(); = entity.getcontent(); } catch (exception e) { log.e("webservice 1", e.tostring()); } 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(); } catch (exception e) { log.e("webservice 2", e.tostring()); } try { jsonarray ja = new jsonarray(result); jsonobject jo = null; list = new arraylist<string>(); (int = 0; < ja.length(); i++) { jo = ja.getjsonobject(i); list.add(jo.getstring("bloodgroup")); } } catch (exception e) { log.e("webservice 3", e.tostring()); } } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.menu_enroll, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); //noinspection simplifiableifstatement if (id == r.id.action_settings) { return true; } return super.onoptionsitemselected(item); } @override public void onclick(view view) { int = view.getid(); name = name.gettext().tostring(); paso = passwordone.gettext().tostring(); past = passwordtwo.gettext().tostring(); age = age.gettext().tostring(); bgroup=bloodgroup.getselecteditem().tostring(); add = address.gettext().tostring(); state= states.getselecteditem().tostring(); city=city.getselecteditem().tostring(); phone = phone.gettext().tostring(); email = email.gettext().tostring(); switch (i) { case r.id.regbutton: insert(); break; case r.id.resbutton: name.settext(" "); passwordone.settext(""); passwordtwo.settext(""); age.settext(" "); bloodgroup.setselection(0); address.settext(" "); states.setselection(0); city.setselection(0); phone.settext(" "); email.settext(" "); break; } } public void insert() { arraylist<namevaluepair> namevaluepairs = new arraylist<namevaluepair>(); namevaluepairs.add(new basicnamevaluepair("nameu", name)); namevaluepairs.add(new basicnamevaluepair("passuone", paso)); namevaluepairs.add(new basicnamevaluepair("passutwo", past)); namevaluepairs.add(new basicnamevaluepair("ageu", age)); namevaluepairs.add(new basicnamevaluepair("bloodgroup",bgroup )); namevaluepairs.add(new basicnamevaluepair("addressu", add)); namevaluepairs.add(new basicnamevaluepair("nameofstate", state)); namevaluepairs.add(new basicnamevaluepair("cityname", city)); namevaluepairs.add(new basicnamevaluepair("mobu", phone)); namevaluepairs.add(new basicnamevaluepair("mailu", email)); try { httpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost("http://iblood.byethost3.com/reg.php"); httppost.setentity(new urlencodedformentity(namevaluepairs)); httpresponse response = httpclient.execute(httppost); httpentity entity = response.getentity(); = entity.getcontent(); log.e("pass 11", "connection success "); } catch (exception e) { log.e("fail 1", e.tostring()); toast.maketext(getapplicationcontext(), "invalid ip address"+e, 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 12", "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) { intent l = new intent(this, mainpage.class); startactivity(l); toast.maketext(getbasecontext(), "registered \n please login", toast.length_short).show(); } else { intent la = new intent(this, mainpage.class); startactivity(la); toast.maketext(getbasecontext(), "sorry, try again", toast.length_long).show(); } } catch (exception e) { log.e("fail 3", e.tostring()); } } }
as mentioned @tomrichardson basic problem doing time consuming operations on ui thread of activity.
as modern environments 'main' thread of activity should available process user input making program responsive. if want time consuming (such making http request) need in different background thread.
typically sequence goes this: 1. ui thread pops progress spinner show it's working , starts task thread. 2. task thread works , on completion signals uithread 3. ui thread clears progress spinner , whatever needs next.
if sounds bit complex try reading android's asynctask class implements of these bits in direct , easy way understand.
asynctask has serious issues , knowledge of android develops might consider leaving behind easy place start task yours.
Comments
Post a Comment