import android.content.Intent; import android.os.AsyncTask; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.ProgressBar; import android.widget.TextView; import android.widget.Toast; import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.sql.Connection; public class AddActivity extends AppCompatActivity { private String urlString = "http://mobiledev.ronaldramos.info/writecontact.php"; String urlFinal; TextView tvName; TextView tvLocation; TextView tvNumber; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_add); tvName = (TextView)findViewById(R.id.editText); tvLocation = (TextView)findViewById(R.id.editText2); tvNumber = (TextView)findViewById(R.id.editText3); } public void process(View v){ urlFinal = "name=" + tvName.getText().toString(); urlFinal = urlFinal + "&address=" + tvLocation.getText().toString(); urlFinal = urlFinal + "&phone=" +tvNumber.getText().toString(); Log.d("URL Values", urlFinal); //for debug purposes new SaveData().execute(urlString);//call AsyncTask to save data }//end of process method public void goBack(View view){ Intent intent = new Intent(getApplicationContext(), JSONActivity.class); startActivity(intent); } public void clear(View view){ tvName.setText(""); tvNumber.setText(""); tvLocation.setText(""); tvName.hasFocus(); } //AsyncTask to send data to Server public class SaveData extends AsyncTask{ @Override protected void onPreExecute(){ //we won't do anything here } @Override protected void onPostExecute(Integer result){ if (result == 1) { Toast.makeText(getApplicationContext(), "Record Saved!", Toast.LENGTH_LONG).show(); } else{ Toast.makeText(getApplicationContext(), "No Record Saved!", Toast.LENGTH_LONG).show(); } } @Override protected Integer doInBackground(String... strings) { URL url; HttpURLConnection connection = null; try{ //create connection url = new URL(strings[0]); connection = (HttpURLConnection)url.openConnection(); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); connection.setRequestProperty("Content-Length", "" + Integer.toString(urlFinal.getBytes().length)); connection.setRequestProperty("Content-Language","en-US"); connection.setUseCaches(false); connection.setDoInput(true); connection.setDoOutput(true); //Send request DataOutputStream wr = new DataOutputStream(connection.getOutputStream()); wr.writeBytes(urlFinal); wr.flush(); wr.close(); //Get response InputStream is = connection.getInputStream(); BufferedReader rd = new BufferedReader(new InputStreamReader(is)); String line; StringBuffer response = new StringBuffer(); while((line = rd.readLine())!= null){ response.append(line); response.append("\r"); } rd.close(); return 1; }catch (Exception e){ return 0; }finally { if(connection != null){ connection.disconnect(); } }//end of try-catch-finally }// end of doInBackground }//end of SaveData asynctask class }//end of Activity Class