Reputation: 33
I am a college student taking my OJT at this company and have been tasked to create an Android app that adds data to a Firebase database using REST API. The app is an Items list app where I need to have a sign up, sign in, list view for the items, and to be able to add, edit, and delete items. I am required to use Node.js, Android Studio, and Firebase. My team leader told me to study REST API, Node.js, JSON, Firebase, and I now somewhat kind of understand what I'm supposed to do with these, at least the concepts and in theory.
The problem is with the codes. It's overwhelming. I am new to javascript, I just started learning it on the day this task was given to me (a week ago). And I have no idea how to create the codes on Android Studio and Node.js so that the API can handle requests/responses between client and server. Please keep in mind that I have a deadline in about 4-5 weeks.
The codes I have right now are very basic, and I am not even sure that they are correct. Right now I just have the Sign Up Activity and the server on Node.js. I use Android Studio 3.1.4, node v8.11.4, npm v5.6.0
aSignUpActivity.java
public class aSignUpActivity extends AppCompatActivity {
EditText etFname, etLname, etEmail, etUname, etPword, etConfPword;
Button btnConfirmSU;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_a_sign_up);
etFname = (EditText) findViewById(R.id.asuFnameET);
etLname = (EditText) findViewById(R.id.asuLnameET);
etEmail = (EditText) findViewById(R.id.asuEmailET);
etUname = (EditText) findViewById(R.id.asuUnameET);
etPword = (EditText) findViewById(R.id.asuPwordET);
etConfPword = (EditText) findViewById(R.id.asuCpwordET);
btnConfirmSU = (Button) findViewById(R.id.asuConfirmSU);
btnConfirmSU.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
confirmSignupClicked();
}
});
}
public void confirmSignupClicked(){
String fName = etFname.getText().toString();
String lName = etLname.getText().toString();
String eMail = etEmail.getText().toString();
String uName = etUname.getText().toString();
String pWord = etPword.getText().toString();
String cPword = etConfPword.getText().toString();
if(TextUtils.isEmpty(fName) || TextUtils.isEmpty(lName) || TextUtils.isEmpty(eMail) || TextUtils.isEmpty(uName) || TextUtils.isEmpty(pWord) || TextUtils.isEmpty(cPword)){
Toast.makeText(this, "Can't leave any text fields empty.", Toast.LENGTH_SHORT).show();
}
else{
Intent intent = new Intent(aSignUpActivity.this, aStartPageActivity.class);
Toast.makeText(this, "Account created.", Toast.LENGTH_SHORT).show();
startActivity(intent);
}
}
}
server.js (I copy-pasted these codes and edited it to fit my situation)
var admin = require('firebase-admin')
var serviceAccount = require('./myprivatekey.json')
// var port = process.env.PORT || 3000
// var express = require('express')
// var app = express()
// var bodyParser = require('body-parser')
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "mydatabase.firebaseio.com"
})
// app.use(bodyParser.urlencoded({extended: true}))
// app.use(bodyParser.json())
var db = admin.database()
var dbRef = db.ref('usersinfo')
//API - SIGN UP USER
function addUser(userid, username, email, password){
var usersRef = dbRef.child("user")
usersRef.set({
"USER ID": userid,
"USERNAME": username,
"EMAIL": email,
"PASSWORD": password,
})
}
//testing if function works
addUser("test", "test", "test", "test")
//API - ADD ITEM
function addItem(userid, image, name, description){
var itemsRef = dbRef.child("useritems")
itemsRef.set({
"USER ID": userid,
"IMAGE": image,
"NAME": name,
"DESCRIPTION": description
})
var itemId = itemsRef.key
}
//testing if function works
addItem("test", "test", "test", "test")
//API - VIEW ITEM
function viewItem(itemId) {
var itemRef = dbRef.child("useritems/" + itemId)
itemRef.on("value", function(snapshot) {
if (snapshot.exists()) {
console.log(snapshot.val())
return snapshot.val()
} else {
console.log("Cannot view item.", itemId)
return null
}
}, function (errorObject) {
console.log("error: " + errorObject.code)
});
}
//API - EDIT ITEM
function editItem(itemId, editimage, editname, editdescription){
var itemRef = dbRef.child("useritems/", itemId)
itemRef.update({
"IMAGE": editimage,
"NAME": editname,
"DESCRIPTION": editdescription
})
console.log("Item successfully edited")
itemRef.on("value", function(snapshot){
console.log(snapshot.val())
}, function (errorObject){
console.log("Edit failed: " + errorObect.code)
})
}
//API - DELETE ITEM
function deleteItem(itemId){
itemRef = dbRef.child("useritems/" + itemId)
itemRef.once("value")
.then(function(snapshot){
if(snapshot.exists()){
console.log("Item removed successfully.")
return "Item removed."
}else{
console.log("Item does not exist")
return "Error."
}
})
.catch(function(error){
console.log("deleteItem failed: " + error.code)
})
}
What I'm asking is if someone can point me in the right direction of how to even begin this? Like links to resources? Advice on my codes? Something beginner friendly. Most of the examples on YouTube and google use HTML as the front-end for the client, or tutorials for an Android Chat app, or use mongoDB/SQL for the database. Maybe I am not searching well enough? Please help.
Upvotes: 1
Views: 669
Reputation: 5498
I would find a tutorial on how to make a node.js REST app (maybe using Express) and work through it. Use Postman to send requests. After you get the server working you can move on to the Andraid app. I'd recommend Retrofit for making the requests.
Upvotes: 1