如何从 Web Api 获取 pdf 响应到 Android?
How to fetch pdf response from Web Api to Android?
我创建了一个 Web Api 服务,其中 returns 一个 pdf 文件流。如何在 android 中获取此响应并显示为 pdf 文件?我的网站 Api 代码如下
[Route("api/GetPdf")]
public HttpResponseMessage GetPdf(string PdfName)
{
DataAccess objClsData = new DataAccess();
SqlDataReader _SqlReader;
_SqlReader = objClsData.ExecuteQuery("select VALUE FROM [Param] where subkey='imagepath'");
if (_SqlReader.HasRows)
{
if (_SqlReader.Read())
{
string FileName = WebUtility.UrlDecode(PdfName);
string PdfFile = _SqlReader["VALUE"].ToString() + "\" + FileName + ".pdf";
//string PdfFile = _SqlReader["VALUE"].ToString() + "\" + "bill##8000006" + ".pdf";
HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
FileStream fileStream = File.OpenRead(PdfFile);
response.Content = new StreamContent(fileStream);
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
return response;
}
}
HttpResponseMessage responseError = new HttpResponseMessage(HttpStatusCode.NotFound);
return responseError;
}
检查下面的代码片段
new Retrofit.Builder()
.baseUrl(apiUrl)
.addConverterFactory(GsonConverterFactory.create())
.build()
.create(WebApiEndPoint.class).
downloadFileWithDynamicUrlSync("File_Url")//Change to your file name
.enqueue(new retrofit.Callback<ResponseBody>() {
@Override
public void onResponse(retrofit.Response<ResponseBody> response, Retrofit retrofit)
{
if (response != null && response.body() != null) {
//response.body() will contains the downloaded file details
}
@Override
public void onFailure(Throwable t) {
Log.e("Failed to Download file ", "");
}
});
WebApiEndPoint.class
@GET("GetPdf")
Call<ResponseBody> downloadFileWithDynamicUrlSync(@Query("PdfName") String PdfName);
我创建了一个 Web Api 服务,其中 returns 一个 pdf 文件流。如何在 android 中获取此响应并显示为 pdf 文件?我的网站 Api 代码如下
[Route("api/GetPdf")]
public HttpResponseMessage GetPdf(string PdfName)
{
DataAccess objClsData = new DataAccess();
SqlDataReader _SqlReader;
_SqlReader = objClsData.ExecuteQuery("select VALUE FROM [Param] where subkey='imagepath'");
if (_SqlReader.HasRows)
{
if (_SqlReader.Read())
{
string FileName = WebUtility.UrlDecode(PdfName);
string PdfFile = _SqlReader["VALUE"].ToString() + "\" + FileName + ".pdf";
//string PdfFile = _SqlReader["VALUE"].ToString() + "\" + "bill##8000006" + ".pdf";
HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
FileStream fileStream = File.OpenRead(PdfFile);
response.Content = new StreamContent(fileStream);
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
return response;
}
}
HttpResponseMessage responseError = new HttpResponseMessage(HttpStatusCode.NotFound);
return responseError;
}
检查下面的代码片段
new Retrofit.Builder()
.baseUrl(apiUrl)
.addConverterFactory(GsonConverterFactory.create())
.build()
.create(WebApiEndPoint.class).
downloadFileWithDynamicUrlSync("File_Url")//Change to your file name
.enqueue(new retrofit.Callback<ResponseBody>() {
@Override
public void onResponse(retrofit.Response<ResponseBody> response, Retrofit retrofit)
{
if (response != null && response.body() != null) {
//response.body() will contains the downloaded file details
}
@Override
public void onFailure(Throwable t) {
Log.e("Failed to Download file ", "");
}
});
WebApiEndPoint.class
@GET("GetPdf")
Call<ResponseBody> downloadFileWithDynamicUrlSync(@Query("PdfName") String PdfName);