gpt4 book ai didi

java - Android 媒体 HttpRequestHandler 的 Http 响应

转载 作者:太空宇宙 更新时间:2023-11-04 14:05:06 25 4
gpt4 key购买 nike

我正在尝试使用 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;
}

}

这是响应处理程序

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) 中,但问题是此方法可用于单个输出流,因此我一次只能写入一个文件。

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

最佳答案

您需要监听每个 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);
}
}

希望这会有所帮助

关于java - Android 媒体 HttpRequestHandler 的 Http 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28937785/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com