I have a simple form that submits a name to a database. the php works on the html form already.
xml for form
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:onClick="sumbitdata"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
tools:layout_constraintTop_creator="1"
tools:layout_constraintRight_creator="1"
android:layout_marginTop="53dp"
app:layout_constraintTop_toBottomOf="@+id/name"
tools:layout_constraintLeft_creator="1" />
<EditText
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:text="Name"
tools:layout_constraintTop_creator="1"
tools:layout_constraintRight_creator="1"
android:layout_marginEnd="69dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="222dp"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginRight="69dp" />
</android.support.constraint.ConstraintLayout>
java for from page
public class checklist extends AppCompatActivity {
EditText name;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_checklist);
name = (EditText)findViewById(R.id.name);
}
public void poslji (View view){
String checklist_data = name.getText().toString().trim();
String type ="mydata";
backgroundworker backgroundWorker = new backgroundworker(this);
backgroundWorker.execute(type, checklist_data);
}
}
background worker java
class backgroundworker extends AsyncTask<String,Void,String> {
Context context;
backgroundworker (Context ctx) {
context=ctx;
}
@Override
protected String doInBackground (String... params){
String type = params [0];
String submit_url= "http://mylocalwebsever/submit.php";
if(type.equals("mydata")) {
try {
String name = params[1];
URL url = new URL( submit_url );
HttpURLConnection httpURLConnection = (HttpURLConnection)
url.openConnection();
httpURLConnection.setRequestMethod( "POST" );
httpURLConnection.setDoOutput( true );
httpURLConnection.setDoInput( true );
OutputStream outputStream =
httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter( new
OutputStreamWriter( outputStream, "UTF-8" ) );
String post_data = URLEncoder.encode( "name", "UTF-8" ) +
"=" + URLEncoder.encode( name, "UTF-8");
bufferedWriter.write( post_data );
bufferedWriter.flush();
bufferedWriter.close();
InputStream inputStream = httpURLConnection.getInputStream
();
BufferedReader bufferedReader = new BufferedReader( new
InputStreamReader( inputStream, "iso-8859-1" ) );
String result = "";
String line = "";
while ((line = bufferedReader.readLine()) != null) {
result += line;
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
return result;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}