Question

I've created http service on android. Now I'm trying to make HttpService show me some image into a browser. I write an url in the browser (for example http://127.0.0.1:6789/home.html (I play with emulator)). And the http service sends me html like below:

<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 code from server side is:

/*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);

or another way

@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);
}

I've tried both way, but there aren't any images anyway! What's wrong with it? How can I use for example JavaScript's files? Thanks.

Was it helpful?

Solution

It works perfect! My mistake was I wrote:

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

But it returns blank page, because I missed http://.

I should write like below:

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

Thanks.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top