Advertisement
Guest User

mysql4me

a guest
Sep 13th, 2017
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.13 KB | None | 0 0
  1. MYSQL
  2.  
  3. CREATE TABLE IF NOT EXIST 'USUARIOS'{
  4.     'Id_Usuario' int(10) NOT NULL AUTO_INCREMENT,
  5.     'username' varchar(20) NOT NULL,
  6.     'passw' varchar(20) NOT NULL,
  7.     PRIMARY KEY('Id_Usuario')
  8. };
  9. -----------------------------------------------------------
  10. <?php
  11. function Conectarse()
  12. {
  13.     if (!($link=mysql_connect("localhost","root","")))
  14.     {
  15.         echo "Error conectando a la base de datos.";
  16.         exit();
  17.     }
  18.     if (!mysql_select_db("usuarios",$link))
  19.     {
  20.         echo "Error seleccionando la base de datos.";
  21.         exit();
  22.     }
  23.     return $link;
  24. }
  25. ?>
  26. -----------------------------------------------------------
  27. <?php
  28. include("conection.php");
  29. $user=$_POST['Usuario'];
  30. $pass=$_POST['Contrasena'];
  31.  
  32. $link=Conectarse();
  33. $q=mysql_query("SELECT * FROM usuarios WHERE username='$user' AND passw='$pass'");
  34.  
  35. if($e=mysql_fetch_array($q)){
  36.     $output[]=$e;
  37. }
  38.  
  39. print(json_encode($output));
  40. ?>
  41. ------------------------------------------------------------------
  42.  
  43. public class Main extends Activity{
  44.     private EditText user, pass;
  45.     Button validar;
  46.    
  47.     @Override
  48.     public void onCreate(Bundle savedInstanceState){
  49.         super.onCreate(savedInstanceState);
  50.         setContentView(R.layout.main);
  51.        
  52.         user=(EditText)findViewById(R.id.usuario);
  53.         pass=(EditText)findViewById(R.id.password);
  54.        
  55.         validar=(Button) findViewById(R.id.btnValidar);
  56.        
  57.         validar.setOnClickListener(new OnClickListener){
  58.         @Override
  59.        
  60.         public void onClick(View v){
  61.             ArrayList<String> parametros = new ArrayList<String>();
  62.             parametros.add("Usuario");
  63.             parametros.add(user.getText().toString());
  64.             parametros.add("Contraseña");
  65.             parametros.add(pass.getText().toString());
  66.            
  67.             try{
  68.                 Post post = new Post();
  69.                 JSONArray datos = post.getServerData(parametros,"http://192.168.1.155/android/login.php");
  70.                
  71.                 if (datos !=null && datos.length()>0){
  72.                 JSONObject json_data=datos.getJSONObject(0);
  73.                 int numRegistrados= json_data.getInt("Id_Usuario");
  74.                 if(numRegistrados>0){
  75.                         Toast.makeText(getBaseContext(), "Usuario Correcto.", Toast.LENGTH_SHORT).show();
  76.                 }else{
  77.                         Toast.makeText(getBaseContext(),"Usuario Incorrecto.", Toast.LENGTH_SHORT).show();
  78.                 }
  79.               }catch(Exception e){
  80.                 Toast.makeText(getBaseContext(),"Error al conectar con el servidor.", Toast.LENGTH_SHORT).show();
  81.               }
  82.             }
  83.         });
  84.        
  85.        
  86.     }
  87. ----------------------------------------------------------------------------------------------------------------------------------
  88. POST
  89.  
  90. public class Post{
  91.     private InputStream is null;
  92.     private String respuesta = "";
  93.    
  94.     private void conectaPost(ArrayList parametros, String URL){
  95.         ArrayList nameValuePairs;
  96.         try{
  97.             HttpPost httppost=new HttpPost(URL);
  98.             nameValuePairs= new ArrayList(),
  99.             if (parametros!=null){
  100.                 for (int i=0; i<parametros.size() -1; i+=2)
  101.                 {
  102.                 nameValuePairs.add(newBasicNameValuePair((String)parametros.get(i),(String)parametros.get(i+)));
  103.                 }
  104.                 httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
  105.                 }
  106.                 HttpResponse response = httpclient.execute(httppost);
  107.                 HttpEntity entity = response.getEntity();
  108.                 is=entity.getContent();
  109.             }catch (Exception e){
  110.                 Log.e("log_tag","Error in http connection" + e.toString());
  111.             }finally{
  112.             }  
  113.         }
  114.     private void getRespuestaPost(){
  115.         try{
  116.             BufferedReader reader = new BufferedReader(
  117.             new InputStreamReader(is, "iso-8859-1"),8);
  118.             StringBuilder sb = new StringBuilder();
  119.             String line = null;
  120.             while ((line = reader.readLine()) ! = null){
  121.             sb.append(line+"\n");
  122.             }
  123.             is.close(),
  124.             respuesta = sb.toString();
  125.             Log.e("log_tag","Cadena JSon" + respuesta);
  126.         }catch (Exception e){
  127.         Log.e("log_tag","Error converting resutl" + e.toString());
  128.             }
  129.         }
  130.     @SuppressWarnings("finally")
  131.         private JSONArray getJsonArray(){
  132.         JSONArray jArray = null;
  133.         try{
  134.             jArray=new JSONArray(respuesta);
  135.         }catch(Exception e){
  136.         }finally{
  137.             return jArray;
  138.         }
  139.     }
  140.     public JSONArray getServerData(ArrayList parametros, String URL)
  141.         conectaPost(parametros, URL);
  142.         if(is !=null){
  143.         getRespuestaPost();
  144.     }
  145.     if(respuesta !=null && respuesta.trim() !=""){
  146.         return getJsonArray();
  147.     }else{
  148.         return null;
  149.     }
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement