Guest User

Untitled

a guest
Dec 14th, 2017
585
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.20 KB | None | 0 0
  1. {
  2. "contacts": [
  3. {
  4. "id": "c200",
  5. "name": "Ravi Tamada",
  6. "email": "ravi@gmail.com",
  7. "address": "xx-xx-xxxx,x - street, x - country",
  8. "gender" : "male",
  9. "phone": {
  10. "mobile": "+91 0000000000",
  11. "home": "00 000000",
  12. "office": "00 000000"
  13. }
  14. },
  15. {
  16. "id": "c201",
  17. "name": "Johnny Depp",
  18. "email": "johnny_depp@gmail.com",
  19. "address": "xx-xx-xxxx,x - street, x - country",
  20. "gender" : "male",
  21. "phone": {
  22. "mobile": "+91 0000000000",
  23. "home": "00 000000",
  24. "office": "00 000000"
  25. }
  26. },
  27. {
  28. "id": "c202",
  29. "name": "Leonardo Dicaprio",
  30. "email": "leonardo_dicaprio@gmail.com",
  31. "address": "xx-xx-xxxx,x - street, x - country",
  32. "gender" : "male",
  33. "phone": {
  34. "mobile": "+91 0000000000",
  35. "home": "00 000000",
  36. "office": "00 000000"
  37. }
  38. },
  39. {
  40. "id": "c203",
  41. "name": "John Wayne",
  42. "email": "john_wayne@gmail.com",
  43. "address": "xx-xx-xxxx,x - street, x - country",
  44. "gender" : "male",
  45. "phone": {
  46. "mobile": "+91 0000000000",
  47. "home": "00 000000",
  48. "office": "00 000000"
  49. }
  50. },
  51. {
  52. "id": "c204",
  53. "name": "Angelina Jolie",
  54. "email": "angelina_jolie@gmail.com",
  55. "address": "xx-xx-xxxx,x - street, x - country",
  56. "gender" : "female",
  57. "phone": {
  58. "mobile": "+91 0000000000",
  59. "home": "00 000000",
  60. "office": "00 000000"
  61. }
  62. },
  63. {
  64. "id": "c205",
  65. "name": "Dido",
  66. "email": "dido@gmail.com",
  67. "address": "xx-xx-xxxx,x - street, x - country",
  68. "gender" : "female",
  69. "phone": {
  70. "mobile": "+91 0000000000",
  71. "home": "00 000000",
  72. "office": "00 000000"
  73. }
  74. },
  75. {
  76. "id": "c206",
  77. "name": "Adele",
  78. "email": "adele@gmail.com",
  79. "address": "xx-xx-xxxx,x - street, x - country",
  80. "gender" : "female",
  81. "phone": {
  82. "mobile": "+91 0000000000",
  83. "home": "00 000000",
  84. "office": "00 000000"
  85. }
  86. },
  87. {
  88. "id": "c207",
  89. "name": "Hugh Jackman",
  90. "email": "hugh_jackman@gmail.com",
  91. "address": "xx-xx-xxxx,x - street, x - country",
  92. "gender" : "male",
  93. "phone": {
  94. "mobile": "+91 0000000000",
  95. "home": "00 000000",
  96. "office": "00 000000"
  97. }
  98. },
  99. {
  100. "id": "c208",
  101. "name": "Will Smith",
  102. "email": "will_smith@gmail.com",
  103. "address": "xx-xx-xxxx,x - street, x - country",
  104. "gender" : "male",
  105. "phone": {
  106. "mobile": "+91 0000000000",
  107. "home": "00 000000",
  108. "office": "00 000000"
  109. }
  110. },
  111. {
  112. "id": "c209",
  113. "name": "Clint Eastwood",
  114. "email": "clint_eastwood@gmail.com",
  115. "address": "xx-xx-xxxx,x - street, x - country",
  116. "gender" : "male",
  117. "phone": {
  118. "mobile": "+91 0000000000",
  119. "home": "00 000000",
  120. "office": "00 000000"
  121. }
  122. },
  123. {
  124. "id": "c2010",
  125. "name": "Barack Obama",
  126. "email": "barack_obama@gmail.com",
  127. "address": "xx-xx-xxxx,x - street, x - country",
  128. "gender" : "male",
  129. "phone": {
  130. "mobile": "+91 0000000000",
  131. "home": "00 000000",
  132. "office": "00 000000"
  133. }
  134. },
  135. {
  136. "id": "c2011",
  137. "name": "Kate Winslet",
  138. "email": "kate_winslet@gmail.com",
  139. "address": "xx-xx-xxxx,x - street, x - country",
  140. "gender" : "female",
  141. "phone": {
  142. "mobile": "+91 0000000000",
  143. "home": "00 000000",
  144. "office": "00 000000"
  145. }
  146. },
  147. {
  148. "id": "c2012",
  149. "name": "Eminem",
  150. "email": "eminem@gmail.com",
  151. "address": "xx-xx-xxxx,x - street, x - country",
  152. "gender" : "male",
  153. "phone": {
  154. "mobile": "+91 0000000000",
  155. "home": "00 000000",
  156. "office": "00 000000"
  157. }
  158. }
  159. ]
  160. }
  161.  
  162. import android.support.v7.app.AppCompatActivity;
  163. import android.os.Bundle;
  164. import android.support.v7.widget.LinearLayoutManager;
  165. import android.support.v7.widget.RecyclerView;
  166. import android.util.Log;
  167. import android.widget.Toast;
  168.  
  169.  
  170. import com.jakewharton.retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
  171.  
  172. import java.util.ArrayList;
  173. import java.util.List;
  174. import java.util.logging.Level;
  175. import java.util.logging.Logger;
  176.  
  177. import io.reactivex.android.schedulers.AndroidSchedulers;
  178. import io.reactivex.disposables.CompositeDisposable;
  179. import io.reactivex.schedulers.Schedulers;
  180. import okhttp3.OkHttpClient;
  181. import okhttp3.logging.HttpLoggingInterceptor;
  182. import retrofit2.Retrofit;
  183. import retrofit2.converter.gson.GsonConverterFactory;
  184.  
  185. public class MainActivity extends AppCompatActivity {
  186.  
  187. public static final String BASE_URL = "https://api.androidhive.info/";
  188.  
  189. private RecyclerView mRecyclerView;
  190.  
  191. private CompositeDisposable mCompositeDisposable;
  192.  
  193. private DataAdapter mAdapter;
  194.  
  195. private ArrayList<Example> mAndroidArrayList;
  196.  
  197. @Override
  198. protected void onCreate(Bundle savedInstanceState) {
  199. super.onCreate(savedInstanceState);
  200. setContentView(R.layout.activity_main);
  201.  
  202. mCompositeDisposable = new CompositeDisposable();
  203. initRecyclerView();
  204. loadJSON();
  205. }
  206.  
  207. private void initRecyclerView() {
  208.  
  209. mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
  210. mRecyclerView.setHasFixedSize(true);
  211. RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
  212. mRecyclerView.setLayoutManager(layoutManager);
  213. }
  214.  
  215. private void loadJSON() {
  216.  
  217.  
  218. RequestInterface requestInterface = new Retrofit.Builder()
  219. .baseUrl(BASE_URL)
  220. .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
  221. .addConverterFactory(GsonConverterFactory.create())
  222. .build().create(RequestInterface.class);
  223.  
  224. mCompositeDisposable.add(requestInterface.register()
  225. .observeOn(AndroidSchedulers.mainThread())
  226. .subscribeOn(Schedulers.io())
  227. .subscribe(this::handleResponse,this::handleError));
  228.  
  229.  
  230. }
  231.  
  232.  
  233.  
  234.  
  235. private void handleResponse(List<Example> androidList) {
  236.  
  237. mAndroidArrayList = new ArrayList<>(androidList);
  238. mAdapter = new DataAdapter(mAndroidArrayList);
  239. mRecyclerView.setAdapter(mAdapter);
  240. }
  241.  
  242. private void handleError(Throwable error) {
  243.  
  244. Toast.makeText(this, "Error "+error.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
  245. Log.e("ca", "Error "+error.getLocalizedMessage());
  246. }
  247.  
  248. @Override
  249. public void onDestroy() {
  250. super.onDestroy();
  251. mCompositeDisposable.clear();
  252. }
  253. }
  254.  
  255.  
  256.  
  257.  
  258. package com.example.kostya.textto;
  259.  
  260. import com.google.gson.annotations.Expose;
  261. import com.google.gson.annotations.SerializedName;
  262.  
  263. import java.util.ArrayList;
  264. import java.util.List;
  265.  
  266.  
  267. public class Example {
  268.  
  269. @SerializedName("contacts")
  270. @Expose
  271. private List<Android> contacts;
  272.  
  273. public List<Android> getContacts() {
  274. return contacts;
  275. }
  276.  
  277. public void setContacts(List<Android> contacts) {
  278. this.contacts = contacts;
  279. }
  280. }
  281.  
  282.  
  283.  
  284. package com.example.kostya.textto;
  285.  
  286. import com.google.gson.annotations.Expose;
  287. import com.google.gson.annotations.SerializedName;
  288.  
  289.  
  290. public class Android {
  291. @SerializedName("id")
  292. @Expose
  293. private String id;
  294. @SerializedName("name")
  295. @Expose
  296. private String name;
  297. @SerializedName("email")
  298. @Expose
  299. private String email;
  300. @SerializedName("address")
  301. @Expose
  302. private String address;
  303. @SerializedName("gender")
  304. @Expose
  305. private String gender;
  306. @SerializedName("phone")
  307. @Expose
  308. private Phone phone;
  309.  
  310. public String getId() {
  311. return id;
  312. }
  313.  
  314. public void setId(String id) {
  315. this.id = id;
  316. }
  317.  
  318. public String getName() {
  319. return name;
  320. }
  321.  
  322. public void setName(String name) {
  323. this.name = name;
  324. }
  325.  
  326. public String getEmail() {
  327. return email;
  328. }
  329.  
  330. public void setEmail(String email) {
  331. this.email = email;
  332. }
  333.  
  334. public String getAddress() {
  335. return address;
  336. }
  337.  
  338. public void setAddress(String address) {
  339. this.address = address;
  340. }
  341.  
  342. public String getGender() {
  343. return gender;
  344. }
  345.  
  346. public void setGender(String gender) {
  347. this.gender = gender;
  348. }
  349.  
  350. public Phone getPhone() {
  351. return phone;
  352. }
  353.  
  354. public void setPhone(Phone phone) {
  355. this.phone = phone;
  356. }
  357.  
  358. }
  359.  
  360.  
  361.  
  362. package com.example.kostya.textto;
  363.  
  364. import com.google.gson.annotations.Expose;
  365. import com.google.gson.annotations.SerializedName;
  366.  
  367.  
  368.  
  369. public class Phone {
  370.  
  371. @SerializedName("mobile")
  372. @Expose
  373. private String mobile;
  374. @SerializedName("home")
  375. @Expose
  376. private String home;
  377. @SerializedName("office")
  378. @Expose
  379. private String office;
  380.  
  381. public String getMobile() {
  382. return mobile;
  383. }
  384.  
  385. public void setMobile(String mobile) {
  386. this.mobile = mobile;
  387. }
  388.  
  389. public String getHome() {
  390. return home;
  391. }
  392.  
  393. public void setHome(String home) {
  394. this.home = home;
  395. }
  396.  
  397. public String getOffice() {
  398. return office;
  399. }
  400.  
  401. public void setOffice(String office) {
  402. this.office = office;
  403. }
  404. }
  405.  
  406.  
  407. package com.example.kostya.textto;
  408.  
  409. import java.util.List;
  410.  
  411. import io.reactivex.Observable;
  412. import retrofit2.http.GET;
  413.  
  414.  
  415. public interface RequestInterface {
  416. @GET("contacts/")
  417. Observable<List<Example>> register();
  418.  
  419. }
  420.  
  421. public interface RequestInterface {
  422. @GET("contacts/")
  423. //Observable<List<Example>> register();
  424. Observable<Example> register();
  425.  
  426. }
Add Comment
Please, Sign In to add comment