Teachmeandroidhub
Step -1. Create a new Project name it as you want
Retrofit android-Post Data Using Retrofit in Android:Let's start work with Retrofit in android retrofit is most latest library that work with Rest Api. it is fast and easiest way to posting and getting data from server . Basically retrofit works on okhttp and we use retrofit android for sending data to json on internet so in this topic I'll explain how to post data using Retrofit Step by step that how to work Retrofit in android.
Step -1. Create a new Project name it as you want
Step -2. Add Dependencies in app level gradle like below
compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.google.code.gson:gson:2.6.2'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
Step -3 Design your UI with XML Like
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context="com.example.rajeev.getandpostusingratrofit.MainActivity">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/pname"
android:inputType="textPersonName"
android:hint="patient name"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/bloodg"
android:hint="Blood group"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/city1"
android:hint="City"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:id="@+id/contact1"
android:hint="Contact"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/time1"
android:inputType="time"
android:hint="Time"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/donar1"
android:inputType="textPersonName"
android:hint="Donar name"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/addrees1"
android:hint="Address"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="post"
android:id="@+id/post"/>
</LinearLayout>
Step-4 :-Before Start MainActivity.java You need to create a class and an Interface.
1.Model Class: it is used for crating a constructor in my case it is named as
Patient.java
package com.example.rajeev.getandpostusingratrofit;
/**
* Created by RAJEEV on 01-Aug-18.
*/
public class Patient {
public String bldgr;
public String adress;
public String city;
public String contactno;
public String name;
public String time;
public String donar;
public Patient(String bldgr, String adress, String city, String contactno, String name, String time, String donar) {
this.bldgr = bldgr;
this.adress = adress;
this.city = city;
this.contactno = contactno;
this.name = name;
this.time = time;
this.donar = donar;
}
}
Now It's time to ceate Retrofit client for that named as Service.java interface it will work as interface .
in Retrofit we don't use full Url in single sting we seprate it in two part one Bare Url and another halp used to post here I am using only half part of url in Service.java which is highlighted with red color
Url="amgopalabs.000webhostapp.com/v1/EmergencyRequirement.php/?op=addPatient"
Service.java
package com.example.rajeev.getandpostusingratrofit;
import retrofit2.Call;
import retrofit2.http.Field;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.GET;
import retrofit2.http.POST;
/**
* Created by RAJEEV on 01-Aug-18.
*/
public interface Service {
@FormUrlEncoded
@POST("v1/EmergencyRequirement.php/?op=addPatient")
Call<Result> addPerson(@Field("BloodGroup") String bloodgroup,
@Field("Address") String Address,
@Field("City") String city,@Field("ContactNumber") String contactnumber,@Field("PatientName") String name
,@Field("Time") String Time,
@Field("DonatedBy") String donar);
}
So now all set so now let Work on MainActivity on java
in Main Activity You see the Base Url that the First half of the Url define here.
MainActivity.java
package com.example.rajeev.getandpostusingratrofit;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.telecom.Call;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import okhttp3.Response;
import retrofit2.Callback;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class MainActivity extends AppCompatActivity {
EditText n1,b1,a1,c1,d1,t1,cn1;
Button post;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
n1=(EditText)findViewById(R.id.pname);
b1=(EditText)findViewById(R.id.bloodg);
a1=(EditText)findViewById(R.id.addrees1);
c1=(EditText)findViewById(R.id.city1);
d1=(EditText)findViewById(R.id.donar1);
t1=(EditText)findViewById(R.id.time1);
cn1=(EditText)findViewById(R.id.contact1);
post=(Button)findViewById(R.id.post);
post.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
click();
}
});
}
private void click() {
String name = n1.getText().toString().toLowerCase();
String bldgr = b1.getText().toString().toLowerCase().trim();
String city = c1.getText().toString().toLowerCase().trim();
String contactno = cn1.getText().toString().toLowerCase().trim();
String adress = a1.getText().toString().toLowerCase().trim();
String time = t1.getText().toString().toLowerCase().trim();
String donar = d1.getText().toString().toLowerCase().trim();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://ramgopalabs.000webhostapp.com/")
.addConverterFactory(GsonConverterFactory.create())
.build();
final Service service = retrofit.create(Service.class);
retrofit2.Call<Result> call = service.addPerson(bldgr, adress, city, contactno, name, time, donar);
call.enqueue(new Callback<Result>() {
@Override
public void onResponse(retrofit2.Call<Result> call, retrofit2.Response<Result> response) {
Toast.makeText(MainActivity.this, response.body().result, Toast.LENGTH_LONG).show();
}
@Override
public void onFailure(retrofit2.Call<Result> call, Throwable t) {
Toast.makeText(MainActivity.this, t.getLocalizedMessage(), Toast.LENGTH_LONG).show();
}
});
}
}
create anther class Result.java
package com.example.rajeev.getandpostusingratrofit;
/**
* Created by RAJEEV on 01-Aug-18.
*/
public class Result {
public final String result;
public Result(String result) {
this.result = result;
}
}
Finally add Internet Permission and AndroidMenifest.xml
<uses-permission android:name="android.permission.INTERNET" />
Thanks For Visit
Comments