Wednesday, July 8, 2015

Android Mail sample

package com.example.studentregistration;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import com.example.helpers.JSONParser;
import com.example.studentregistration.Register.CreateUser;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class ContactUs extends Activity implements OnClickListener{

private Button btnSend, btnCancel;
private ProgressDialog pDialog;

JSONParser jsonParser = new JSONParser();
private static final String LOGIN_URL = "http://pratheepan.info/ws4/mymail.php";
//  ws4/show.php
private static final String TAG_SUCCESS = "success";
private static final String TAG_MESSAGE = "message";   
   
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.contactus);
       
        btnSend = (Button)findViewById(R.id.btnSend);
        btnCancel = (Button)findViewById(R.id.btnCancel);
       
        //register listeners
        btnSend.setOnClickListener(this);
        btnCancel.setOnClickListener(this);
       
    }
   
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
        case R.id.btnSend:
        //do somthing here.................
           
            new CreateUser().execute();
           
            break;
        case R.id.btnCancel:
                Intent i2 = new Intent(this, HomePage.class);
                startActivity(i2);
            break;

        default:
            break;
        }
    }
   
   
    class CreateUser extends AsyncTask<String, String, String> {

        EditText strName = (EditText)findViewById(R.id.txtName);
        String name = strName.getText().toString();
        EditText strPhone = (EditText)findViewById(R.id.txtPhone);
        String phone = strPhone.getText().toString();   
       
        EditText strSubject = (EditText)findViewById(R.id.txtSubject);
        String subject = strSubject.getText().toString();   
        EditText strDepartment = (EditText)findViewById(R.id.txtMessage);
        String message = strDepartment.getText().toString();   
   
       
       
         /**
        * Before starting background thread Show Progress Dialog
        * */
        boolean failure = false;
       
       @Override
       protected void onPreExecute() {
           super.onPreExecute();
           pDialog = new ProgressDialog(ContactUs.this);
           pDialog.setMessage("Creating User...");
           pDialog.setIndeterminate(false);
           pDialog.setCancelable(true);
           pDialog.show();
       }
       
        @Override
        protected String doInBackground(String... args) {
            // TODO Auto-generated method stub
             // Check for success tag
       
           int success;
          // String username = user.getText().toString();
         //  String password = pass.getText().toString();
         //  String email = em.getText().toString();
           try {
               // Building Parameters
               List<NameValuePair> params = new ArrayList<NameValuePair>();
               params.add(new BasicNameValuePair("Name", name));
            
               params.add(new BasicNameValuePair("Phone", phone));
               params.add(new BasicNameValuePair("Subject", subject));
               params.add(new BasicNameValuePair("Message", message));
              // Log.d("request!", "starting");
              //====================================================== ws4/mymail.php
              
               //Posting user data to script
               JSONObject json = jsonParser.makeHttpRequest(
                      LOGIN_URL, "POST", params);

               success = json.getInt(TAG_SUCCESS);
               if (success == 1) {
                                
            //       Intent in = new Intent(ContactUs.this, Homepage.class);
             //      finish();
                //   TextView tvres = (TextView)findViewById(R.id.tvResult);
                 //   tvres.setText("Message Successfully Sent !");
                              
                   return json.getString(TAG_MESSAGE);
               }
               else
               {
                   Log.d("Login Failure!", json.getString(TAG_MESSAGE));
                   return json.getString(TAG_MESSAGE);
                  
               }
           } catch (JSONException e) {
               e.printStackTrace();
           }

           return null;
           
        }
        /**
        * After completing background task Dismiss the progress dialog
        * **/
       protected void onPostExecute(String file_url) {
           // dismiss the dialog once product deleted
          
        //    UserServ serv = UserServImpl.getInstance();
        //    DataList.setUsers(serv.getUsers());
          
         
           pDialog.dismiss();
           if (file_url != null){
               Toast.makeText(ContactUs.this, file_url, Toast.LENGTH_LONG).show();
              
        
           
           }

       }
       
    }
       
}

No comments:

Post a Comment