改造 - 从android中的服务器下载csv文件

时间:2018-06-04 13:55:28

标签: android retrofit2

我正在尝试使用改造从服务器下载文件。使用HttpLoggingInterceptor我尝试记录正在发生的事情。我可以找到文件名。但是响应机构是空的。

我是新手使用改造。有人可以指出我在哪里出错了正确的方向

改造客户端界面:

public interface DownloadTransactions {

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

}

Java调用:

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: --------");
        }
    });


}

日志响应:

<-- 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}

1 个答案:

答案 0 :(得分:1)

  

但是响应主体是空的。

Retrofit使用API​​接口中定义的方法的返回类型确定响应类型。

getBalancesAndTransactions的返回类型为Void,这意味着什么。

解决方案:定义自定义POJO类并将Void替换为“POJO

或仅用于测试目的,您可以使用Object来处理任何类型的响应,但您必须使用转换来进行特定操作

例如

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

参考文献:

How can we handle different response type with Retrofit 2?

Using Retrofit in Android