Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.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
Spring mvc 如果我有一个返回字节[]的spring mvc rest控制器,我将如何使用volley下载到我的android应用程序?_Spring Mvc_Android Volley_Networkimageview - Fatal编程技术网

Spring mvc 如果我有一个返回字节[]的spring mvc rest控制器,我将如何使用volley下载到我的android应用程序?

Spring mvc 如果我有一个返回字节[]的spring mvc rest控制器,我将如何使用volley下载到我的android应用程序?,spring-mvc,android-volley,networkimageview,Spring Mvc,Android Volley,Networkimageview,我需要实现一个包含缩略图的列表视图,这个缩略图是使用volley networkimageview加载的。如果我的控制器如下所示,我将如何实现此功能: @RequestMapping(value=“/rest/getphoto/”,products=MediaType.IMAGE\u PNG\u value) public@ResponseBody字节[]获取图像(@RequestParam(“imageId”) 我发现了很多关于截击的例子,但它们对我没有帮助。此外,我正在使用安全连接。提前谢谢

我需要实现一个包含缩略图的列表视图,这个缩略图是使用volley networkimageview加载的。如果我的控制器如下所示,我将如何实现此功能:

@RequestMapping(value=“/rest/getphoto/”,products=MediaType.IMAGE\u PNG\u value)
public@ResponseBody字节[]获取图像(@RequestParam(“imageId”)

我发现了很多关于截击的例子,但它们对我没有帮助。此外,我正在使用安全连接。提前谢谢

编辑:我在我的SpringMVC项目中包含控制器代码,在我的android客户端中包含请求图像的部分代码

*春季MVC*

@RequestMapping(value = "/rest/singlephoto/", method = RequestMethod.GET, produces = MediaType.IMAGE_PNG_VALUE)
    public @ResponseBody byte[] base64ImageForAndroid(@RequestParam("photoId") String photoIdParam, HttpServletRequest request)
    {
        String pathToLoad = "/path/default.png";
        //HashMap<String, String> retVal = new HashMap<String, String>();
        byte[] retVal;

        try
        {
            long photoId = Long.parseLong(photoIdParam);
            Photo photo = photoManager.getSinglePhoto(photoId);

            if (photo != null)
                pathToLoad = photo.getPath();
        }
        catch (NumberFormatException ex)
        {
        }
        finally
        {
            try
            {
                File file = new File(pathToLoad);
                retVal = FileUtils.readFileToByteArray(file);
            }
            catch (IOException ex)
            {
                retVal = null;
            }
        }

        return retVal;
Bitmap thumb = imageCache.get(item.getThumbnailUrl() + "thumb");
        if (thumb == null)
        {
            HttpHeaders headers = new HttpHeaders();
            HttpBasicAuthentication auth = new HttpBasicAuthentication(this.username, this.password);
            headers.setAuthorization(auth);
            headers.setAccept(Collections.singletonList(MediaType.APPLICATION_OCTET_STREAM));

            Listener<byte[]> imageLoadedListener = new Response.Listener<byte[]>() {

                @Override
                public void onResponse(byte[] photoByteArray) {
                    Bitmap bitmap = EfficientImageLoading.decodeBitmapFromByteArray(photoByteArray, viewHolder.thumbnail.getWidth(), viewHolder.thumbnail.getHeight());

                    viewHolder.thumbnail.setImageBitmap(bitmap);
                    imageCache.put(item.getThumbnailUrl() + "thumb", bitmap);

                    //Cache full size and recycle
                    Bitmap fullBmp = EfficientImageLoading.decodeImageFromByteFullSize(photoByteArray);
                    imageCache.put(item.getThumbnailUrl(), fullBmp);
                }
            };

            ErrorListener errorListener = new Response.ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError error) {
                    viewHolder.thumbnail.setImageResource(R.drawable.ic_launcher);
                }
            };

            this.singleInstance.addToRequestQueue(new CustomImageRequest(Request.Method.GET, item.getThumbnailUrl(), errorListener, headers, imageLoadedListener));
}
@RequestMapping(value=“/rest/singlephoto/”,method=RequestMethod.GET,products=MediaType.IMAGE\u PNG\u value)
public@ResponseBody byte[]base64ImageForAndroid(@RequestParam(“photoId”)字符串photoIdParam,HttpServletRequest)
{
字符串pathtoad=“/path/default.png”;
//HashMap retVal=新建HashMap();
字节[]返回;
尝试
{
长photoId=long.parseLong(photoIdParam);
Photo Photo=photoManager.getSinglePhoto(photoId);
如果(照片!=null)
pathToLoad=photo.getPath();
}
捕获(NumberFormatException ex)
{
}
最后
{
尝试
{
文件文件=新文件(路径加载);
retVal=FileUtils.readFileToByteArray(文件);
}
捕获(IOEX异常)
{
retVal=null;
}
}
返回返回;
*Android客户端通过截击请求*

@RequestMapping(value = "/rest/singlephoto/", method = RequestMethod.GET, produces = MediaType.IMAGE_PNG_VALUE)
    public @ResponseBody byte[] base64ImageForAndroid(@RequestParam("photoId") String photoIdParam, HttpServletRequest request)
    {
        String pathToLoad = "/path/default.png";
        //HashMap<String, String> retVal = new HashMap<String, String>();
        byte[] retVal;

        try
        {
            long photoId = Long.parseLong(photoIdParam);
            Photo photo = photoManager.getSinglePhoto(photoId);

            if (photo != null)
                pathToLoad = photo.getPath();
        }
        catch (NumberFormatException ex)
        {
        }
        finally
        {
            try
            {
                File file = new File(pathToLoad);
                retVal = FileUtils.readFileToByteArray(file);
            }
            catch (IOException ex)
            {
                retVal = null;
            }
        }

        return retVal;
Bitmap thumb = imageCache.get(item.getThumbnailUrl() + "thumb");
        if (thumb == null)
        {
            HttpHeaders headers = new HttpHeaders();
            HttpBasicAuthentication auth = new HttpBasicAuthentication(this.username, this.password);
            headers.setAuthorization(auth);
            headers.setAccept(Collections.singletonList(MediaType.APPLICATION_OCTET_STREAM));

            Listener<byte[]> imageLoadedListener = new Response.Listener<byte[]>() {

                @Override
                public void onResponse(byte[] photoByteArray) {
                    Bitmap bitmap = EfficientImageLoading.decodeBitmapFromByteArray(photoByteArray, viewHolder.thumbnail.getWidth(), viewHolder.thumbnail.getHeight());

                    viewHolder.thumbnail.setImageBitmap(bitmap);
                    imageCache.put(item.getThumbnailUrl() + "thumb", bitmap);

                    //Cache full size and recycle
                    Bitmap fullBmp = EfficientImageLoading.decodeImageFromByteFullSize(photoByteArray);
                    imageCache.put(item.getThumbnailUrl(), fullBmp);
                }
            };

            ErrorListener errorListener = new Response.ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError error) {
                    viewHolder.thumbnail.setImageResource(R.drawable.ic_launcher);
                }
            };

            this.singleInstance.addToRequestQueue(new CustomImageRequest(Request.Method.GET, item.getThumbnailUrl(), errorListener, headers, imageLoadedListener));
}
Bitmap thumb=imageCache.get(item.getThumbnailUrl()+“thumb”);
if(thumb==null)
{
HttpHeaders=新的HttpHeaders();
HttpBasicAuthentication auth=新的HttpBasicAuthentication(this.username,this.password);
headers.setAuthorization(auth);
setAccept(Collections.singletonList(MediaType.APPLICATION_OCTET_STREAM));
Listener imageLoadedListener=new Response.Listener(){
@凌驾
公共void onResponse(字节[]photoByteArray){
位图Bitmap=EfficientImageLoading.decodeBitmapFromByteArray(photoByteArray,viewHolder.thumbnail.getWidth(),viewHolder.thumbnail.getHeight());
viewHolder.thumboil.setImageBitmap(位图);
imageCache.put(item.getThumbnailUrl()+“thumb”,位图);
//缓存完整大小和回收
位图fullBmp=有效图像加载。按完整大小解码图像(photoByteArray);
imageCache.put(item.getThumbnailUrl(),fullBmp);
}
};
ErrorListener ErrorListener=新响应。ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
viewHolder.thumbnail.setImageResource(R.drawable.ic_启动器);
}
};
this.singleInstance.addToRequestQueue(新的CustomImageRequest(Request.Method.GET,item.getThumbnailUrl(),errorListener,headers,imageLoadedListener));
}
应用程序服务器日志显示:

