Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class AddInfo extends AppCompatActivity {
- EditText Name , Email ,Mobile;
- String name , email,mobile;
- TextView textView ;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_add_info);
- Name = (EditText)findViewById(R.id.editText);
- Email = (EditText)findViewById(R.id.editText2);
- Mobile = (EditText)findViewById(R.id.editText3);
- textView = (TextView)findViewById(R.id.textView);
- }
- public void saveInfo(View view){
- name = Name.getText().toString();
- email = Email.getText().toString();
- mobile = Mobile.getText().toString();
- BackgroundTask backgroundTask = new BackgroundTask();
- backgroundTask.execute(name,email,mobile);
- finish();
- }
- class BackgroundTask extends AsyncTask<String,Void,String>{
- String add_info_url;
- @Override
- protected void onPreExecute() {
- add_info_url = "url";
- }
- @Override
- protected String doInBackground(String... args) {
- String name,email,mobile;
- name= args[0];
- email = args[1];
- mobile = args[2];
- try{
- URL url = new URL(add_info_url);
- HttpURLConnection httpURLConnection =(HttpURLConnection) url.openConnection();
- httpURLConnection.setRequestMethod("POST");
- httpURLConnection.setDoOutput(true);
- OutputStream outputStream = httpURLConnection.getOutputStream();
- BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream,"UTF-8"));
- String data_string = URLEncoder.encode("name","UTF-8")+"="+URLEncoder.encode(name,"UTF-8")+"&"+
- URLEncoder.encode("email","UTF-8")+"="+URLEncoder.encode(email,"UTF-8")+"&"+
- URLEncoder.encode("mobile","UTF-8")+"="+URLEncoder.encode(mobile,"UTF-8");
- bufferedWriter.write(data_string);
- bufferedWriter.flush();
- bufferedWriter.close();
- outputStream.close();
- InputStream inputStream= httpURLConnection.getInputStream();
- inputStream.close();
- httpURLConnection.disconnect();
- return "One row of data inserted";
- }
- catch (MalformedURLException e) {
- e.printStackTrace();
- } catch (IOException e) {
- }
- return null;
- }
- @Override
- protected void onProgressUpdate(Void... values) {
- textView.setText("Loading.....");
- }
- @Override
- protected void onPostExecute(String result) {
- Toast.makeText(getApplicationContext(),result,Toast.LENGTH_LONG).show();
- }
- }
Add Comment
Please, Sign In to add comment