Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MainActivity extends Activity {
- private EditText etName,etMobile, etAddress;
- private Button btnSubmit;
- private ProgressDialog pDialog;
- private JSONObject json;
- private int success=0;
- private HTTPURLConnection service;
- private String strname ="", strMobile ="",strAddress="";
- //Initialize webservice URL
- private String path = "";
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- etName= (EditText) findViewById(R.id.etName);
- etMobile= (EditText) findViewById(R.id.etMobile);
- etAddress= (EditText) findViewById(R.id.etAddress);
- btnSubmit= (Button) findViewById(R.id.btnSubmit);
- //Initialize HTTPURLConnection class object
- service=new HTTPURLConnection();
- btnSubmit.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- if (!etName.getText().toString().equals("") && !etMobile.getText().toString().equals("")) {
- strname = etName.getText().toString();
- strMobile = etMobile.getText().toString();
- strAddress = etAddress.getText().toString();
- //Call WebService
- new PostDataTOServer().execute();
- } else {
- Toast.makeText(getApplicationContext(), "Please Enter all fields", Toast.LENGTH_LONG).show();
- }
- }
- });
- }
- private class PostDataTOServer extends AsyncTask<Void, Void, Void> {
- String response = "";
- //Create hashmap Object to send parameters to web service
- HashMap<String, String> postDataParams;
- @Override
- protected void onPreExecute() {
- super.onPreExecute();
- pDialog = new ProgressDialog(MainActivity.this);
- pDialog.setMessage("Please wait...");
- pDialog.setCancelable(false);
- pDialog.show();
- }
- @Override
- protected Void doInBackground(Void... arg0) {
- postDataParams=new HashMap<String, String>();
- postDataParams.put("name", strname);
- postDataParams.put("mobile", strMobile);
- postDataParams.put("address", strAddress);
- //Call ServerData() method to call webservice and store result in response
- response= service.ServerData(path,postDataParams);
- try {
- json = new JSONObject(response);
- //Get Values from JSONobject
- System.out.println("success=" + json.get("success"));
- success = json.getInt("success");
- } catch (JSONException e) {
- e.printStackTrace();
- }
- return null;
- }
- @Override
- protected void onPostExecute(Void result) {
- super.onPostExecute(result);
- if (pDialog.isShowing())
- pDialog.dismiss();
- if(success==1) {
- Toast.makeText(getApplicationContext(), "Employee Added successfully..!", Toast.LENGTH_LONG).show();
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment