Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android-类似套接字的http服务器,将嵌入图像发送到浏览器_Java_Android_Httpserver - Fatal编程技术网

Java Android-类似套接字的http服务器,将嵌入图像发送到浏览器

Java Android-类似套接字的http服务器,将嵌入图像发送到浏览器,java,android,httpserver,Java,Android,Httpserver,我为我的android应用程序实现了一个简单的HTTP服务器,它通过套接字传递html标记,一切都按预期进行 但我尝试在客户端(浏览器)加载一个简单的嵌入图像(http://localhost:1234/img.jpg\“/>)我不知道如何让套接字加载它。 有人能帮我提供坐标吗 我的简单http服务器: public class MainClass extends Activity { // Called when the activity is first created // It

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

[...]

}

}
公共类MainClass扩展活动{
//首次创建活动时调用
//它是从包含try-catch的onCreate方法调用的
[...]
ServerSocket ss=新的ServerSocket(1234);
while(true){
套接字s=ss.accept();
PrintStream out=新的PrintStream(s.getOutputStream());
BufferedReader in=新的BufferedReader(新的InputStreamReader(s.getInputStream());
字符串信息=null;
而((info=in.readLine())!=null){
System.out.println(“现在获得”+信息);
如果(信息等于(“”)
打破
}
out.println(“HTTP/1.0 200 OK”);
out.println(“MIME_版本:1.0”);
println(“内容类型:text/html”);
字符串c=“”+
"" + 
"" + 

“”+/图像未加载的原因是文件
http://localhost:1234/img.jpg
未由您的应用程序提供服务。当

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