1) Add this dependency on your grdule.build(Module File)
implementation 'com.squareup.retrofit2:retrofit:2.1.0'
implementation 'com.google.code.gson:gson:2.6.2'
implementation 'com.squareup.retrofit2:converter-gson:2.1.0'
2) Add Internet Permission On Manifest.xml File
<uses-permission android:name="android.permission.INTERNET"/>
3) Api.interface for creating a method
in retrofit url is define in two parts
i) base url (on my code base url is "https://reqres.in/api/")
ii) route (on my code "users" is a route)
complete url is "https://reqres.in/api/users"
import java.util.List;
import retrofit2.Call;
import retrofit2.http.GET;
public interface Api {
@FormUrlEncoded
@POST("users")
Call<ResponseBody> post_data(
@Field("name") String name,
@Field("job") String job
);
}
4) RetrofitClient.class
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class RetrofitClient {
private String BASE_URL = "https://reqres.in/api/";
private static RetrofitClient mInstance;
private Retrofit retrofit;
private RetrofitClient(){
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
public static synchronized RetrofitClient getmInstance(){
if(mInstance == null) {
mInstance = new RetrofitClient();
}
return mInstance;
}
public Api getApi(){
return retrofit.create(Api.class);
}
}
5) Activity XML Layout File
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".Post">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:padding="20dp">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/et_name"
android:hint="Name"
android:textStyle="bold"
android:textColor="#000000"
android:textSize="25dp"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/et_job"
android:hint="Job"
android:textStyle="bold"
android:textColor="#000000"
android:textSize="25dp"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btn_post"
android:text="Post"/>
</LinearLayout>
</RelativeLayout>
6) Activity Java File
package com.example.admin.retrofitget;
import android.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
public class Post extends AppCompatActivity {
EditText et_name, et_job;
Button btn_post;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_post);
et_name = (EditText)findViewById(R.id.et_name);
et_job = (EditText)findViewById(R.id.et_job);
btn_post = (Button)findViewById(R.id.btn_post);
final Call<ResponseBody> call = RetrofitClient
.getmInstance()
.getApi()
.post_data(et_name.getText().toString(),
et_job.getText().toString());
btn_post.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
try {
String res = response.body().toString();
showMessage("Success", response.body().string());
} catch (Exception ex) {
ex.printStackTrace();
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
showMessage("Error", t.getMessage());
}
});
}
});
}
public void showMessage(String title,String Message){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(true);
builder.setTitle(title);
builder.setMessage(Message);
builder.show();
}
}
No comments:
Post a Comment