我为我的 android 应用程序实现了一个简单的 HTTP 服务器,它通过套接字传递 html 标记,一切都按预期进行。
但我尝试在客户端(浏览器)中加载一个简单的嵌入图像(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();
}
[...]
}
}
提前致谢!