Guest User

Untitled

a guest
Dec 16th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.49 KB | None | 0 0
  1. public class AddInfo extends AppCompatActivity {
  2.  
  3. EditText Name , Email ,Mobile;
  4. String name , email,mobile;
  5. TextView textView ;
  6. @Override
  7. protected void onCreate(Bundle savedInstanceState) {
  8. super.onCreate(savedInstanceState);
  9. setContentView(R.layout.activity_add_info);
  10.  
  11. Name = (EditText)findViewById(R.id.editText);
  12.  
  13. Email = (EditText)findViewById(R.id.editText2);
  14. Mobile = (EditText)findViewById(R.id.editText3);
  15. textView = (TextView)findViewById(R.id.textView);
  16.  
  17.  
  18. }
  19.  
  20. public void saveInfo(View view){
  21.  
  22. name = Name.getText().toString();
  23. email = Email.getText().toString();
  24. mobile = Mobile.getText().toString();
  25. BackgroundTask backgroundTask = new BackgroundTask();
  26. backgroundTask.execute(name,email,mobile);
  27. finish();
  28.  
  29. }
  30.  
  31. class BackgroundTask extends AsyncTask<String,Void,String>{
  32.  
  33. String add_info_url;
  34. @Override
  35. protected void onPreExecute() {
  36.  
  37. add_info_url = "url";
  38.  
  39. }
  40.  
  41. @Override
  42. protected String doInBackground(String... args) {
  43. String name,email,mobile;
  44. name= args[0];
  45. email = args[1];
  46. mobile = args[2];
  47.  
  48. try{
  49. URL url = new URL(add_info_url);
  50. HttpURLConnection httpURLConnection =(HttpURLConnection) url.openConnection();
  51. httpURLConnection.setRequestMethod("POST");
  52. httpURLConnection.setDoOutput(true);
  53. OutputStream outputStream = httpURLConnection.getOutputStream();
  54. BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream,"UTF-8"));
  55. String data_string = URLEncoder.encode("name","UTF-8")+"="+URLEncoder.encode(name,"UTF-8")+"&"+
  56. URLEncoder.encode("email","UTF-8")+"="+URLEncoder.encode(email,"UTF-8")+"&"+
  57. URLEncoder.encode("mobile","UTF-8")+"="+URLEncoder.encode(mobile,"UTF-8");
  58. bufferedWriter.write(data_string);
  59. bufferedWriter.flush();
  60. bufferedWriter.close();
  61. outputStream.close();
  62.  
  63. InputStream inputStream= httpURLConnection.getInputStream();
  64. inputStream.close();
  65. httpURLConnection.disconnect();
  66. return "One row of data inserted";
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73. }
  74. catch (MalformedURLException e) {
  75. e.printStackTrace();
  76. } catch (IOException e) {
  77.  
  78. }
  79.  
  80. return null;
  81. }
  82.  
  83. @Override
  84. protected void onProgressUpdate(Void... values) {
  85.  
  86. textView.setText("Loading.....");
  87.  
  88. }
  89.  
  90. @Override
  91. protected void onPostExecute(String result) {
  92.  
  93. Toast.makeText(getApplicationContext(),result,Toast.LENGTH_LONG).show();
  94.  
  95.  
  96.  
  97. }
  98.  
  99. }
Add Comment
Please, Sign In to add comment