0

{ "status": true, "message": "Welcome jaymin", "data": { "id": 1, "name": "jaymin", "email": "jaymin@gmail.com", "mobile": "123456" } }

2 Answers2

1

you can add GsonFactory or can JacksonFactory in creating retrofit service and can use this link http://www.jsonschema2pojo.org/ to create a pojo class through which you can parse data. I have converted your JSON into Gson format java class which you can use in android to parse data.

   -----------------------------------com.example.Data.java----------------------- 
   ------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Data {

@SerializedName("id")
@Expose
private Integer id;
@SerializedName("name")
@Expose
private String name;
@SerializedName("email")
@Expose
private String email;
@SerializedName("mobile")
@Expose
private String mobile;

public Integer getId() {
return id;
}

public void setId(Integer 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 getMobile() {
return mobile;
}

public void setMobile(String mobile) {
this.mobile = mobile;
}

}
-----------------------------------com.example.FollowersResponse.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class FollowersResponse {

@SerializedName("status")
@Expose
private Boolean status;
@SerializedName("message")
@Expose
private String message;
@SerializedName("data")
@Expose
private Data data;

public Boolean getStatus() {
return status;
}

public void setStatus(Boolean status) {
this.status = status;
}

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

public Data getData() {
return data;
}

public void setData(Data data) {
this.data = data;
}

}
Abhishek Sharma
  • 271
  • 2
  • 11
  • I have used this before..But i am always getting null in onresponse method – Jaimin Thakkar Sep 16 '18 at 18:45
  • Retrofit.Builder retrofitBuilder = new Retrofit.Builder() .baseUrl(BASE_URL) .addConverterFactory(GsonConverterFactory.create()); Retrofit retrofit = retrofitBuilder.client(httpClient.build()).build(); return retrofit.create(ApiClient.class); you have to add the line addConverterFactory() in creation of retrofit @JaiminThakkar and have to add this dependency in gradle:- implementation 'com.squareup.retrofit2:converter-gson:2.4.0' – Abhishek Sharma Sep 16 '18 at 18:47
  • I have also use that – Jaimin Thakkar Sep 16 '18 at 18:53
  • Can you share the proper code where you are facing problem? I can tell you the exact solution after that only. – Abhishek Sharma Sep 16 '18 at 18:54
  • public class RetrofitClient { private static Retrofit retrofit = null; public static Retrofit getClient(String baseUrl) { if (retrofit==null) { retrofit = new Retrofit.Builder() .baseUrl(baseUrl) .addConverterFactory(GsonConverterFactory.create()) .build(); } return retrofit; } } This is my builder class – Jaimin Thakkar Sep 16 '18 at 18:56
  • ok how you are using it in your code while fetching the data ? I mean to say How you are making a network call ? – Abhishek Sharma Sep 16 '18 at 18:58
  • I have got null pointer error for this @SerializedName("data") @Expose private Data data; – Jaimin Thakkar Sep 16 '18 at 19:02
  • java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String exapense.roommate.com.retrofitjava.modal.LoginResponse.getName()' on a null object reference – Jaimin Thakkar Sep 16 '18 at 19:03
  • There can be two cases here :- Either your data is coming null from the server or your data key is changed to another name . – Abhishek Sharma Sep 16 '18 at 19:08
  • LoginResponse class is your Data class ?? @JaiminThakkar – Abhishek Sharma Sep 16 '18 at 19:09
  • Got the solution mate.. Defined the constuctor in pojo class and got the solution @abhishek sharma – Jaimin Thakkar Sep 16 '18 at 19:12
0

To phrase your string to a JSONObject you do the following

String jsonString = '{ "status": true, "message": "Welcome jaymin", "data": { "id": 1, "name": "jaymin", "email": "jaymin@gmail.com", "mobile": "123456" } }'
JSONObject jsonObj = new JSONObject(jsonString)

Then you can retrieve values from the JSONObject like so

String message = jsonObj.get("message") //Message = "Welcome jaymin"
Jamie Turner
  • 71
  • 1
  • 5