-1

In my android app. I am using retrofit2. I want to get json details from the api. The response is coming but the json objects are showing as null. I am getting the json objects size. But i did not getting the values. How to parse the json from response.body. I want to get the value of DATA_TITLE,DATA_IMAGE, DATA.

I am first time using retrofit. I have tried different methods. Please help me.

My json code is shown below. And the all source codes are also shown in below.

[{
        "ID": {
            "Tag": "string",
            "Method": 5,
            "Value": "68613",
            "status": true
        },
        "DATA_TITLE": {
            "Tag": "string",
            "Method": 5,
            "Value": "Hai aaaaaaa",
            "status": true
        },
        "DATA_IMAGE": {
            "Tag": "string",
            "Method": 5,
            "Value": "http://xxxxxx.com/desktopModules/Farabi.ICatalog/CatalogImages/488/C-2048_sd-eng.jpg",
            "status": true
        },
        "DATA": {
            "Tag": "string",
            "Method": 5,
            "Value": "",
            "status": true
        }
    }, {
        "ID": {
            "Tag": "string",
            "Method": 5,
            "Value": "68613",
            "status": true
        },
        "DATA_TITLE": {
            "Tag": "string",
            "Method": 5,
            "Value": "Hai aaaaaaa",
            "status": true
        },
        "DATA_IMAGE": {
            "Tag": "string",
            "Method": 5,
            "Value": "http://xxxxxx.com/desktopModules/Farabi.ICatalog/CatalogImages/488/C-2048_sd-eng.jpg",
            "status": true
        },
        "DATA": {
            "Tag": "string",
            "Method": 5,
            "Value": "",
            "status": true
        }
    }

]

And my gradle code is

compile 'com.google.code.gson:gson:2.6.2'
compile 'com.squareup.retrofit2:retrofit:2.0.2'
compile 'com.squareup.retrofit2:converter-gson:2.0.2'

MainActivity.java

public class MainActivity extends AppCompatActivity {
private ArrayList<List<Objects>> data;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ApiInterface apiService =
            ApiClient.getClient().create(ApiInterface.class);


    Call<List<DataResponse>> call =apiService.getDetails(Util.TemplateName,Util.p,Util.Handler,Util.AppName,Util.Type,Util.F);
    call.enqueue(new Callback<List<DataResponse>>() {
        @Override
        public void onResponse(Call<List<DataResponse>> call, Response<List<DataResponse>> response) {


            if (response.isSuccessful()) {
                String json = gson.toJson(response.body());
                List<DataResponse> dataResponses = response.body();
                Log.d("LOG","Response size:"+dataResponses.size());
                Log.d("LOG","Response data:"+dataResponses.get(0).getDataList());

  // I want to get the value of DATA_TITLE,DATA_IMAGE, DATA

            }
        }

        @Override
        public void onFailure(Call<List<DataResponse>> call, Throwable t) {
            Log.e("LOG","Response error:"+t);
        }
    });


}
}

ApiInterface

public interface ApiInterface {

@GET("getdetails")
Call<List<DataResponse>> getDetails(@Query("TemplateName") String TemplateName);
}

ApiClient.java

public class ApiClient {
private static Retrofit retrofit = null;
public static Retrofit getClient() {
    if (retrofit==null) {
        retrofit = new Retrofit.Builder()
                .baseUrl(Util.URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }
    return retrofit;
}
}

DataResponse

public class DataResponse {

private List<Objects> results;
@SerializedName("")
@Expose
private List<Objects> dataList = new ArrayList<>();

public List<Objects> getDataList() {
    return dataList;
}
public List<Objects> getResults() {
    return results;
}
public void setResults(List<Objects> results) {
    this.results = results;
}
}

Objects.java

public class Objects {

@SerializedName("ID")
private String id;
public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}
}
Joe
  • 246
  • 1
  • 3
  • 15

2 Answers2

2

First of all see if everything is working fine. Replace all your List<DataResponse> with JsonElement and then hit your webservice. After getting response in response.body(), either parse manually or best way is to copy your response and go to http://www.jsonschema2pojo.org/ and create POJO class. After then pass the new POJO class object in your <PojoObject> retrofit api. For more on POJO refer here

karanatwal.github.io
  • 3,613
  • 3
  • 25
  • 57
1

It's a detail but I am always adding the tag @Headers while getting or sending JSON objects.

e.g.

  @Headers("Content-Type: application/json")
    @GET("articles")
    Call<String> getArticles();