angamba

sdfsdf

May 15th, 2019
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.24 KB | None | 0 0
  1. public class MainActivity extends Activity {
  2.  
  3. private EditText etName,etMobile, etAddress;
  4. private Button btnSubmit;
  5. private ProgressDialog pDialog;
  6. private JSONObject json;
  7. private int success=0;
  8. private HTTPURLConnection service;
  9. private String strname ="", strMobile ="",strAddress="";
  10. //Initialize webservice URL
  11. private String path = "";
  12.  
  13. @Override
  14. protected void onCreate(Bundle savedInstanceState) {
  15. super.onCreate(savedInstanceState);
  16. setContentView(R.layout.activity_main);
  17.  
  18. etName= (EditText) findViewById(R.id.etName);
  19. etMobile= (EditText) findViewById(R.id.etMobile);
  20. etAddress= (EditText) findViewById(R.id.etAddress);
  21. btnSubmit= (Button) findViewById(R.id.btnSubmit);
  22.  
  23. //Initialize HTTPURLConnection class object
  24. service=new HTTPURLConnection();
  25.  
  26. btnSubmit.setOnClickListener(new View.OnClickListener() {
  27. @Override
  28. public void onClick(View v) {
  29. if (!etName.getText().toString().equals("") && !etMobile.getText().toString().equals("")) {
  30. strname = etName.getText().toString();
  31. strMobile = etMobile.getText().toString();
  32. strAddress = etAddress.getText().toString();
  33. //Call WebService
  34. new PostDataTOServer().execute();
  35. } else {
  36. Toast.makeText(getApplicationContext(), "Please Enter all fields", Toast.LENGTH_LONG).show();
  37. }
  38. }
  39. });
  40. }
  41.  
  42. private class PostDataTOServer extends AsyncTask<Void, Void, Void> {
  43.  
  44. String response = "";
  45. //Create hashmap Object to send parameters to web service
  46. HashMap<String, String> postDataParams;
  47. @Override
  48. protected void onPreExecute() {
  49. super.onPreExecute();
  50.  
  51. pDialog = new ProgressDialog(MainActivity.this);
  52. pDialog.setMessage("Please wait...");
  53. pDialog.setCancelable(false);
  54. pDialog.show();
  55. }
  56. @Override
  57. protected Void doInBackground(Void... arg0) {
  58. postDataParams=new HashMap<String, String>();
  59. postDataParams.put("name", strname);
  60. postDataParams.put("mobile", strMobile);
  61. postDataParams.put("address", strAddress);
  62. //Call ServerData() method to call webservice and store result in response
  63. response= service.ServerData(path,postDataParams);
  64. try {
  65. json = new JSONObject(response);
  66. //Get Values from JSONobject
  67. System.out.println("success=" + json.get("success"));
  68. success = json.getInt("success");
  69.  
  70. } catch (JSONException e) {
  71. e.printStackTrace();
  72. }
  73. return null;
  74. }
  75. @Override
  76. protected void onPostExecute(Void result) {
  77. super.onPostExecute(result);
  78. if (pDialog.isShowing())
  79. pDialog.dismiss();
  80. if(success==1) {
  81. Toast.makeText(getApplicationContext(), "Employee Added successfully..!", Toast.LENGTH_LONG).show();
  82. }
  83. }
  84. }
  85. }
Add Comment
Please, Sign In to add comment