我正在开发一个需要从api保存pdf文件的Android应用程序。所以我不得不将Request volley类扩展为ByteArray类:
"Apples are good for health" , "I live in America" , "I love Mangoes and Apples and Strawberries." , "Mangoes and Apples and Honey"
我的Activity我正在调用ByteArrayRequest来执行api请求:
private void getCarnePDF(final int empreendimento,final int coligada,final String quadra,final String lote){ RequestQueue requestQueue; ByteArrayRequest请求;
package br.com.tarcisojunior.myapp;
import android.support.annotation.NonNull;
import com.android.volley.NetworkResponse;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.toolbox.HttpHeaderParser;
/**
* Created by tarcisojunior on 18/04/18.
*/
public class ByteArrayRequest extends Request<byte[]> {
private final Response.Listener<byte[]> mListener;
public ByteArrayRequest(String url, Response.Listener<byte[]> listener,
Response.ErrorListener errorListener) {
this(Method.GET, url, listener, errorListener);
}
public ByteArrayRequest(int method, String url, Response.Listener<byte[]> listener, Response.ErrorListener errorListener) {
super(method, url, errorListener);
mListener = listener;
}
@Override
protected Response parseNetworkResponse(NetworkResponse response) {
return Response.success(response.data, HttpHeaderParser.parseCacheHeaders(response));
}
@Override
protected void deliverResponse(byte[] response) {
if(null != mListener){
mListener.onResponse(response);
}
}
@Override
public String getBodyContentType() {
return "application/octet-stream";
}
}
}
并且在监听器中saveToFile函数应该创建pdf文件。已创建pdf文件,但引发错误“无法打开文件”。这是我的saveToFile函数:
requestQueue = Volley.newRequestQueue(this);
request = new ByteArrayRequest(Request.Method.GET, getString(R.string.baseUrl) + getString(R.string.carnePdfUrl),
new Response.Listener<byte[]>() {
@Override
public void onResponse(byte[] response) {
Log.i("getBilletCard", response.toString());
try {
byte[] bytes = response;
saveToFile(bytes, "card.pdf");
}catch (Exception e){
Toast.makeText(BilletCardActivity.this, "Erro ao converter resposta", Toast.LENGTH_LONG).show();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
}){
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
//headers.put("Content-Type", "application/json");
headers.put("token", token);
headers.put("empreendimento", String.valueOf(empreendimento));
headers.put("coligada", String.valueOf(coligada));
headers.put("quadra", quadra);
headers.put("lote",lote);
return headers;
}
};
requestQueue.add(request);
答案 0 :(得分:1)
在将Postman标头与我的代码进行比较后,我发现请求中缺少“Cache-control = no-cache”标头。
添加此标题后,文件已正确下载。
所以改为: 。 。 。 。
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Cache-Control", "no-cache");
headers.put("token", token);
headers.put("empreendimento", String.valueOf(empreendimento));
headers.put("coligada", String.valueOf(coligada));
headers.put("quadra", quadra);
headers.put("lote",lote);
return headers;
}
。 。 。 。