Uploading image to RESTful webservice through android giving NoSuchMethodError

772 views Asked by At

My current app is taking a picture. Once the picture has been taken, it should automatically send the picture to my webservice so i can see it being saved on my computer, just for a starter.

My current code for the android part is as follows:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE_CONTENT_RESOLVER) {
        if (resultCode == RESULT_OK) {
            String[] projection = { MediaStore.MediaColumns._ID,
                    MediaStore.Images.ImageColumns.ORIENTATION,
                    MediaStore.Images.Media.DATA };
            Cursor cursor = getContentResolver().query(imageFilePath,
                    projection, null, null, null);
            cursor.moveToFirst();
            imageFileName = cursor.getString(cursor
                    .getColumnIndexOrThrow(MediaStore.Images.Media.DATA));
            Toast.makeText(this, imageFileName, Toast.LENGTH_LONG).show();
            Log.i("Image", imageFileName);
            Log.i("Image", imageFilePath.toString());
            try {
                bm = BitmapFactory.decodeFile(imageFileName);
                if (bm == null) {
                    throw new Exception("no picture!");
                }
                new FetchItemsTask().execute();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
};

class FetchItemsTask extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... params) {
        try {
            Log.i("Response", "Entered doInBackground");
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            bm.compress(CompressFormat.JPEG, 50, bos);
            byte[] data = bos.toByteArray();

            HttpClient httpClient = new DefaultHttpClient();
            HttpPut putRequest = new HttpPut(URL_SERVER);
            Log.i("Response", "Sending HTTP to "
                    + putRequest.getURI().toASCIIString());
            File file = new File(imageFileName);
            ByteArrayBody bab = new ByteArrayBody(data, file.getName());
            MultipartEntity reqEntity = new MultipartEntity(
                    HttpMultipartMode.BROWSER_COMPATIBLE);
            reqEntity.addPart("image", bab);
            putRequest.setEntity(reqEntity);
            Log.i("Response", "postRequest is: " + putRequest);
            HttpResponse response = httpClient.execute(putRequest);
            Log.i("Response", "Response is: " + response);
            Log.i("Response", "Status is: " + response.getStatusLine());
            BufferedReader newReader = new BufferedReader(new FileReader(imageFileName));
            //BufferedReader reader = new BufferedReader(
            //      new InputStreamReader(
            //              response.getEntity().getContent(), "UTF-8"));
            String sResponse;
            StringBuilder s = new StringBuilder();
            while ((sResponse = newReader.readLine()) != null) {
                s = s.append(sResponse);
            }
        } catch (Exception e) {
            // handle exception here
            e.printStackTrace();
        }
        return null;
    }
}

For my webservice, the code i have is this:

@PUT
@Consumes({MediaType.MULTIPART_FORM_DATA})
@Produces({MediaType.TEXT_PLAIN})
@Path("/image")
public Response uploadImage(
        @FormDataParam("image") InputStream fileInputStream,
        @FormDataParam("image") FormDataContentDisposition contentDispositionHeader) {
    String filePath = SERVER_UPLOAD_LOCATION_FOLDER + contentDispositionHeader.getFileName();

    // save the file to the server
    saveToFile(fileInputStream, filePath);
    String output = "File saved to server location : " + filePath;
    return Response.status(200).entity(output).build();
}

My log file tells me this, and i cannot figure out where the problem lies.

HTTP/1.1 500 Internal Server Error
Server: Apache-Coyote/1.1
Content-Type: text/html;charset=utf-8
Content-Language: en
Content-Length: 8100
Date: Fri, 28 Nov 2014 13:02:30 GMT
Connection: close

