Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {
- "contacts": [
- {
- "id": "c200",
- "name": "Ravi Tamada",
- "email": "ravi@gmail.com",
- "address": "xx-xx-xxxx,x - street, x - country",
- "gender" : "male",
- "phone": {
- "mobile": "+91 0000000000",
- "home": "00 000000",
- "office": "00 000000"
- }
- },
- {
- "id": "c201",
- "name": "Johnny Depp",
- "email": "johnny_depp@gmail.com",
- "address": "xx-xx-xxxx,x - street, x - country",
- "gender" : "male",
- "phone": {
- "mobile": "+91 0000000000",
- "home": "00 000000",
- "office": "00 000000"
- }
- },
- {
- "id": "c202",
- "name": "Leonardo Dicaprio",
- "email": "leonardo_dicaprio@gmail.com",
- "address": "xx-xx-xxxx,x - street, x - country",
- "gender" : "male",
- "phone": {
- "mobile": "+91 0000000000",
- "home": "00 000000",
- "office": "00 000000"
- }
- },
- {
- "id": "c203",
- "name": "John Wayne",
- "email": "john_wayne@gmail.com",
- "address": "xx-xx-xxxx,x - street, x - country",
- "gender" : "male",
- "phone": {
- "mobile": "+91 0000000000",
- "home": "00 000000",
- "office": "00 000000"
- }
- },
- {
- "id": "c204",
- "name": "Angelina Jolie",
- "email": "angelina_jolie@gmail.com",
- "address": "xx-xx-xxxx,x - street, x - country",
- "gender" : "female",
- "phone": {
- "mobile": "+91 0000000000",
- "home": "00 000000",
- "office": "00 000000"
- }
- },
- {
- "id": "c205",
- "name": "Dido",
- "email": "dido@gmail.com",
- "address": "xx-xx-xxxx,x - street, x - country",
- "gender" : "female",
- "phone": {
- "mobile": "+91 0000000000",
- "home": "00 000000",
- "office": "00 000000"
- }
- },
- {
- "id": "c206",
- "name": "Adele",
- "email": "adele@gmail.com",
- "address": "xx-xx-xxxx,x - street, x - country",
- "gender" : "female",
- "phone": {
- "mobile": "+91 0000000000",
- "home": "00 000000",
- "office": "00 000000"
- }
- },
- {
- "id": "c207",
- "name": "Hugh Jackman",
- "email": "hugh_jackman@gmail.com",
- "address": "xx-xx-xxxx,x - street, x - country",
- "gender" : "male",
- "phone": {
- "mobile": "+91 0000000000",
- "home": "00 000000",
- "office": "00 000000"
- }
- },
- {
- "id": "c208",
- "name": "Will Smith",
- "email": "will_smith@gmail.com",
- "address": "xx-xx-xxxx,x - street, x - country",
- "gender" : "male",
- "phone": {
- "mobile": "+91 0000000000",
- "home": "00 000000",
- "office": "00 000000"
- }
- },
- {
- "id": "c209",
- "name": "Clint Eastwood",
- "email": "clint_eastwood@gmail.com",
- "address": "xx-xx-xxxx,x - street, x - country",
- "gender" : "male",
- "phone": {
- "mobile": "+91 0000000000",
- "home": "00 000000",
- "office": "00 000000"
- }
- },
- {
- "id": "c2010",
- "name": "Barack Obama",
- "email": "barack_obama@gmail.com",
- "address": "xx-xx-xxxx,x - street, x - country",
- "gender" : "male",
- "phone": {
- "mobile": "+91 0000000000",
- "home": "00 000000",
- "office": "00 000000"
- }
- },
- {
- "id": "c2011",
- "name": "Kate Winslet",
- "email": "kate_winslet@gmail.com",
- "address": "xx-xx-xxxx,x - street, x - country",
- "gender" : "female",
- "phone": {
- "mobile": "+91 0000000000",
- "home": "00 000000",
- "office": "00 000000"
- }
- },
- {
- "id": "c2012",
- "name": "Eminem",
- "email": "eminem@gmail.com",
- "address": "xx-xx-xxxx,x - street, x - country",
- "gender" : "male",
- "phone": {
- "mobile": "+91 0000000000",
- "home": "00 000000",
- "office": "00 000000"
- }
- }
- ]
- }
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.support.v7.widget.LinearLayoutManager;
- import android.support.v7.widget.RecyclerView;
- import android.util.Log;
- import android.widget.Toast;
- import com.jakewharton.retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import io.reactivex.android.schedulers.AndroidSchedulers;
- import io.reactivex.disposables.CompositeDisposable;
- import io.reactivex.schedulers.Schedulers;
- import okhttp3.OkHttpClient;
- import okhttp3.logging.HttpLoggingInterceptor;
- import retrofit2.Retrofit;
- import retrofit2.converter.gson.GsonConverterFactory;
- public class MainActivity extends AppCompatActivity {
- public static final String BASE_URL = "https://api.androidhive.info/";
- private RecyclerView mRecyclerView;
- private CompositeDisposable mCompositeDisposable;
- private DataAdapter mAdapter;
- private ArrayList<Example> mAndroidArrayList;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- mCompositeDisposable = new CompositeDisposable();
- initRecyclerView();
- loadJSON();
- }
- private void initRecyclerView() {
- mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
- mRecyclerView.setHasFixedSize(true);
- RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
- mRecyclerView.setLayoutManager(layoutManager);
- }
- private void loadJSON() {
- RequestInterface requestInterface = new Retrofit.Builder()
- .baseUrl(BASE_URL)
- .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
- .addConverterFactory(GsonConverterFactory.create())
- .build().create(RequestInterface.class);
- mCompositeDisposable.add(requestInterface.register()
- .observeOn(AndroidSchedulers.mainThread())
- .subscribeOn(Schedulers.io())
- .subscribe(this::handleResponse,this::handleError));
- }
- private void handleResponse(List<Example> androidList) {
- mAndroidArrayList = new ArrayList<>(androidList);
- mAdapter = new DataAdapter(mAndroidArrayList);
- mRecyclerView.setAdapter(mAdapter);
- }
- private void handleError(Throwable error) {
- Toast.makeText(this, "Error "+error.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
- Log.e("ca", "Error "+error.getLocalizedMessage());
- }
- @Override
- public void onDestroy() {
- super.onDestroy();
- mCompositeDisposable.clear();
- }
- }
- package com.example.kostya.textto;
- import com.google.gson.annotations.Expose;
- import com.google.gson.annotations.SerializedName;
- import java.util.ArrayList;
- import java.util.List;
- public class Example {
- @SerializedName("contacts")
- @Expose
- private List<Android> contacts;
- public List<Android> getContacts() {
- return contacts;
- }
- public void setContacts(List<Android> contacts) {
- this.contacts = contacts;
- }
- }
- package com.example.kostya.textto;
- import com.google.gson.annotations.Expose;
- import com.google.gson.annotations.SerializedName;
- public class Android {
- @SerializedName("id")
- @Expose
- private String id;
- @SerializedName("name")
- @Expose
- private String name;
- @SerializedName("email")
- @Expose
- private String email;
- @SerializedName("address")
- @Expose
- private String address;
- @SerializedName("gender")
- @Expose
- private String gender;
- @SerializedName("phone")
- @Expose
- private Phone phone;
- public String getId() {
- return id;
- }
- public void setId(String id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getEmail() {
- return email;
- }
- public void setEmail(String email) {
- this.email = email;
- }
- public String getAddress() {
- return address;
- }
- public void setAddress(String address) {
- this.address = address;
- }
- public String getGender() {
- return gender;
- }
- public void setGender(String gender) {
- this.gender = gender;
- }
- public Phone getPhone() {
- return phone;
- }
- public void setPhone(Phone phone) {
- this.phone = phone;
- }
- }
- package com.example.kostya.textto;
- import com.google.gson.annotations.Expose;
- import com.google.gson.annotations.SerializedName;
- public class Phone {
- @SerializedName("mobile")
- @Expose
- private String mobile;
- @SerializedName("home")
- @Expose
- private String home;
- @SerializedName("office")
- @Expose
- private String office;
- public String getMobile() {
- return mobile;
- }
- public void setMobile(String mobile) {
- this.mobile = mobile;
- }
- public String getHome() {
- return home;
- }
- public void setHome(String home) {
- this.home = home;
- }
- public String getOffice() {
- return office;
- }
- public void setOffice(String office) {
- this.office = office;
- }
- }
- package com.example.kostya.textto;
- import java.util.List;
- import io.reactivex.Observable;
- import retrofit2.http.GET;
- public interface RequestInterface {
- @GET("contacts/")
- Observable<List<Example>> register();
- }
- public interface RequestInterface {
- @GET("contacts/")
- //Observable<List<Example>> register();
- Observable<Example> register();
- }
Add Comment
Please, Sign In to add comment