0

私は自分のAndroidアプリケーションに、ソケット付きのhtmlタグを渡す単純な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();
}

[...]

}

}

前もって感謝します!

4

1 に答える 1

0

画像がロードされない理由は、ファイルhttp://localhost:1234/img.jpgがアプリケーションによって提供されていないためです。タグが処理される<img />と、ブラウザーは src パスに移動し、そのファイルをページに読み込みます。

そのオフハンドを実装する方法がわかりません (以前に HTTP を実装したことがありません)。ただし、少なくとも入力されたリクエストを処理しGET、ベース Web ページと画像リクエストを区別する必要があります。

于 2013-02-26T17:28:47.857 に答える