0

我为我的 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();
}

[...]

}

}

提前致谢!

4

1 回答 1

0

图像未加载的原因http://localhost:1234/img.jpg是您的应用程序未提供该文件。当一个<img />标签被处理时,浏览器将转到 src 路径并将该文件加载到页面中。

我不知道如何临时实现(我之前没有实现过 HTTP)。但是您至少必须处理输入的GET请求,并区分基本网页和图像请求。

于 2013-02-26T17:28:47.857 回答