简体   繁体   English

是否可以在Android上使用凌空下载任何文件(pdf或zip)?

[英]Is it possible to download any file (pdf or zip) using volley on android?

How it is possible to download a pdf file or a zip file using android volley. 如何使用Android Volley下载pdf文件或zip文件。 It is easy to work using String, JSON, Image or Video. 使用String,JSON,图像或视频很容易工作。 What about other files? 那其他文件呢?

Firstly you have to create your own custom request class like 首先,您必须创建自己的自定义请求类,例如

class InputStreamVolleyRequest extends Request<byte[]> {
private final Response.Listener<byte[]> mListener;
private Map<String, String> mParams;

//create a static map for directly accessing headers
public Map<String, String> responseHeaders ;

public InputStreamVolleyRequest(int method, String mUrl ,Response.Listener<byte[]> listener,
                                Response.ErrorListener errorListener, HashMap<String, String> params) {
  // TODO Auto-generated constructor stub

    super(method, mUrl, errorListener);
     // this request would never use cache.
     setShouldCache(false);
     mListener = listener;
     mParams=params;
}

@Override
protected Map<String, String> getParams()
     throws com.android.volley.AuthFailureError {
  return mParams;
};


@Override
protected void deliverResponse(byte[] response) {
    mListener.onResponse(response);
}

@Override
protected Response<byte[]> parseNetworkResponse(NetworkResponse response) {

    //Initialise local responseHeaders map with response headers received
    responseHeaders = response.headers;

    //Pass the response data here
    return Response.success( response.data, HttpHeaderParser.parseCacheHeaders(response));
}
}

Now just send request though our custom class with Request.Method.GET and the url from where you want to doenload the file. 现在,通过带有Request.Method.GET的自定义类和要从其重新加载文件的URL发送请求。

String mUrl= <YOUR_URL>;
InputStreamVolleyRequest request = new     InputStreamVolleyRequest(Request.Method.GET, mUrl,
    new Response.Listener<byte[]>() { 
         @Override 
         public void onResponse(byte[] response) { 
       // TODO handle the response 
        try { 
        if (response!=null) {

          FileOutputStream outputStream;
          String name=<FILE_NAME_WITH_EXTENSION e.g reference.txt>;
            outputStream = openFileOutput(name, Context.MODE_PRIVATE);
            outputStream.write(response);
            outputStream.close();
            Toast.makeText(this, "Download complete.", Toast.LENGTH_LONG).show();
        }
    } catch (Exception e) {
        // TODO Auto-generated catch block
        Log.d("KEY_ERROR", "UNABLE TO DOWNLOAD FILE");
        e.printStackTrace();
    }
  }
} ,new Response.ErrorListener() {

@Override
public void onErrorResponse(VolleyError error) {
// TODO handle the error
error.printStackTrace();
  }
}, null);
      RequestQueue mRequestQueue = Volley.newRequestQueue(getApplicationContext(), new HurlStack());
      mRequestQueue.add(request);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM