带有媒体 HttpRequestHandler 的 Http 响应 Android

Http Response with media HttpRequestHandler Android

我正在尝试使用 apache http

在 android 设备中创建 http 服务器

这是我的主题

public class RegisterThread extends Thread {


private boolean isRunning = false;

private BasicHttpProcessor httpproc = null;
private BasicHttpContext httpContext = null;
private HttpService httpService = null;
private HttpRequestHandlerRegistry registry = null;

public RegisterThread(Context context) {
    super(Constants.SERVER_NAME);

    httpproc = new BasicHttpProcessor();
    httpContext = new BasicHttpContext();
    httpproc.addInterceptor(new ResponseDate());
    httpproc.addInterceptor(new ResponseServer());
    httpproc.addInterceptor(new ResponseContent());
    httpproc.addInterceptor(new ResponseConnControl());
    httpService = new HttpService(httpproc,
            new DefaultConnectionReuseStrategy(),
            new DefaultHttpResponseFactory());
    registry = new HttpRequestHandlerRegistry();
    registry.register(Constants.ALL_PATTERN, new ResponseHandler(context));
    httpService.setHandlerResolver(registry);
}

@Override
public void run() {
    super.run();

    try {
        ServerSocket serverSocket = new ServerSocket(Constants.SERVER_PORT);

        serverSocket.setReuseAddress(true);

        while (isRunning) {
            try {
                final Socket socket = serverSocket.accept();

                DefaultHttpServerConnection serverConnection = new DefaultHttpServerConnection();

                serverConnection.bind(socket, new BasicHttpParams());

                httpService.handleRequest(serverConnection, httpContext);

                serverConnection.shutdown();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (HttpException e) {
                e.printStackTrace();
            }   

        }

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

public synchronized void startThread() {
    isRunning = true;
    super.start();
}

public synchronized void stopThread() {
    isRunning = false;
}

}

这里是 Responsehandler

public class ResponseHandler  implements HttpRequestHandler {
private Context context;
public ResponseHandler(Context context) {
    this.context = context;
}

@Override
public void handle(HttpRequest request, HttpResponse response,
        HttpContext httpContext) throws HttpException, IOException {

    HttpEntity entity = new EntityTemplate(new ContentProducer() {
        public void writeTo(final OutputStream outstream)
                throws IOException {
            OutputStreamWriter writer = new OutputStreamWriter(outstream,
                    "UTF-8");
            String resp = Utility.openHTMLString(context, R.raw.home);
            writer.write(resp);
            writer.flush();
        }
    });
    response.setHeader("Content-Type", "text/html");
    response.setEntity(entity);

}

}

此代码也可以将 http 响应内容类型处理为 html 形式,并且适用于非媒体 html 内容。

我的问题是如何处理此处的媒体内容(例如图片)。 我的 html 页面包含一些图片标签。我已经将图像放入其中 (R.raw.mypic),但问题是此方法可用于单个输出流,因此我一次只能写入一个文件。

希望您理解我的 problem.I 只需要加载内容图像或类似媒体的页面....

您需要监听每个 html 个元素(例如图像)

这是示例

String uriString = request.getRequestLine().getUri();
    Uri uri = Uri.parse(uriString);
    if (uri != null) {
        if (uri.toString().contains("mypic.jpg")) {
            HttpEntity entity = new EntityTemplate(new ContentProducer() {
                public void writeTo(final OutputStream outstream)
                        throws IOException {
                    InputStream is = context.getResources()
                            .openRawResource(R.raw.mypic);
                    copyStream(is, outstream);
                }
            });
            response.setHeader("Content-Type", "image/jpg");
            response.setEntity(entity);
        }
        else{
            HttpEntity entity = new EntityTemplate(new ContentProducer() {
                public void writeTo(final OutputStream outstream)
                        throws IOException {
                    InputStream is = context.getResources().openRawResource(
                            R.raw.home);
                    copyStream(is, outstream);
                }
            });
            response.setHeader("Content-Type", "text/html");
            response.setEntity(entity);
        }
    }

希望这会有所帮助