0% found this document useful (0 votes)
130 views7 pages

Android 18 - 19

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
130 views7 pages

Android 18 - 19

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

EXPERIMENT-18

Create an application to Implement OkHttp .


Procedure:-
STEP-1
To create a new project click on the new project button. Then a new page
will appear.

STEP-2
Here select on Empty Activity, then click on next. Then a page will
Appear.

STEP-3
Here You have to give your project name, keep the package and file
location default , then select language JAVA, then choose minimum
SDK as your Choice.
Source Code:-
Activity_Main.xml File:-
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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">

<TextView
android:id="@+id/textView"
android:layout_width="339dp"
android:layout_height="561dp"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
We have to allow INTERNET permission in Manifest.xml File So that
we can access internet.
<uses-permission android:name="android.permission.INTERNET"/>
To implement OkHttp we have to add some dependencies in our
create.gardel file.
// define a BOM and its version
implementation(platform("com.squareup.okhttp3:okhttp-bom:4.10.0"))

// define any required OkHttp artifacts without version


implementation("com.squareup.okhttp3:okhttp")
implementation("com.squareup.okhttp3:logging-interceptor")
MainActivity.java File:-
package com.example.json2;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.nfc.Tag;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.io.IOException;
import java.net.URL;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {


// OkHttpClient client = new OkHttpClient();

TextView txtString;

public String url= "https://reqres.in/api/users/2";

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

txtString= (TextView)findViewById(R.id.textView);

try {
run();
} catch (IOException e) {
e.printStackTrace();}}
void run() throws IOException {

OkHttpClient client = new OkHttpClient();

Request request = new Request.Builder()


.url(url)
.build();

client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
call.cancel();
}

@Override
public void onResponse(Call call, Response response) throws
IOException {

final String myResponse = response.body().string();

MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
txtString.setText(myResponse);
}
});

}
});
}

Snapshots of Output:-

Conclusion:-
We have successfully implemented OkHttp in
the above Experiment.
EXPERIMENT-19
AIM OF THE EXPERIMENT: Implementing Retrofit in our application.
PROCEDURE:
Step1:
Firstly, we need to add dependencies in Gradle Scripts ->
build.gradle, so that we can easily manage the external
libraries for our project.

Step2:
Then we need to take internet permission in the manifests ->
AndroidManifest.xml file.

Step3:
Create a new POJO class SignUpResponse.java in which we have
setter/getter method to get the data from API.
Step4:
Create an Interface and name it ApiInterface.java in which we have a registration post-request
method to send the data using API to our server.

Step5:
Create a new Class Api.java class to set the Retrofit. In this class getClient method returns the
Api Interface class object which we are using in our MainActivity.

Step6:
In order to design the front page and for that, we need to write
the code in Activity_main.xml.

Step7:
Lastly, we need to write the code in the MainActivity.java file
to execute the application.
CONCLUSION
In this experiment we came to know how to implement retrofit in
our application

You might also like