String ServerURL = "your_php_file_url" ;
EditText first_name, last_name,p_address,phne_home,phne_office ;
Button button;
Spinner state_spinner,district_spinner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_activity_name);
/*==================================================================
Bind Your spinners and Edittext with the IDs Here
====================================================================*/
fname = (EditText)findViewById(R.id.fname);
/*==================================================================
Bind Your spinners and Edittext with the IDs ABove ^^^
====================================================================*/
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
GetData();
InsertData(f_name,l_name,address,phne_hme,phne_ofice,State,district);
}
});
}
public void GetData () {
String f_name = first_name.getText().toString();
String l_name = last_name.getText().toString();
String address = p_address.getText().toString();
String phne_hme = phne_home.getText().toString();
String phne_ofice = phne_office.getText().toString();
String State = state_spinner.getSelectedItem().toString();
String district = district_spinner.getSelectedItem().toString();
}
public void InsertData(final String fname, final String lname,final String address,final String phne_hme, final String phn_ofice, final String State, final String district){
class SendPostReqAsyncTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
String f_nameHolder = fname;
String l_nameHolder = lname;
String addressHolder = address;
String phne_hmeHolder = phne_hme;
String phne_oficeHolder = phne_ofice;
String StateHolder = State;
String districtHolder = district;
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("replace these names with your key given in PHP files", f_nameHolder));
nameValuePairs.add(new BasicNameValuePair("replace these names with your key given in PHP files",l_nameHolder));
nameValuePairs.add(new BasicNameValuePair("replace these names with your key given in PHP files", addressHolder));
nameValuePairs.add(new BasicNameValuePair("replace these names with your key given in PHP files", phne_hmeHolder));
nameValuePairs.add(new BasicNameValuePair("replace these names with your key given in PHP files", phne_oficeHolder));
nameValuePairs.add(new BasicNameValuePair("replace these names with your key given in PHP files", StateHolder));
nameValuePairs.add(new BasicNameValuePair("replace these names with your key given in PHP files", districtHolder));
try {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(ServerURL);
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
} catch (ClientProtocolException e) {
} catch (IOException e) {
}
return "Data Inserted Successfully";
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
Toast.makeText(MainActivity.this, "Data Submit Successfully", Toast.LENGTH_LONG).show();
}
}
SendPostReqAsyncTask sendPostReqAsyncTask = new SendPostReqAsyncTask();
sendPostReqAsyncTask.execute(fname,lname,address,phne_hme,phn_ofice,State,district);
}
Не забудьте добавить Org.Apache.Http.Legacy эту зависимость в ваш файл Gradle.