<!DOCTYPE html><html><head><title>Apache Tomcat/8.0.15 - Error report</title><style type="text/css">H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}A.name {color : black;}.line {height: 1px; background-color: #525D76; border: none;}</style> </head><body><h1>HTTP Status 500 - org.glassfish.jersey.server.ContainerException: java.lang.NoSuchMethodError: org.glassfish.jersey.server.CloseableService.add(Ljava/io/Closeable;)V</h1><div class="line"></div><p><b>type</b> Exception report</p><p><b>message</b> <u>org.glassfish.jersey.server.ContainerException: java.lang.NoSuchMethodError: org.glassfish.jersey.server.CloseableService.add(Ljava/io/Closeable;)V</u></p><p><b>description</b> <u>The server encountered an internal error that prevented it from fulfilling this request.</u></p><p><b>exception</b></p><pre>javax.servlet.ServletException: org.glassfish.jersey.server.ContainerException: java.lang.NoSuchMethodError: org.glassfish.jersey.server.CloseableService.add(Ljava/io/Closeable;)V
org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:393)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:381)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:344)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:221)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
com.google.inject.servlet.FilterChainInvocation.doFilter(FilterChainInvocation.java:66)
com.google.inject.servlet.ManagedFilterPipeline.dispatch(ManagedFilterPipeline.java:118)
com.google.inject.servlet.GuiceFilter.doFilter(GuiceFilter.java:113)
</pre><p><b>root cause</b></p><pre>org.glassfish.jersey.server.ContainerException: java.lang.NoSuchMethodError: org.glassfish.jersey.server.CloseableService.add(Ljava/io/Closeable;)V
org.glassfish.jersey.servlet.internal.ResponseWriter.rethrow(ResponseWriter.java:256)
org.glassfish.jersey.servlet.internal.ResponseWriter.failure(ResponseWriter.java:238)
org.glassfish.jersey.server.ServerRuntime$Responder.process(ServerRuntime.java:439)
org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:277)
org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
org.glassfish.jersey.internal.Errors.process(Errors.java:315)
org.glassfish.jersey.internal.Errors.process(Errors.java:297)
org.glassfish.jersey.internal.Errors.process(Errors.java:267)
org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:297)
org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:254)
org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1030)
org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:373)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:381)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:344)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:221)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
com.google.inject.servlet.FilterChainInvocation.doFilter(FilterChainInvocation.java:66)
com.google.inject.servlet.ManagedFilterPipeline.dispatch(ManagedFilterPipeline.java:118)
com.google.inject.servlet.GuiceFilter.doFilter(GuiceFilter.java:113)
</pre><p><b>root cause</b></p><pre>java.lang.NoSuchMethodError: org.glassfish.jersey.server.CloseableService.add(Ljava/io/Closeable;)V
org.glassfish.jersey.media.multipart.internal.MultiPartReaderServerSide.readMultiPart(MultiPartReaderServerSide.java:90)
org.glassfish.jersey.media.multipart.internal.MultiPartReaderClientSide.readFrom(MultiPartReaderClientSide.java:179)
org.glassfish.jersey.media.multipart.internal.MultiPartReaderClientSide.readFrom(MultiPartReaderClientSide.java:91)
org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$TerminalReaderInterceptor.invokeReadFrom(ReaderInterceptorExecutor.java:258)
org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$TerminalReaderInterceptor.aroundReadFrom(ReaderInterceptorExecutor.java:234)
org.glassfish.jersey.message.internal.ReaderInterceptorExecutor.proceed(ReaderInterceptorExecutor.java:154)
org.glassfish.jersey.server.internal.MappableExceptionWrapperInterceptor.aroundReadFrom(MappableExceptionWrapperInterceptor.java:73)
org.glassfish.jersey.message.internal.ReaderInterceptorExecutor.proceed(ReaderInterceptorExecutor.java:154)
org.glassfish.jersey.message.internal.MessageBodyFactory.readFrom(MessageBodyFactory.java:1124)
org.glassfish.jersey.message.internal.InboundMessageContext.readEntity(InboundMessageContext.java:851)
org.glassfish.jersey.message.internal.InboundMessageContext.readEntity(InboundMessageContext.java:783)
org.glassfish.jersey.server.ContainerRequest.readEntity(ContainerRequest.java:233)
org.glassfish.jersey.media.multipart.internal.FormDataParamValueFactoryProvider.getEntity(FormDataParamValueFactoryProvider.java:369)
org.glassfish.jersey.media.multipart.internal.FormDataParamValueFactoryProvider.access$000(FormDataParamValueFactoryProvider.java:86)
org.glassfish.jersey.media.multipart.internal.FormDataParamValueFactoryProvider$FormDataParamValueFactory.provide(FormDataParamValueFactoryProvider.java:201)
org.glassfish.jersey.server.spi.internal.ParameterValueHelper.getParameterValues(ParameterValueHelper.java:81)
org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$AbstractMethodParamInvoker.getParamValues(JavaResourceMethodDispatcherProvider.java:121)
org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$ResponseOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:152)
org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:104)
org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:384)
org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:342)
org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:101)
org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:271)
org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
org.glassfish.jersey.internal.Errors.process(Errors.java:315)
org.glassfish.jersey.internal.Errors.process(Errors.java:297)
org.glassfish.jersey.internal.Errors.process(Errors.java:267)
org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:297)
org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:254)
org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1030)
org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:373)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:381)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:344)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:221)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
com.google.inject.servlet.FilterChainInvocation.doFilter(FilterChainInvocation.java:66)
com.google.inject.servlet.ManagedFilterPipeline.dispatch(ManagedFilterPipeline.java:118)
com.google.inject.servlet.GuiceFilter.doFilter(GuiceFilter.java:113)
</pre><p><b>note</b> <u>The full stack trace of the root cause is available in the Apache     Tomcat/8.0.15 logs.</u></p><hr class="line"><h3>Apache Tomcat/8.0.15</h3></body></html>

And this :

28-Nov-2014 14:02:30.252 INFO [http-nio-8080-exec-161] org.glassfish.jersey.filter.LoggingFilter.log 6 * Server has received a request on thread http-nio-8080-exec-161
6 > PUT http://127.0.0.1:9876/webservice-1.0-SNAPSHOT/DirectorResource/image
6 > connection: Keep-Alive
6 > content-length: 16038
6 > content-type: multipart/form-data; boundary=IdmG6B4wBROo8soEP9rPHGgqDxSThQJGb
6 > host: 127.0.0.1:9876
6 > user-agent: Apache-HttpClient/UNAVAILABLE (java 1.4)
--IdmG6B4wBROo8soEP9rPHGgqDxSThQJGb
Content-Disposition: form-data; name="image"; filename="1417179326131.jpg"
Content-Type: application/octet-stream

If you need any more code, let me know, but i thought this was what was needed. Thanks!

1

There are 1 answers

1
Coty Condry On

I had this problem as well, and I fixed it by upgrading all my Jersey libs to 2.9 in my pom.xml file. I think the root cause of my problem was that the jersey-container-servlet, jersey-container-servlet-core, and jersey-media-multipart libs were not the same version. I had 2.8 jersey-container-servlet-core and jersey-container-servlet, and 2.7 of jersey-media-multipart.