2012-03-11 14 views
1

私はアンドロイドでhttpサービスを作成しました。今、私はHttpServiceにブラウザにいくつかの画像を表示させようとしています。私はブラウザにURLを書いています(例えば、http://127.0.0.1:6789/home.html(私はエミュレータで遊んでいます))。そして、HTTPサービスは、以下のようなHTMLに私を送信します。ブラウザのリクエストandroid HttpService to image

<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252"> 
<title>File Upload</title> 
</head> 
<body> 
<td><img src='127.0.0.1:6789/1.png'/></td><br> 
<td><img src='127.0.0.1:6789/2.png'/></td><br> 
</body> 

サーバー側からのいくつかのコードは次のとおりです。

/*some variables*/  
private static final String IMAGE_PATTERN = "/*.png"; 
/*some code*/ 
registry.register(IMAGE_PATTERN, new ImageCommandHandler(context)); 

ImageCommandHandler:

@Override 
public void handle(HttpRequest request, HttpResponse response, 
    HttpContext httpContext) throws HttpException, IOException {   
    final InputStream is = GetInpuStreamFromResource(getContext(), 
      R.drawable.back); 

    HttpEntity entity = new EntityTemplate(new ContentProducer() { 
     public void writeTo(final OutputStream outstream) throws IOException { 
      int bufSize = 0; 
      byte[] buf = new byte[32768]; 
      while(-1!=(bufSize=is.read(buf))){ 
       outstream.write(buf,0,bufSize); 
      } 
      outstream.flush(); 
      outstream.close(); 
      is.close(); 
     } 
    }); 
    response.setHeader("Content-Type", "image/*");  
    response.setEntity(entity); 

または別の方法

@Override 
public void handle(HttpRequest request, HttpResponse response, 
    HttpContext httpContext) throws HttpException, IOException { 

    final File f= new File("/sdcard/Tulips.jpg");  
    String contentType = URLConnection.guessContentTypeFromName(f.getAbsolutePath()); 
    FileEntity entity = new FileEntity(f, contentType);  
    response.setHeader("Content-Type", contentType); 
    response.setEntity(entity); 
} 

私は両方の方法を試みたが、そこにはとにかく、どんなイメージでもない!どうしたの?たとえば、JavaScriptのファイルをどのように使用できますか?おかげさまで

+0

見てくださいhttp://stackoverflow.com/questions/2935946/sending-images-using-http-post –

答えて

0

これは完璧に機能します。私のミスは私が書いた:

<img src='127.0.0.1:6789/2.png'/> 

しかし、私はHTTPを逃したので、それは、空白のページを返します: //。私は以下のように書くべき

<img src='http://127.0.0.1:6789/2.png'/> 

感謝。