3

I have read How to serve a file on sdcard using NanoHTTPD (inside Android)

The code return new NanoHTTPD.Response(Status.OK, "audio/mpeg", fis) doesn't be supported in latest NanoHTTPD 2.3.0 again.

I try to replace it with return newFixedLengthResponse(fis) , but it's incorrect. How can I do that? Thanks!

public class StackOverflowMp3Server extends NanoHTTPD {

    public StackOverflowMp3Server() {
         super(8089);
    }

    @Override
    public Response serve(String uri, Method method,
        Map<String, String> header, Map<String, String> parameters,
        Map<String, String> files) {
    String answer = "";

    FileInputStream fis = null;
    try {
        fis = new FileInputStream(Environment.getExternalStorageDirectory()
                + "/music/musicfile.mp3");
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return new NanoHTTPD.Response(Status.OK, "audio/mpeg", fis);
  }
}
Community
  • 1
  • 1
HelloCW
  • 843
  • 22
  • 125
  • 310
  • Well how does the declaration of Response look like now? And what error message do you get for the old one? – greenapps Apr 12 '16 at 06:07
  • The code return new NanoHTTPD.Response(Status.OK, "audio/mpeg", fis) can't be compiled when I use NanoHTTPD 2.3.0 – HelloCW Apr 12 '16 at 07:11
  • It is more important that you answer my first question. Why aren't you? The IDE will show you haw it has to be done. – greenapps Apr 12 '16 at 07:34
  • Would you please see https://www.dropbox.com/s/8sppvuwkwg9hscz/aa.bmp?dl=0 – HelloCW Apr 12 '16 at 07:39
  • https://www.dropbox.com/s/ev9crcg2sqgutw6/bb.PNG?dl=0 and https://www.dropbox.com/s/8gzd7xtn6sunh9m/cc.PNG?dl=0 – HelloCW Apr 12 '16 at 07:42
  • Do not place links which require login please. And it was not what i asked. – greenapps Apr 12 '16 at 07:44
  • What you should do is just type `NanoHTTPD.` and wait. The IDE (well in Eclipse but Android Studio will have something like that too) will show you the functions which are available. Probably also a different Response member. – greenapps Apr 12 '16 at 07:45
  • Thanks! but I can't understand what you mean! I only hope to fix the code return new NanoHTTPD.Response(Status.OK, "audio/mpeg", fis) in latest NanoHTTPD – HelloCW Apr 12 '16 at 08:51
  • Just do what i suggested in the editor. Type it and wait. – greenapps Apr 12 '16 at 10:03
  • Otherwise you dig into the code for NanoHTTPD:Response and look how it is declared. There will be a parameter added or changed. Thats all i think. – greenapps Apr 12 '16 at 10:05
  • I wonder that you never saw that the IDE is constantly suggesting memberfunctions as soon as you typed the dot. – greenapps Apr 12 '16 at 10:07

3 Answers3

6

You can use newChunkedResponse() method for InputStream like this:

return newChunkedResponse(Status.OK, "audio/mpeg", fis);

Anton Tananaev
  • 2,458
  • 1
  • 25
  • 48
0

This code work fine (put your sound in the assets folder)

class SoundServer extends NanoHTTPD {

    SoundServer() {
        super(8089);
    }

    @Override
    public Response serve(IHTTPSession session) {
        InputStream myInput = null;
        try {
            myInput = mContext.getAssets().open("sound.mp3");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return createResponse(Response.Status.OK, "audio/mpeg", myInput);
    }

    //Announce that the file server accepts partial content requests
    private Response createResponse(Response.Status status, String mimeType,
                                    InputStream message) {
        return newChunkedResponse(status, mimeType, message);
    }
}
Artificioo
  • 704
  • 1
  • 9
  • 19
0

This code works to serve any type files from nanohttpd webserver.

 @Override
public Response serve(IHTTPSession session) {
    String msg = "<html><body><h1>Hello server</h1></body></html>\n";
    String msgUnauthorized = "<html><body><h1>Unauthorized</h1></body></html>\n";
    Method method = session.getMethod();
    String uri = session.getUri();
    Map<String, String> files = new HashMap<>();
    Storage storage = new Storage(OpenRAP.getContext());
    String currentpath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/www/";
    String temp = Environment.getExternalStorageDirectory().getAbsolutePath() + "/www/temp/";
    String ecarpath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/www/ecars_files/";
    String xcontent = Environment.getExternalStorageDirectory().getAbsolutePath() + "/www/xcontent/";
    MainActivity.checkfiles();
    updateurl();
    String Endpoint = session.getUri();
    if (Endpoint.equals("/")) {

        String answer = "";
        try {
            // Open file from SD Card
            File root = Environment.getExternalStorageDirectory().getAbsoluteFile();
            FileReader index = new FileReader(root +
                    "/www/openrap/index.html");
            BufferedReader reader = new BufferedReader(index);
            String line = "";
            while ((line = reader.readLine()) != null) {
                answer += line;
            }
            reader.close();
        } catch (IOException ioe) {
            Log.w("Httpd", ioe.toString());
        }

        return newFixedLengthResponse(answer);
    }
    else if (uri.equals("/app-debug.apk")) {

        File root = Environment.getExternalStorageDirectory();
        FileInputStream fis = null;
        File file = new File(root.getAbsolutePath() + "/www/resources/app-debug.apk");
        String mime = NanoHTTPD.getMimeTypeForFile("app-debug.apk");
        Log.d("Path", root.getAbsolutePath());
        try {
            if (file.exists()) {
                fis = new FileInputStream(file);

            } else
                Log.d("FOF :", "File Not exists:");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }


        return newFixedLengthResponse(Response.Status.OK, mime, fis, file.length());
    }

Here i am sending apk file you can anytype of file with respective format and file name

sanjay
  • 695
  • 11
  • 22