加载应用程序

时间:2015-09-29 16:00:29

标签: javascript java android webserver

在我的应用中,我创建了托管网络应用的网络服务器。 Web应用程序的所有文件都放在assets文件夹中。

现在,我通过运行我的应用程序然后从crome浏览器启动Web服务器,我尝试通过调用index.html文件来运行我的Web应用程序。页面的html,css部分正在正确加载,但图像没有加载到页面中:

这是我的HttpRequestHandlerCode:

public class HomePageHandler implements HttpRequestHandler {
    private Context context = null;

    private static final Map<String, String> mimeTypes = new HashMap<String, String>() {

        {
            put("css", "text/css");
            put("htm", "text/html");
            put("html", "text/html");
            put("xhtml", "text/xhtml");
            put("xml", "text/xml");
            put("java", "text/x-java-source, text/java");
            put("md", "text/plain");
            put("txt", "text/plain");
            put("asc", "text/plain");
            put("gif", "image/gif");
            put("jpg", "image/jpeg");
            put("jpeg", "image/jpeg");
            put("png", "image/png");
            put("svg", "image/svg+xml");
            put("mp3", "audio/mpeg");
            put("m3u", "audio/mpeg-url");
            put("mp4", "video/mp4");
            put("ogv", "video/ogg");
            put("flv", "video/x-flv");
            put("mov", "video/quicktime");
            put("swf", "application/x-shockwave-flash");
            put("js", "application/javascript");
            put("pdf", "application/pdf");
            put("doc", "application/msword");
            put("ogg", "application/x-ogg");
            put("zip", "application/octet-stream");
            put("exe", "application/octet-stream");
            put("class", "application/octet-stream");
            put("m3u8", "application/vnd.apple.mpegurl");
            put("ts", " video/mp2t");
        }
    };

    public HomePageHandler(Context context){
        this.context = context;
    }

    @Override
    public void handle(HttpRequest request, HttpResponse response, HttpContext httpContext) throws HttpException, IOException {
        //String contentType = "text/html";
        //Log.i("Sushill", "..request : " + request.getRequestLine().getUri().toString());
        final String requestUri = request.getRequestLine().getUri().toString();
        final String contentType = contentType(requestUri);
                        String resp = Utility.openHTMLStringFromAssets(context, "html" + requestUri);
                        writer.write(resp);
                        writer.flush();
//                    }
                }
            });
            ((EntityTemplate) entity).setContentType(contentType);
            response.setEntity(entity);
        }
    }

    /**
     * Get content type
     *
     * @param fileName
     *            The file
     * @return Content type
     */
    private String contentType(String fileName) {

        String ext = "";
        int idx = fileName.lastIndexOf(".");
        if (idx >= 0) {
            ext = fileName.substring(idx + 1);
        }
        if (mimeTypes.containsKey(ext)) {
            //Log.i("Sushill", "...ext : " + ext);
            return mimeTypes.get(ext);
        }
        else
            return "application/octet-stream";
    }

为了处理图像,我尝试了这个,但它不起作用:

if(contentType.contains("image")) {
                    InputStream is = Utility.openImageFromAssets(context, "html" + requestUri);
                    char[] buffer = new char[1024];
                    try {
                        Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
                        int n;
                        while ((n = reader.read(buffer)) != -1) {
                            writer.write(buffer, 0, n);
                        }
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    } finally {
                        try {
                            is.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }

有人可以帮我解决如何在浏览器中加载图像的问题。

感谢您的帮助

1 个答案:

答案 0 :(得分:1)

不要使用BufferedReader(新的InputStreamReader',所以你也不再使用UTF-8。只使用InputStream'是'。不要使用编写器。你没有显示'作家'是什么但是要废除它。使用http连接的OutputStream。将缓冲区和循环保存在缓冲区中并从缓冲区写入。