Retrofit is network library which uses http request to get data from the server . I am going to give you a example by using complex json and post method to load data .
Manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.vishalsingh.retrofit">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:networkSecurityConfig="@xml/actions"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity" />
<activity android:name=".Main2Activity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="com.google.android.actions"
android:resource="@xml/actions" />
</application>
</manifest>
Library pojo.java
package com.vishalsingh.retrofit.Pojo;
import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Library {
@SerializedName("Status")
@Expose private Boolean status;
@SerializedName("Error")
@Expose private Error error;
@SerializedName("Response")
@Expose
private List<Response> response = null;
public Boolean getStatus() {
return status; }
public void setStatus(Boolean status) {
this.status = status; }
public Error getError() {
return error; }
public void setError(Error error) {
this.error = error; }
public List<Response> getResponse() {
return response; }
public void setResponse(List<Response> response) {
this.response = response; }
}
Error pojo.java
package com.vishalsingh.retrofit.Pojo;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Error {
@SerializedName("Code")
@Expose private Integer code;
@SerializedName("Message")
@Expose private String message;
public Integer getCode() {
return code; }
public void setCode(Integer code) {
this.code = code; }
public String getMessage() {
return message; }
public void setMessage(String message) {
this.message = message; }
}
Response pojo.java
package com.vishalsingh.retrofit.Pojo;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Response {
@SerializedName("Name")
@Expose private String name; @SerializedName("Image")
@Expose private String image; @SerializedName("Gender")
@Expose private String gender; @SerializedName("DateOfBirth")
@Expose private String dateOfBirth; @SerializedName("Address")
@Expose private String address; @SerializedName("Country")
@Expose private String country; @SerializedName("PinCode")
@Expose private String pinCode; @SerializedName("EmailId")
@Expose private String emailId; @SerializedName("MobileNo")
@Expose private String mobileNo; @SerializedName("isPartner")
@Expose private String isPartner; @SerializedName("City")
@Expose private String city; @SerializedName("State")
@Expose private String state; @SerializedName("Booth")
@Expose private String booth; @SerializedName("Constituency")
@Expose private String constituency; @SerializedName("Assembly")
@Expose private String assembly; @SerializedName("Citizenship")
@Expose private Object citizenship;
public String getName() {
return name; }
public void setName(String name) {
this.name = name; }
public String getImage() {
return image; }
public void setImage(String image) {
this.image = image; }
public String getGender() {
return gender; }
public void setGender(String gender) {
this.gender = gender; }
public String getDateOfBirth() {
return dateOfBirth; }
public void setDateOfBirth(String dateOfBirth) {
this.dateOfBirth = dateOfBirth; }
public String getAddress() {
return address; }
public void setAddress(String address) {
this.address = address; }
public String getCountry() {
return country; }
public void setCountry(String country) {
this.country = country; }
public String getPinCode() {
return pinCode; }
public void setPinCode(String pinCode) {
this.pinCode = pinCode; }
public String getEmailId() {
return emailId; }
public void setEmailId(String emailId) {
this.emailId = emailId; }
public String getMobileNo() {
return mobileNo; }
public void setMobileNo(String mobileNo) {
this.mobileNo = mobileNo; }
public String getIsPartner() {
return isPartner; }
public void setIsPartner(String isPartner) {
this.isPartner = isPartner; }
public String getCity() {
return city; }
public void setCity(String city) {
this.city = city; }
public String getState() {
return state; }
public void setState(String state) {
this.state = state; }
public String getBooth() {
return booth; }
public void setBooth(String booth) {
this.booth = booth; }
public String getConstituency() {
return constituency; }
public void setConstituency(String constituency) {
this.constituency = constituency; }
public String getAssembly() {
return assembly; }
public void setAssembly(String assembly) {
this.assembly = assembly; }
public Object getCitizenship() {
return citizenship; }
public void setCitizenship(Object citizenship) {
this.citizenship = citizenship; }
}
Apiclient.java
package com.vishalsingh.retrofit;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class APIClient {
private static Retrofit retrofit = null;
static Retrofit getClient() {
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();
retrofit = new Retrofit.Builder()
.baseUrl("Your base ur")
.addConverterFactory(GsonConverterFactory.create())
.client(client)
.build();
return retrofit; }
}
ApiInterface.java
note: name it as new api
package com.vishalsingh.retrofit;
import com.vishalsingh.retrofit.Pojo.Library;
import java.util.List;
import java.util.Map;
import retrofit2.Call;
import retrofit2.http.Field;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.GET;
import retrofit2.http.POST;
interface NewApi {
@FormUrlEncoded
@POST(" Your post method ?")
Call<Library> doCreateUserWithField(@Field("UserId") String UserId);}
MainActivity.java
package com.vishalsingh.retrofit;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;import android.widget.TextView;
import android.widget.Toast;
import com.vishalsingh.retrofit.Pojo.Library;
import java.util.HashMap;import java.util.List;
import java.util.Map;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class Main2Activity extends AppCompatActivity {
TextView text1;
NewApi apiInterface;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
text1 = (TextView) findViewById(R.id.text1);
apiInterface = APIClient.getClient().create(NewApi.class);
Call<Library> call = apiInterface.doCreateUserWithField("your parameter");
call.enqueue(new Callback<Library>() {
@Override
public void onResponse(Call<Library> call, Response<Library> response) {
Library userlist = response.body();
List<com.vishalsingh.retrofit.Pojo.Response> newrepo = userlist.getResponse(); String name="", address="", email=""; for (int i = 0; i <newrepo.size(); i++) {
name = newrepo.get(i).getName();
address = newrepo.get(i).getAddress();
email = newrepo.get(i).getEmailId();
}
text1.setText(name + "\n" + address + "\n" + email); }
@Override
public void onFailure(Call<Library> call, Throwable t) {
Toast.makeText(Main2Activity.this, "cannot load data", Toast.LENGTH_SHORT).show(); }
}); }
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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=".Main2Activity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/text1"/>
</android.support.constraint.ConstraintLayout>
Sample json
{
"Status":true,
"Error":
{
"Code":0,
"Message":""
},
"Response":
[{"Name":"Shubham Shrivastava",
"Image":"http://103.115.194.111:906/Readers/uploads/Screenshot_20190424-202039.jpg",
"Gender":"Mr.",
"DateOfBirth":"26 Dec 2018",
"Address":"Noida",
"Country":"INDIA",
"PinCode":"485001",
"EmailId":"shri.shubham86@gmail.com",
"MobileNo":"9074762379",
"isPartner":"No",
"City":"",
"State":"Madhya Pradesh",
"Booth":"Other",
"Constituency":"Satna",
"Assembly":"Satna",
"Citizenship":null}
]
}
Note
In case of problem in pi create network configuration xml file
and write this code
<?xml version ="1.0" encoding ="utf-8"?>
<network-security-config>
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">Your domain or IP address</domain>
</domain-config>
</network-security-config>