GET/app//photo/rest/singlephoto/?photoId=7 HTTP/1.1“406 1067

这是406——禁止或类似的东西。另外,android的LogCat显示了如下错误:BasicNetwork.PerformRequest:的意外响应代码406


我的控制器或客户机或两者都有问题吗?

我以前从未听说过volley,但我可以使用google查找文档。以下文档解释了如何请求图像:。与
https://your.host/rest/getphoto?imageId=42
作为获取带有42的图像的URL,感谢您的回复ply@JB Nizet。正如我所说,我已经看到了许多示例,但它们并没有真正的帮助。例如,我需要在通过SSL请求映像之前设置授权标头。设置标头后,我想知道如何处理响应,因为rest控制器没有返回json。如何处理响应在我发布的链接中进行了解释。请阅读如果你的问题是“如何向截击请求添加标题?”,那么你为什么不在问题中说一句话呢?ImageRequest有一个getHeader()方法返回标题的映射。我想您可以将标题添加到该映射。因为我希望看到整个图片。正如您所看到的,将图像下载到我的应用程序中涉及到许多部分,因此我希望这里的人能够提供一个链接,引导我找到正确的解决方案。我感谢您的支持但是我想看看工作实例。StackOverflow不是为了寻找工作实例,而是为了提出具体问题。