Retrofit Simple text show

apply plugin: 'com.android.application'

android {
compileSdkVersion 30
buildToolsVersion "30.0.1"
defaultConfig {
applicationId "net.androidsquad.retrofitapp"
minSdkVersion 26
targetSdkVersion 30
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
}

}

dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'

// Retrofit
implementation 'com.squareup.retrofit2:retrofit:2.9.0'

// Converter for retrofit : GSON
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'


}


Xml:- 

<TextView
android:id="@+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="36sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />




Model Class :-

package net.androidsquad.retrofitapp;

import com.google.gson.annotations.SerializedName;

public class DataModel {

// This class will be as a template for the data that we are going to parse

private int userId;

private int id;

private String title;

private boolean completed;



// Getters
public int getUserId() {
return userId;
}

public int getId() {
return id;
}

public String getTitle() {
return title;
}

public boolean isCompleted() {
return completed;
}
}




MainActivity :-


package net.androidsquad.retrofitapp;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.TextView;

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

public class MainActivity extends AppCompatActivity {

TextView txt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

txt = findViewById(R.id.textview);

// Retrofit Builder
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://run.mocky.io/")
.addConverterFactory(GsonConverterFactory.create())
.build();

// instance for interface
MyAPICall myAPICall = retrofit.create(MyAPICall.class);
Call<DataModel> call = myAPICall.getData();

call.enqueue(new Callback<DataModel>() {
@Override
public void onResponse(Call<DataModel> call, Response<DataModel> response) {

// Checking for the response
if (response.code() != 200 ){
txt.setText("Check the connection");
return;
}


// Get the data into textview
String jsony = "";
jsony = "ID= " + response.body().getId() +
"\nuserId= "+response.body().getUserId() +
"\ntitle= " +response.body().getTitle() +
"\nCompleted= " +response.body().isCompleted();

txt.append(jsony);


}

@Override
public void onFailure(Call<DataModel> call, Throwable t) {

}
});













}
}




Interface :-
package net.androidsquad.retrofitapp;

import retrofit2.Call;
import retrofit2.http.GET;

public interface MyAPICall {

// https://run.mocky.io/ v3/90fea295-8b4d-4e7c-be1e-f0788188102f

@GET("v3/90fea295-8b4d-4e7c-be1e-f0788188102f")
Call<DataModel> getData();


}













 

Comments

Popular posts from this blog

Gride View in Firebase

Register in kotlin

Check Permission in Android Studio