gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 23:53:09 35 4
gpt4 key购买 nike

我正在尝试使用改造从服务器下载文件。使用 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}

最佳答案

But the response body is empty.

Retrofit 使用您的 API 接口(interface)中定义的方法的返回类型来确定响应类型。

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

关于android - 改造 - 从 android 中的服务器下载 csv 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50682292/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com