Android - Se puede vincular app a sitio web?

 
Vista:
sin imagen de perfil
Val: 12
Ha aumentado 1 puesto en Android (en relación al último mes)
Gráfica de Android

Se puede vincular app a sitio web?

Publicado por Raul (5 intervenciones) el 26/11/2020 03:14:14
Hola. Tengo una app cuya finalidad es ingresar datos a una base de datos de firebase. La parte de diseño tiene este código:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical">
 
<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Nombre"
    android:id="@+id/etNombre"
    android:layout_marginTop="40dp"/>
 
<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Apellido"
    android:id="@+id/etApellido"/>
 
<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Telefono"
    android:id="@+id/etTelefono"
    android:inputType="number"/>
 
<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Direccion"
    android:id="@+id/etDireccion"/>
 
<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Enviar Datos"
    android:id="@+id/btnSubirDatos"/>
 
</LinearLayout>

La parte de programación consiste en:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package com.enviofirebase.myapplication;
 
import androidx.appcompat.app.AppCompatActivity;
 
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
 
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
 
import java.util.HashMap;
import java.util.Map;
 
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
 
    DatabaseReference mRootReference;
    Button mButtonSubirDatosFirebase;
    EditText mEditTextDatoNombreUsuario,mEditTextDatoApellidoUsuario,mEditTextDatoTelefonoUsuario,mEditTextDatoDireccionUsuario;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        mButtonSubirDatosFirebase = findViewById(R.id.btnSubirDatos);
        mButtonSubirDatosFirebase.setOnClickListener(this);
        mEditTextDatoNombreUsuario = findViewById(R.id.etNombre);
        mEditTextDatoApellidoUsuario = findViewById(R.id.etApellido);
        mEditTextDatoTelefonoUsuario = findViewById(R.id.etTelefono);
        mEditTextDatoDireccionUsuario = findViewById(R.id.etDireccion);
 
        mRootReference = FirebaseDatabase.getInstance().getReference();
 
        solicitarDatosFirebase();
 
    }
 
    private void solicitarDatosFirebase() {
        mRootReference.child("Usuario").addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
 
                for(final DataSnapshot snapshot : dataSnapshot.getChildren()){
 
                    mRootReference.child("Usuario").child(snapshot.getKey()).addValueEventListener(new ValueEventListener() {
                        @Override
                        public void onDataChange(DataSnapshot dataSnapshot) {
                            userPojo user = snapshot.getValue(userPojo.class);
                            String nombre = user.getNombre();
                            String apellido = user.getApellido();
                            int telefono = user.getTelefono();
                            String direccion = user.getDireccion();
 
                            Log.e("NombreUsuario:",""+nombre);
                            Log.e("ApellidoUsuario:",""+apellido);
                            Log.e("TelefonoUsuario:",""+telefono);
                            Log.e("DireccionUsuario:",""+direccion);
                            Log.e("Datos:",""+snapshot.getValue());
                        }
 
                        @Override
                        public void onCancelled(DatabaseError databaseError) {
 
                        }
                    });
 
                }
 
            }
 
            @Override
            public void onCancelled(DatabaseError databaseError) {
 
            }
        });
    }
 
    private void cargarDatosFirebase(String nombre, String apellido, int telefono, String direccion) {
 
        Map<String, Object> datosUsuario = new HashMap<>();
        datosUsuario.put("nombre", nombre);
        datosUsuario.put("apellido", apellido);
        datosUsuario.put("telefono", telefono);
        datosUsuario.put("direccion", direccion);
 
        mRootReference.child("Usuario").push().setValue(datosUsuario);
    }
 
    @Override
    public void onClick(View v) {
 
        switch(v.getId()){
 
            case R.id.btnSubirDatos:
 
                String nombre = mEditTextDatoNombreUsuario.getText().toString();
                String apellido = mEditTextDatoApellidoUsuario.getText().toString();
                int telefono = Integer.parseInt(mEditTextDatoTelefonoUsuario.getText().toString());
                String direccion = mEditTextDatoDireccionUsuario.getText().toString();
                cargarDatosFirebase(nombre, apellido, telefono, direccion);
                break;
        }
 
    }
}

más una parte para los constructores:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package com.enviofirebase.myapplication;
 
public class userPojo {
 
    private String nombre;
    private String apellido;
    private int telefono;
    private String direccion;
 
    public userPojo() {
 
    }
 
    public String getApellido() {
        return apellido;
    }
 
    public void setApellido(String apellido) {
        this.apellido = apellido;
    }
 
    public int getTelefono() {
        return telefono;
    }
 
    public void setTelefono(int telefono) {
        this.telefono = telefono;
    }
 
    public String getDireccion() {
        return direccion;
    }
 
    public void setDireccion(String direccion) {
        this.direccion = direccion;
    }
 
 
 
    public String getNombre() {
        return nombre;
    }
 
    public void setNombre(String nombre) {
        this.nombre = nombre;
    }
 
}
envio-datos


Ahora bien, existe la posibilidad de recoger datos de esta base de datos a través de un sitio web con un código como este, por ejemplo?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<html>
	<head>
	</head>
		<body>
 
			<!-- The core Firebase JS SDK is always required and must be listed first -->
			<script src="..."></script>
 
			<!-- TODO: Add SDKs for Firebase products that you want to use
    	 	https://firebase.google.com/docs/web/setup#available-libraries -->
			<script src="..."></script>
 
			<script>
  				// Your web app's Firebase configuration
  				// For Firebase JS SDK v7.20.0 and later, measurementId is optional
  					var firebaseConfig = {
    					apiKey: "...",
    					authDomain: "...",
    					databaseURL: "...",
    					projectId: "...",
    					storageBucket: "...",
    					messagingSenderId: "...",
    					appId: "...",
    					measurementId: "..."
  					};
  				// Initialize Firebase
  				firebase.initializeApp(firebaseConfig);
  				firebase.analytics();
 
  				function getData() {
  					firebase.database().ref('/').once('value', function(snapshot) {
  						snapshot.forEach(function(childSnapshot)
  							 	{
  							 		var childKey = childSnapshot.key;
  							 		var childData = childSnapshot.val();
  							 		document.getElementById("data").innerHTML=childData['Nombre'] + ", " + childData['Apellido'] + ", " + childData['Telefono'] + ", " + childData['Direccion'];
  							 	})
  					})
  				}
			</script>
 
			<h1>User Database</h1>
			<button onclick= "getData()">Obtener</button>
			<p id="data"></p>
		</body>
</html>


Muchas gracias
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder