我正在尝试使用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),但问题是这个方法可以用于单个输出流,所以我只能一次写一个文件。
希望您了解我的问题。我只需要加载页面内容图片或类似媒体......
答案 0 :(得分:0)
你需要听每个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);
}
}
希望这会有所帮助