How to make HttpServer app "com.sun.net.httpserver" with android studio?

156 Views Asked by At

How to make HttpServer app "com.sun.net.httpserver" with android studio?

I want make HttpServer that it will run from internal storage text file like- "/storage/emulated/0/htdocs/index.html"

I am trying but not working!

public void handle(HttpExchange t) throws IOException {
    // .. read the request body
    String path ="/storage/emulated/0/htdocs" + t.getRequestURI();
    String plain_text ="";
    File file = new File(path);
    
    if(file.isDirectory()){
        path = path +"index.html";
    }
    String mime = MimeTypeMap.getFileExtensionFromUrl(path);
    mime = MimeTypeMap.getSingleton().getMimeTypeFromExtension(mime);

if(mime.contains("text")){
    Scanner sc = new Scanner(file);
    
    while(sc.hasNextLine()){
        plain_text = plain_text + sc.nextLine();
    }
    if(plain_text.equals("")){
        plain_text = "Its no text file!";
    }
    t.sendResponseHeaders(200, plain_text.length());
    OutputStream os = t.getResponseBody();
    
    os.close();
    }
}
}
0

There are 0 best solutions below