0

I am trying to to send a video as response to my android application. here is the code for both server side (REST call) and client side:

Server side code:

    return Response.ok().entity(new FileInputStream(new File(fileName))).build();

here 'fileName' is the path of the video on my server.

Client side code in my android app:

@Override
protected File doInBackground(Void... params) {
    String address = <url of the rest service>;
    HttpGet httpGet = new HttpGet(address);
    StringBuilder stringBuilder = new StringBuilder();
    HttpClient client = new DefaultHttpClient();
    HttpResponse response;
    stringBuilder = new StringBuilder();
     File myVideo=null;
    try {
        response = client.execute(httpGet);
        HttpEntity entity = response.getEntity();
        InputStream stream = entity.getContent();
        int b;
        videoRoot.mkdirs();
         myVideo = new File(videoRoot, "myVideo.3gp");
        FileOutputStream fOut = new FileOutputStream(myVideo);
        OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);
        while ((b = stream.read()) != -1) {
            stringBuilder.append((char) b);
            myOutWriter.append((char) b);
        }
        Log.i("VideoResponse", stringBuilder.toString());

        myOutWriter.close();
        fOut.close();

    } catch (ClientProtocolException e) {

        e.printStackTrace();
    } catch (IOException e) {

        e.printStackTrace();
    }

    return myVideo;
}

FYI- the video plays fine when i run it on server . but on the android device it gives me following error code "video cant be played". Also I matched the checksum of both video and both were different. Also , when i copied the generated video from android device to my computer, it was corrupted .So I have figured out that I my client side code is wrong. can anyone please help me with this.

Puneetr90
  • 189
  • 1
  • 6
  • 18

0 Answers0