download a file which is coming from a rest webservice in Android app

182 views Asked by At

How to receive the file coming from web service in Android?

Web Service code :

private static final String FILE_PATH = "D:\\visitorHistory.csv";

@GET
@Path("/exportCsv")
@Produces("application/octet-stream")
public Response downloadFile() {
  File file = new File(FILE_PATH);
  ResponseBuilder response = Response.ok((Object) file);
  response.header("Content-Disposition", "attachment; filename=visitorHistory.csv");
  return response.build();
}

Android code:

AsyncHttpClient client = new AsyncHttpClient();
client.getHttpClient().getParams().setParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS, true);
client.get(restServiceConstants.EXPORTCSV , null, new AsyncHttpResponseHandler() {

  @Override
  public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {

    super.onSuccess(arg0, arg1, arg2);
    Toast.makeText(getBaseContext(), "on sucess"+arg2.length, 100).show();
    for (int i = 0; i < arg2.length; i++) {
      System.out.println(arg2[i]);              
      Toast.makeText(getBaseContext(), "on sucess"+arg1[0], 100).show();
    }

  }
  @Override
  public void onFailure(int arg0, Header[] arg1, byte[] arg2, Throwable arg3){
    // TODO Auto-generated method stub
    super.onFailure(arg0, arg1, arg2, arg3);
  }
});
0

There are 0 answers