Saturday, November 16, 2024
Google search engine
HomeLanguagesJavaHow to Connect Android App with Back4App?

How to Connect Android App with Back4App?

Back4App is another famous backend platform that provides backend services for different types of applications whether it may be web, android, or IOS devices. Back4App also provides us similar services to that of Firebase. We have seen using Firebase in Android App. In this article, we will take a look at adding Back4App in your Android App in Android Studio. We will be building a simple application in which we will be connecting our Android App with Back4App. 

Back4App

Back4App is one of the finest and the most popular alternatives for Parse among the developer’s community. It’s an easy way to build, host, and manage apps using the open-source Parse Server. Based on the highly efficient open-source backend framework, Parse Server, Back4App has several rich functionalities:

  • Featured Parse Server: Back4App uses Parse Server as a core product as it is the finest framework for backend development which can help developers save precious time building an app.
  • Boosted Server Performance: It supports a smart database index, queries optimizers, auto-scaling, automated backups, and redundant storage capacity.
  • Easy Deployment: Back4app is a ready-to-go platform. You can set up your app in less than 5 minutes.
  • Real-time database and analytics: It provides real-time data storage and synchronization. Real-time analytics is a key feature.
  • Within your budget: Predictable pricing, transparent and easy-to-budget.
  • Great technical support team: The engineering support of Back4App is always available to help its users.

Step by Step Implementation

Step 1: Create a New Project

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.

Step 2: Add dependency and JitPack Repository

Navigate to the Gradle Scripts > build.gradle(Module:app) and add the below dependency in the dependencies section.   

implementation “com.github.parse-community.Parse-SDK-Android:parse:1.26.0”

Add the JitPack repository to your build file. Add it to your root build.gradle at the end of repositories inside the allprojects{ } section.

allprojects {

 repositories {

   …

   maven { url “https://jitpack.io” }

     }

}

Now sync your project and we are ready to add Back4App to our app. 

Step 3: Adding permissions to the internet in the AndroidManifest.xml file

Navigate to the app > AndroidManifest.xml and add the below code to it. 

XML




<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>


Step 4: Working with the activity_main.xml file

Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. 

XML




<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
  
    <!--text view for displaying the text 
        which we are going to pass in back4app-->
    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:gravity="center_horizontal"
        android:padding="3dp"
        android:text="Hello World!"
        android:textAlignment="center"
        android:textColor="@color/black" />
  
</RelativeLayout>


Step 5: Creating a new app in back4App 

Create a new account in back4App or you can simply sign in using your Google Account. After creating your new account you will get to see the below screen. Click on the first option to create a new app. The screenshot is shown below. 

After clicking on this option you have to enter the name of your application and click on Create option. You will get to see this option on the below screenshot. 

After you have created a new application it will take some time to create a new application. Once your app has been created you have to go to the left nav drawer and then click on the App Settings option > Security & Keys option to get the key and client id for your app. The screenshot for this option is given below. 

Step 6: Adding your App id and app client key in your strings.xml file

Navigate to the app > res > values > strings.xml file and add the below code to it. Add your original client id and app id in these strings. 

XML




<resources>
    <string name="app_name">GFG Parse</string>
  
    <string name="back4app_server_url">https://parseapi.back4app.com/</string>
  
    <!-- Change the following strings as required -->
    <string name="back4app_app_id">Enter your app id here</string>
    <string name="back4app_client_key">Enter your client key here</string>
      
</resources>


Step 7: Creating a new Java class for our Application 

Navigate to the app > java > your app’s package name > Right-click on it > New > Java class and name it as App and add the below code to it. Comments are added in the code to get to know in more detail. 

Java




import android.app.Application;
  
import com.parse.Parse;
  
public class App extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
          
        // initializing our Parse application with 
        // our application id, client key and server url
        Parse.initialize(new Parse.Configuration.Builder(this)
                .applicationId(getString(R.string.back4app_app_id))
                .clientKey(getString(R.string.back4app_client_key))
                .server(getString(R.string.back4app_server_url))
                 // at last we are building our 
                 // parse with the above credentials
                .build());
    }
}


Step 8: Adding the name for our App class in our AndroidManifest.xml file 

Navigate to the app > AndroidManifest.xml file and inside your application tag on the first line. Add the below line of code. Along with that, we have to add metadata to our application in your Manifest file. Below is the complete code for the AndroidManifest.xml file. 

XML




    package="com.gtappdevelopers.gfgparse">
  
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
  
    <application
        android:name=".App"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.GFGParse">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
  
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
  
    <!--Meta data for your file for server url-->
    <meta-data
        android:name="com.parse.SERVER_URL"
        android:value="@string/back4app_server_url" />
    
    <!--Meta data for your file for application id-->
    <meta-data
        android:name="com.parse.APPLICATION_ID"
        android:value="@string/back4app_app_id" />
    
    <!--Meta data for your file for client key-->
    <meta-data
        android:name="com.parse.CLIENT_KEY"
        android:value="@string/back4app_client_key" />
  
</manifest>


Step 9: Working with the MainActivity.java file

Go to the MainActivity.java file and refer to the following code. Below is the code for the MainActivity.java file. Comments are added inside the code to understand the code in more detail.

Java




import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
  
import androidx.appcompat.app.AppCompatActivity;
  
import com.parse.ParseObject;
  
public class MainActivity extends AppCompatActivity {
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
          
        // creating and initializing variable for our text view.
        TextView textView = findViewById(R.id.textView);
          
        // creating a variable for parse object and setting name as First Class.
        ParseObject firstObject = new ParseObject("FirstClass");
          
        // on below line we are passing the message as key and value to our object.
        firstObject.put("message", "Hey ! Welcome to Geeks for Geeks. Parse is now connected");
          
        // on below line we are calling a 
        // method to save in background.
        firstObject.saveInBackground(e -> {
            // checking if the error is null or not.
            if (e != null) {
                // displaying error toast message on failure.
                Toast.makeText(this, "Fail to add data.." + e.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
            } else {
                // if the data is send successfully we are displaying that data in our text view on below line.
                // as we are passing our key as message so we are calling our data with the key.
                textView.setText(String.format("Data saved is : \n %s", firstObject.get("message")));
            }
        });
    }
}


Now run your app and see the output of the app. Make sure you have added all the keys inside your strings.xml file. After running your app you can see the data has been added to the back4app server which is shown in the below screenshot. 

Output: 

Check out the project on the below link: https://github.com/ChaitanyaMunje/GFG-Back4App

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments