java - Send mobile number on click button to the Server URL in Android -
i want make app in user enter mobile number click on proceed button user mobile number send server url.here code
mainactivity.java
public class mainactivity extends activity { private static final string tag = "http connection"; private edittext mobileno; private button proceed; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.mobilenumber); mobileno = (edittext) findviewbyid(r.id.edittext); proceed = (button) findviewbyid(r.id.button); proceed.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { asynchttptask asynchttptask = new asynchttptask(); final string url = "http://javatechig.com/api/get_category_posts/?dev=1&slug=android"; string mobileno = mobileno.gettext().tostring(); new asynchttptask().execute(url); } }); } public class asynchttptask extends asynctask<string, void, integer> { private progressdialog pdialog; @override protected void onpreexecute() { super.onpreexecute(); pdialog = new progressdialog(mainactivity.this); pdialog.setmessage("attempting proceed.."); pdialog.setindeterminate(false); pdialog.setcancelable(true); pdialog.show(); pdialog.dismiss(); } @override protected integer doinbackground(string... params) { inputstream inputstream = null; httpurlconnection urlconnection = null; integer result = 0; try { /* forming th java.net.url object */ url url = new url(params[0]); string mobileno = mobileno.gettext().tostring(); urlconnection = (httpurlconnection) url.openconnection(); /* optional request header */ urlconnection.setrequestproperty("content-type", "application/json"); /* optional request header */ urlconnection.setrequestproperty("accept", "application/json"); /* request */ urlconnection.setrequestmethod("get"); int statuscode = urlconnection.getresponsecode(); /* 200 represents http ok */ if (statuscode == 200) { inputstream = new bufferedinputstream(urlconnection.getinputstream()); string response = convertinputstreamtostring(inputstream); parseresult(response); result = 1; // successful }else{ result = 0; //"failed fetch data!"; } } catch (exception e) { log.d(tag, e.getlocalizedmessage()); } return result; //"failed fetch data!"; } @override protected void onpostexecute(integer result) { if(result == 1){ toast.maketext(mainactivity.this,result, toast.length_long).show(); }else{ log.e(tag, "failed fetch data!"); } } } private string convertinputstreamtostring(inputstream inputstream) throws ioexception { bufferedreader bufferedreader = new bufferedreader( new inputstreamreader(inputstream)); string line ; string result=null ; while((line = bufferedreader.readline()) != null){ result += line; } /* close stream */ if(null!=inputstream){ inputstream.close(); } return result; } private void parseresult(string result) { try{ jsonobject response = new jsonobject(result); }catch (jsonexception e){ e.printstacktrace(); } } }
here xml file:
mobilenumber.xml
<?xml version="1.0" encoding="utf-8" ?> <linearlayout android:layout_width="match_parent" android:layout_height="match_parent" xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:background="@drawable/background" android:weightsum="1"> <linearlayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center_horizontal" android:layout_weight="1" android:gravity="center_horizontal" android:orientation="vertical" > <imageview android:id="@+id/boton" android:layout_width="110dp" android:layout_height="110dp" android:layout_gravity="center_horizontal" android:layout_marginstart="0dp" android:layout_margin="110dp" android:background="@drawable/logo" android:gravity="center_horizontal" /> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="your number" android:textsize="40sp" android:layout_marginright="4dp" android:layout_margintop="4dp" android:layout_marginleft="4dp" android:textcolor="#ffffff" android:textstyle="normal" /> <edittext android:layout_width="match_parent" android:layout_height="60dp" android:inputtype="number" android:ems="10" android:textsize="24sp" android:hint=" +91 enter number" android:digits="0123456789" android:textcolorhighlight="@color/abc_primary_text_material_dark" android:textcolorhint="#ffffff" android:textstyle="normal" android:background="#5fffffff" android:layout_margintop="20dp" android:layout_marginleft="40dp" android:layout_marginright="35dp" android:id="@+id/edittext" android:layout_gravity="center_horizontal" /> <button android:layout_width="match_parent" android:layout_height="60dp" android:textsize="20dp" android:text="proceed" android:textstyle="bold" android:layout_margintop="10dp" android:layout_marginleft="40dp" android:layout_marginright="35dp" android:layout_marginbottom="50dp" android:textcolor="@color/bright_foreground_inverse_material_light" android:background="@drawable/yellow" android:id="@+id/button" android:layout_gravity="center_horizontal" /> </linearlayout> </linearlayout>
you aren't passing mobileno while makin server call.
to so,
string mobileno = mobileno.gettext().tostring(); final string url = "http://javatechig.com/api/get_category_posts/?dev=1&slug=android&mobile_no=" + mobileno ;
replace mobile_no
appropriate parameter name.
Comments
Post a Comment