1

I want to parse XML response by using Retrofit, but i get null response and i created model class for its each xml tag. i want to get all response print in logcat just like json response print by using new Gson().toJson(response.body) response from web service-

<?xml version="1.0"?>
<string xmlns="http://tempuri.org/">
  <NewDataSet>
    <Table>
      <g_Vill>700</g_Vill>
      <g_Code>47751</g_Code>
      <g_adhaar_card_no>0</g_adhaar_card_no>
      <G_Mobile_Number>9616265075</G_Mobile_Number>
      <g_unique_cd>500109460194</g_unique_cd>
      <G_Name>Nirmala Devi</G_Name>
      <F_Name>Jokhan</F_Name>
      <S_Name>COOP. CANE DEVP. SOC. GHOSI. </S_Name>
      <C_Name>Karkhiya</C_Name>
      <V_Name>Roshangunj</V_Name>
      <bank>Kashi Gomati Bank Ltd</bank>
      <MD_Mode>TROLLY-GATE</MD_Mode>
      <g_bquota>149.56600</g_bquota>
      <g_TArea>0.09700</g_TArea>
      <g_accnt>111552010001742</g_accnt>
      <g_T_Bond>48.06800</g_T_Bond>
      <AvgUpaj>0</AvgUpaj>
      <crushyear>2017-2018</crushyear>
      <Branch>ChalakpurAzamgarh</Branch>
    </Table>
  </NewDataSet>
</string>

i created it's model class by XML to JavaPojo class

and i get null response

EndPointInterface git = ApiClient.getClient().create(EndPointInterface.class);
            Call<GrowerDetailsResponse> call = git.GrowerDetails("700","47751","1819","8");
            call.enqueue(new Callback<GrowerDetailsResponse>() {

                @Override
                public void onResponse(Call<GrowerDetailsResponse> call, retrofit2.Response<GrowerDetailsResponse> response) {
                    Log.e("FundRequest", "hello response : " + response.body().getNewDataSet().getTable().getAvgUpaj());
                }

                @Override
                public void onFailure(Call<GrowerDetailsResponse> call, Throwable t) {
                    Log.e("response", "error "+t.getMessage());

                }
            });
IMSoP
  • 89,526
  • 13
  • 117
  • 169
Divyanshu
  • 462
  • 6
  • 17

1 Answers1

1

You need to add Converter Factory method like this :

Retrofit retrofit = new Retrofit.Builder()  
                  .baseUrl(API_BASE_URL)
    .client(new OkHttpClient())
    .addConverterFactory(SimpleXmlConverterFactory.create())
    .build();

Also need to add these lines to gradle

 compile 'com.squareup.retrofit2:converter-simplexml:2.3.0'

For reference visit : https://futurestud.io/tutorials/retrofit-how-to-integrate-xml-converter

Aman Rawat
  • 375
  • 1
  • 11
  • Is your response is null or the line you are using for logcat (response.body().getNewDataSet().getTable().getAvgUpaj()) this? – Aman Rawat Oct 01 '18 at 09:30
  • yes i am getting null response when using response.body().getNewDataSet().getTable().getAvgUpaj() – Divyanshu Oct 01 '18 at 09:36
  • I used your xml to pojo converter and found out that it was not giving the required pojo. can you please post the pojo u are using .Also there are requirement for some tags which are needed to be declared above the variables. – Aman Rawat Oct 01 '18 at 09:39
  • Copy my response and generate pojo class by [link] (http://pojo.sodhanalibrary.com/) – Divyanshu Oct 01 '18 at 10:22
  • you will have to use tags like @Root etc just like we do to convert json into a java class. refer the link i provided above . – Aman Rawat Oct 01 '18 at 10:43
  • @aman SimpleXml has been duplicated due to some vulnerability, what other alternative is there – Peter Jun 01 '22 at 13:01
  • @Peter https://stackoverflow.com/questions/53774071/what-kind-of-xmlconverter-can-i-use-for-retrofit-in-android You can go to this thread. – Aman Rawat Jun 04 '22 at 07:40
  • @AmanRawat The converter suggested here does not seem to be available on the maven repo, I am working with Spring boot – Peter Jun 13 '22 at 06:50