Advertisement
ziedrebhi

MainActivity.java

Apr 19th, 2014
1,138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.27 KB | None | 0 0
  1. package com.mysql.enisandroidclub;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. import org.apache.http.NameValuePair;
  7. import org.apache.http.message.BasicNameValuePair;
  8. import org.json.JSONException;
  9. import org.json.JSONObject;
  10.  
  11. import android.app.Activity;
  12. import android.os.AsyncTask;
  13. import android.os.Bundle;
  14. import android.util.Log;
  15. import android.view.View;
  16. import android.view.View.OnClickListener;
  17. import android.widget.Button;
  18. import android.widget.EditText;
  19. import android.widget.Toast;
  20.  
  21. public class MainActivity extends Activity {
  22.     CustomProgressDialog  progressDialog;
  23.     Button ajout,annuler;
  24.     EditText col2Valeur,col3Valeur,col4Valeur;
  25.     String urlAdd="http://192.168.1.5/enis_android_club/ajout_bd.php";
  26.     AddDataAsyncTask AddData;
  27.     String message;
  28.     int success;
  29.    
  30.     @Override
  31.     protected void onCreate(Bundle savedInstanceState) {
  32.         super.onCreate(savedInstanceState);
  33.         setContentView(R.layout.activity_main);
  34.         progressDialog = new CustomProgressDialog(this, R.drawable.loading_throbber);
  35.         progressDialog.setCancelable(true);
  36.         ajout=(Button)findViewById(R.id.ajout);
  37.         annuler=(Button)findViewById(R.id.annuler);
  38.         col2Valeur=(EditText)findViewById(R.id.col2);
  39.         col3Valeur=(EditText)findViewById(R.id.col3);
  40.         col4Valeur=(EditText)findViewById(R.id.col4);
  41.         AddData =new AddDataAsyncTask();
  42.         ajout.setOnClickListener(new OnClickListener() {
  43.            
  44.             @Override
  45.             public void onClick(View arg0) {
  46.                 AddData.execute();
  47.            
  48.             }
  49.         });
  50.     }
  51.    
  52.    
  53.     private class AddDataAsyncTask extends  AsyncTask<Void, Void, Void> {
  54.         @Override
  55.         protected void onPreExecute() {
  56.             Log.i("add", "onPreExecute");
  57.             super.onPreExecute();
  58.             progressDialog.show();
  59.         }
  60.        
  61.         @Override
  62.         protected Void doInBackground(Void... params) {
  63.             Log.i("add", " start doInBackground");
  64.             // Creating service handler class instance
  65.             ServiceHandler sh = new ServiceHandler();
  66.            
  67.             List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(1);
  68.            
  69.             nameValuePair.add(new BasicNameValuePair("col2",col2Valeur.getText().toString()));
  70.             nameValuePair.add(new BasicNameValuePair("col3",col3Valeur.getText().toString()));
  71.             nameValuePair.add(new BasicNameValuePair("col4",col4Valeur.getText().toString()));
  72.        
  73.             // Making a request to url and getting response
  74.             String jsonStr = sh.makeServiceCall(urlAdd, ServiceHandler.POST,nameValuePair);
  75.            
  76.             Log.d("Response: ",jsonStr);
  77.             if (jsonStr != null) {
  78.                 try {
  79.                    
  80.                     JSONObject jsonObj = new JSONObject(jsonStr);
  81.                     success = jsonObj.getInt("success");
  82.                     message = jsonObj.getString("message");
  83.                     Log.i("suucess", String.valueOf(success));
  84.                     Log.i("message", message);
  85.                    
  86.                 } catch (JSONException e) {
  87.                    
  88.                     e.printStackTrace();
  89.                 }
  90.             }
  91.  
  92.             Log.i("add", " end doInBackground");
  93.             return null;
  94.         }
  95.        
  96.         @Override
  97.         protected void onPostExecute(Void result) {
  98.             Log.i("add", "onPostExecute");
  99.             super.onPostExecute(result);
  100.             if (progressDialog.isShowing())
  101.             {
  102.                 progressDialog.dismiss();
  103.             }
  104.             if(success==1)
  105.             {
  106.                 Toast.makeText(getApplicationContext(), "Good "+message, Toast.LENGTH_LONG).show();
  107.             }
  108.             else
  109.             {
  110.                 Toast.makeText(getApplicationContext(), "Erreur" +message, Toast.LENGTH_LONG).show();
  111.             }
  112.  
  113.         }
  114.        
  115.     }
  116.    
  117.  
  118.    
  119.  
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement