0

I am developing a web application for university project, where an android client sends to a java server an image, and after that the server compares the image with those in the database and it has to send a response as a String object to the client who invoked the server. How can i do it? I use android-async-http to send the image from client and it works great, but i don't know how to send a response. This is a partial code of the client:

public void makeHTTPCall(){
    prgDialog.setMessage("Server Connection..);
    AsyncHttpClient client=new AsyncHttpClient();
    client.post(ipAddress,params, new AsyncHttpResponseHandler() {
        @Override
        public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
            prgDialog.hide();
            Snackbar.make(parentLayout,"Image uploaded.",Snackbar.LENGTH_LONG).show();
        }

        @Override
        public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
            prgDialog.hide();
            if(statusCode==404){
                Snackbar.make(parentLayout,"Page not found.",Snackbar.LENGTH_LONG).show();
            }
            else if(statusCode==500){

                Snackbar.make(parentLayout,"Server problem.",Snackbar.LENGTH_LONG).show();
            }else{
                Snackbar.make(parentLayout,"Generic Error. code: "+statusCode,Snackbar.LENGTH_LONG).show();
            }
        }
    });


}

and here the code from server:

<%
String imgEncodedStr = request.getParameter("image");
String fileName = request.getParameter("filename");
System.out.println("Filename: "+ fileName);
if(imgEncodedStr != null){
    ManipolaImmagini.stringToImage(imgEncodedStr, fileName);
    System.out.println("Inside if");
    out.print("Image upload complete, Please check your directory");

} else{
    System.out.println("Inside else");
    out.print("Image is empty");    
}%>
Kakey
  • 3,936
  • 5
  • 29
  • 45
sochisei
  • 11
  • 2

2 Answers2

0

What you looking for is a WebService.

Here are some resources to start with that:

Difference between web services and servlet

Oracle Web Service Guide

Your problem could be related to: Calling WebServices from JSP

Community
  • 1
  • 1
ikkarion
  • 178
  • 4
  • 17
  • Yes, i want to build a WebService. If i use a Servlet, how can i do the call from the client to Servlet? – sochisei Jun 17 '16 at 14:42
  • Your Servlet has a url pattern associate with it. For example: @WebServlet("/YourService"). So you need to call: your.server/YourService. The example that I provided worked? – ikkarion Jun 17 '16 at 14:50
  • There are a large amount of resources, here in SO and the in the web, about how to properly use WebServices and Servlets. Check the updated answer. – ikkarion Jun 17 '16 at 14:57
  • Yes, for this project i have to implement this web service, but sorry i don't understand really how to do. I found this [link](http://programmerguru.com/android-tutorial/how-to-call-java-web-service-in-android/) and i think i can use this method for send the image and to get an answer. Do you think that work? – sochisei Jun 17 '16 at 15:08
  • That is up to you. You need to know if that aproach fits your needs. I think you should make some research about websevices. – ikkarion Jun 17 '16 at 16:31
0

I'm working on a similar project. I allready have my webservices and now I'm on Android side. My webservices use Jersey. If your server is compatible, it's not so tricky to make a webservice and Jersey documentation is really good.

Server side If needed, you have to put librairies in your project. If you use maven, you have to follow : https://jersey.java.net/documentation/latest/modules-and-dependencies.html#dependencies

In you war project, to activate Jersey, an easy way could be to make a class as simple as this :

@javax.ws.rs.ApplicationPath("/url_to_your_webservices")
public class MyApplication extends ResourceConfig {

    public MyApplication() {
        // Association des classes de webservices
        packages(true, "path.to.your.webservices.package");
    }
}

Otherwise, there are other solutions described here : https://jersey.java.net/documentation/latest/deployment.html

Next step, you have to create a Resource class which is your Webservice and could be something like that :

@Path("url_of_the_resource")
@RequestScoped
public class YourResource {
    @GET
    @Path("/url_of_the_method_inside_your_resource")
    @Produces("application/txt")
    @Consumes("image/jpeg")
    public Response compareMyImage() {
        // See how you can get your images and build your answer which could be what you want TXT, JSON, XML...

        return Response.ok("Awnser Could be what you want.").build();
    }
}

More details on how to construct webservices here : https://docs.oracle.com/javaee/7/tutorial/partwebsvcs.htm#BNAYK

Client side
It's seems that there is a Jersey client for android : https://blogs.oracle.com/japod/entry/jersey_2_x_client_on1 Otherwise, you can a traditionnal http client.

Of course, you have to adapt my examples to your case. Good luck, hope it helps.

Xavier Lambros
  • 776
  • 11
  • 19