Android - 像HTTP服务器,向浏览器发送嵌入图像
问题描述:
我为我的android应用程序实现了一个简单的HTTP服务器,它传递了带有套接字的html标记,一切都按预期进行。Android - 像HTTP服务器,向浏览器发送嵌入图像
但我试图加载在客户端(浏览器)一个简单的嵌入图像(http:// localhost:1234/img.jpg \“/>),我不知道如何使套接字加载它。 谁能帮我给的坐标,使其
我简单的HTTP服务器:?!
public class MainClass extends Activity {
// Called when the activity is first created
// It was called from onCreate method surrounded with try catch
[...]
ServerSocket ss = new ServerSocket(1234);
while (true) {
Socket s = ss.accept();
PrintStream out = new PrintStream(s.getOutputStream());
BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
String info = null;
while ((info = in.readLine()) != null) {
System.out.println("now got " + info);
if(info.equals(""))
break;
}
out.println("HTTP/1.0 200 OK");
out.println("MIME_version:1.0");
out.println("Content_Type:text/html");
String c = "<html>" +
"<head></head>" +
"<body>" +
"<img src=\"http://localhost:1234/img.jpg\" />" + // << Does not load in the browser
"<h1> hi </h1>" +
"</body>" +
"</html>";
out.println("Content_Length:" + c.length());
out.println("");
out.println(c);
out.close();
s.close();
in.close();
}
[...]
}
}
在此先感谢
答
为什么图像没有加载的原因是因为该文件http://localhost:1234/img.jpg
没有被你的应用程序服务。当一个<img />
标记被处理,浏览器将出去src路径并将该文件加载到页面中。
我不知道如何实现这个offhand(我以前没有实现HTTP)。但是您至少必须处理输入的GET
请求,并区分基础网页和图片请求。
谢谢,伙计! :) – SpecTrum 2013-02-26 20:52:02