如何从Web Api获取pdf响应到Android?
我创建了一个返回pdf文件流的Web Api服务。如何在android中获取此响应并显示为pdf文件?下面给出了我的Web Api代码如何从Web Api获取pdf响应到Android?,android,c#-4.0,asp.net-web-api2,Android,C# 4.0,Asp.net Web Api2,我创建了一个返回pdf文件流的Web Api服务。如何在android中获取此响应并显示为pdf文件?下面给出了我的Web Api代码 [Route("api/GetPdf")] public HttpResponseMessage GetPdf(string PdfName) { DataAccess objClsData = new DataAccess(); SqlDataReader _SqlReader; _SqlRead
[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 ", "");
}
});
new reformation.Builder()
.baseUrl(apiUrl)
.addConverterFactory(GsonConverterFactory.create())
.build()
.create(webapident.class)。
DownloadFileWithDynamicCurlsync(“文件\ Url”)//更改文件名
.enqueue(新的改型.Callback(){
@凌驾
公共响应(改装。响应,改装)
{
if(response!=null&&response.body()!=null){
//response.body()将包含下载的文件详细信息
}
@凌驾
失效时的公共无效(可丢弃的t){
Log.e(“未能下载文件”,“下载失败”);
}
});
webapidentpoint.class
@GET(“GetPdf”)
调用downloadfilewithdynamiccurlsync(@Query(“PdfName”)字符串PdfName);
@GET("GetPdf")
Call<ResponseBody> downloadFileWithDynamicUrlSync(@Query("PdfName") String PdfName);