Sun. Dec 22nd, 2024

We will do registration using below API

URL:

http://reqres.in/api/register

Request: {“email”:”raj@gmail.com”,”password”:”raj1256″}

Response:

{“page”:1,”per_page”:6,”total”:12,”total_pages”:2,”data”:[{“id”:1,”name”:”cerulean”,”year”:2000,” color”:”#98B2D1″,”pantone_value”:”15-4020″},{“id”:2,”name”:”fuchsia rose”,”year”:2001, “color”:”#C74375″, “pantone_value”:”17-2031″},{“id”:3,”name”:”true red”,”year”:2002,”color”:”#BF1932″,”pantone_value”:”19-1664″},{“id”:4,”name”:”aqua sky”,”year”:2003,”color”:”#7BC4C4″,”pantone_value”:”14-4811″},{“id”:5, “name”:”tigerlily”,”year”:2004,”color”:”#E2583E”,”pantone_value”:”17-1456″},{“id”:6,”name”:”blue turquoise”, “year”:2005,”color”:”#53B0AE”,”pantone_value”:”15-5217″}],”support”:{“url”:”https://reqres.in/#support-heading”, “text”:”To keep ReqRes free, contributions towards server costs are appreciated!”}}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<layout
    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"
    tools:context=".MainActivity">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"

        android:orientation="vertical">




        <EditText
            android:id="@+id/mail"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="textEmailAddress"
            android:hint="mail"/>

        <EditText
            android:id="@+id/password"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="textPassword"
            android:hint="Password"/>

        <Button
            android:id="@+id/reg"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Reg" />
    </LinearLayout>
</layout>

MainActivity.java

package com.androidian.fit;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.androidian.fit.databinding.ActivityMainBinding;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import org.json.JSONException;
import org.json.JSONObject;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class MainActivity extends AppCompatActivity {

    ActivityMainBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding= DataBindingUtil.setContentView(
                MainActivity.this,R.layout.activity_main);


        binding.reg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                JsonObject jsonObject=new JsonObject();

                jsonObject.addProperty("email",binding.mail.getText().toString().trim());
                jsonObject.addProperty("password",binding.password.getText().toString().trim());


                Retrofit retrofit=new Retrofit.Builder()
                        .baseUrl("http://reqres.in/")
                        .addConverterFactory(GsonConverterFactory.create())
                        .build();

                RetroInterface retroInterface=retrofit.create(RetroInterface.class);





                Call<RegResponse> regResponseCall=retroInterface.createUser(jsonObject);

                           regResponseCall.enqueue(new Callback<RegResponse>() {
                    @Override
                    public void onResponse(Call<RegResponse> call,
                                           Response<RegResponse> response) {
                        Log.e("response",String.valueOf(response.body()));
                        String res=response.body().getData().toString();

                        if(res!=null){
                            Toast.makeText(MainActivity.this, "Reg Sucess", Toast.LENGTH_SHORT).show();

                        }else {
                            Toast.makeText(MainActivity.this, "Reg failed", Toast.LENGTH_SHORT).show();

                      }

                                           }

                    @Override
                    public void onFailure(Call<RegResponse> call,
                                          Throwable t) {
                        Toast.makeText(MainActivity.this, ""+t.toString(), Toast.LENGTH_SHORT).show();

                    }
                });


            }
        });


    }
}

Retrointerface:

package com.androidian.fit;

import com.google.gson.JsonObject;

import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.Headers;
import retrofit2.http.POST;

public interface RetroInterface {

    @Headers("Content-Type:application/json")
    @POST("api/register")
    abstract Call<RegResponse> createUser(@Body JsonObject jsonObject);
}
RegResponse 
package com.androidian.fit;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

import java.util.List;

public class RegResponse {

    @SerializedName("page")
    @Expose
    private Integer page;
    @SerializedName("per_page")
    @Expose
    private Integer perPage;
    @SerializedName("total")
    @Expose
    private Integer total;
    @SerializedName("total_pages")
    @Expose
    private Integer totalPages;
    @SerializedName("data")
    @Expose
    private List<ResList> data;
    @SerializedName("support")
    @Expose
    private Support support;

    public Integer getPage() {
        return page;
    }

    public void setPage(Integer page) {
        this.page = page;
    }

    public Integer getPerPage() {
        return perPage;
    }

    public void setPerPage(Integer perPage) {
        this.perPage = perPage;
    }

    public Integer getTotal() {
        return total;
    }

    public void setTotal(Integer total) {
        this.total = total;
    }

    public Integer getTotalPages() {
        return totalPages;
    }

    public void setTotalPages(Integer totalPages) {
        this.totalPages = totalPages;
    }

    public List<ResList> getData() {
        return data;
    }

    public void setData(List<ResList> data) {
        this.data = data;
    }

    public Support getSupport() {
        return support;
    }

    public void setSupport(Support support) {
        this.support = support;
    }

}
ResList
package com.androidian.fit;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class ResList {

    @SerializedName("id")
    @Expose
    private Integer id;
    @SerializedName("name")
    @Expose
    private String name;
    @SerializedName("year")
    @Expose
    private Integer year;
    @SerializedName("color")
    @Expose
    private String color;
    @SerializedName("pantone_value")
    @Expose
    private String pantoneValue;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getYear() {
        return year;
    }

    public void setYear(Integer year) {
        this.year = year;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public String getPantoneValue() {
        return pantoneValue;
    }

    public void setPantoneValue(String pantoneValue) {
        this.pantoneValue = pantoneValue;
    }

}
Support
package com.androidian.fit;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Support {

    @SerializedName("url")
    @Expose
    private String url;
    @SerializedName("text")
    @Expose
    private String text;

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

}

Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">
    <uses-permission android:name="android.permission.INTERNET"></uses-permission>

    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:usesCleartextTraffic="true"
        android:theme="@style/Theme.Fit"
        tools:targetApi="31">
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Build.gradle

plugins {
    id 'com.android.application'
}

android {
    namespace 'com.androidian.fit'
    compileSdk 33

    defaultConfig {
        applicationId "com.androidian.fit"
        minSdk 24
        targetSdk 33
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    dataBinding{
        enabled=true
    }
}

dependencies {

    implementation 'androidx.appcompat:appcompat:1.6.0'
    implementation 'com.google.android.material:material:1.8.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.5'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
    implementation 'com.squareup.retrofit2:converter-gson:2.4.0'
    implementation 'com.squareup.retrofit2:retrofit:2.4.0'
    implementation 'com.google.code.gson:gson:2.8.9'
}

By Rajashekar

I’m (Rajashekar) a core Android developer with complimenting skills as a web developer from India. I cherish taking up complex problems and turning them into beautiful interfaces. My love for decrypting the logic and structure of coding keeps me pushing towards writing elegant and proficient code, whether it is Android, PHP, Flutter or any other platforms. You would find me involved in cuisines, reading, travelling during my leisure hours.

Leave a Reply

Your email address will not be published. Required fields are marked *