2

I am trying to download a file from server using retrofit. Using HttpLoggingInterceptor I tried logging what is happening. I can find the file name. But the response body is empty.

I am new to using retrofit. can some one point me out in the right direction on where I am going wrong

Retrofit Client Interface:

public interface DownloadTransactions {

@Streaming
@GET("common/frontend/member/download.aspx")
Call<Void> getBalancesAndTransactions(@Header("Cookie") String header);

}

Java call:

void downloadData() {

    OkHttpClient.Builder client = new OkHttpClient.Builder();
    client.connectTimeout(15, TimeUnit.SECONDS)
            .readTimeout(15, TimeUnit.SECONDS);

    if (BuildConfig.DEBUG) {
        HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
        logging.setLevel(HttpLoggingInterceptor.Level.BODY);

        client.addInterceptor(logging);
    }


    Retrofit.Builder builder = new Retrofit.Builder();
    Retrofit retrofit = builder.client(client.build())
            .baseUrl("https://" + getString(R.string.root_url))
            .addConverterFactory(ScalarsConverterFactory.create())
            .build();

    DownloadTransactions downloadTransactions = retrofit.create(DownloadTransactions.class);
    Call<Void> call = downloadTransactions.getBalancesAndTransactions(CommsManager.getInstance().getASPSessionId());
    call.enqueue(new Callback<Void>() {
        @Override
        public void onResponse(Call<Void> call, retrofit2.Response<Void> response) {
            Log.v(TAG, "success transactions: --------" + response);
        }

        @Override
        public void onFailure(Call<Void> call, Throwable t) {
            Log.v(TAG, "failure transactions: --------");
        }
    });


}

Response in log:

<-- 200 OK https://xxxxx.com/common/xxx/member/download.aspx (387ms)
D/OkHttp: Cache-Control: private
D/OkHttp: Content-Type: text/csv; charset=utf-16
D/OkHttp: Server: Microsoft-IIS/8.5
D/OkHttp: Content-Disposition: attachment; filename=Account_Summary_8978_04062018_142921.csv
D/OkHttp: X-AspNet-Version: 4.0.30319
D/OkHttp: X-Powered-By: ASP.NET
D/OkHttp: Date: Mon, 04 Jun 2018 13:29:21 GMT
D/OkHttp: Connection: keep-alive
D/OkHttp: Content-Length: 446900
D/OkHttp: Set-Cookie: xxx; path=/; Httponly; Secure
D/OkHttp: <-- END HTTP (binary 446900-byte body omitted)


D/Accounts Overview: success transactions: --------
Response{protocol=http/1.1, code=200, message=OK, url=xxx}
Sindhu
  • 421
  • 1
  • 6
  • 16

1 Answers1

3

But the response body is empty.

Retrofit determines the type of response with the return type of the methods defined in your API interface.

The return type of getBalancesAndTransactionsis Void which means nothing.

Solution : Define your customise POJO class and replace Void with `POJO

or Just for testing purpose, you can use Object which can handle any type of response but you will have to use casting for specific operation

e.g

@Streaming
@GET("common/frontend/member/download.aspx")
Call<Object> getBalancesAndTransactions(@Header("Cookie") String header);

References :

How can we handle different response type with Retrofit 2?

Using Retrofit in Android

Pavneet_Singh
  • 36,884
  • 5
  • 53
  • 68