1

currently I am working with Android native and MySQL. To connect both of it, I used Java and REST. However, when I tried to run my REST service, I did not get any response (just blank page show up). I have tried to check the log and I did now find any error. Here is my code:

Database.java -- Database Connection file

package datasource;

import java.sql.Connection;
import java.sql.DriverManager;

public class Database 
{
    @SuppressWarnings("finally")
    public static Connection createConnection() throws Exception {
        Connection con = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/friseur", "root", "");
        } catch (Exception e) {
            throw e;
        } finally {
            return con;
        }
    }
}

QueryPhoto.java -- To query from database

package query;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import datasource.Database;
import model.Photo;

public class QueryPhoto {
    public static ArrayList<Photo> GetPhoto() throws Exception
    {
        ArrayList<Photo> photo = new ArrayList<Photo>();
        Connection dbConn = null;
        try {
            try {
                dbConn = Database.createConnection();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            Statement stmt = dbConn.createStatement();
            String selectPhoto = "SELECT * FROM photo, user where photo.user_id = user.id";
            ResultSet rs = stmt.executeQuery(selectPhoto);
            while(rs.next())
            {
                Photo photoObject = new Photo();
                photoObject.setPDateUpload(rs.getString("added_time"));
                photoObject.setPUrlOriginal(rs.getString("url_original"));
                photoObject.setPUrlWithHair(rs.getString("url_with_hair"));
                photoObject.setCaption(rs.getString("caption"));
                photoObject.setUserId(Integer.parseInt(rs.getString("user_id")));
                photoObject.setUserName(rs.getString("username"));

                photo.add(photoObject);
            }
        }
        catch (SQLException sqle) {
            throw sqle;
        }
        catch (Exception e) {
            // TODO Auto-generated catch block
            if (dbConn != null) {
                dbConn.close();
            }
            throw e;
        }
        finally {
            if (dbConn != null) {
                dbConn.close();
            }
        }
        return photo;
    }
}

PhotoJson.java -- to construct json object

package queryresult;

import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import java.util.*;

import model.Photo;

public class PhotoJson {
    public static String constructJSON( ArrayList<Photo> photo) {
        JSONObject obj = new JSONObject();
        try {
            for (int i = 0; i < photo.size(); i++) {
                Photo photoObj = photo.get(i);
                obj.put("date_upload", photoObj.getPDateUpload());
                obj.put("url_original", photoObj.getPUrlOriginal());
                obj.put("url_with_hair", photoObj.getPUrlWithHair());
                obj.put("caption", photoObj.getCaption());
                obj.put("user_id", photoObj.getUserId());
                obj.put("username", photoObj.getUserName());
            }
        } catch (Exception e) {

        }
        return obj.toString();
    }
}

GetPhoto.java -- for the REST

package webservice;

import java.util.ArrayList;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import model.Photo;
import query.QueryPhoto;
import queryresult.PhotoJson;

@Path("/WebService")
public class GetPhoto {

@POST
@Path("/GetPhotos")
@Produces("application/json")
public String photo()
{
    String response = null;
    try 
    {
        ArrayList<Photo> photo = QueryPhoto.GetPhoto();
        response = PhotoJson.constructJSON(photo);
    }
    catch (Exception e)
    {
        System.out.println("Exception Error"); //Console 
    }
    return response;
    }
}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
    <display-name>useraccount</display-name>
        <servlet>
            <servlet-name>Jersey REST Service</servlet-name>
            <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
            <init-param>
                    <param-name>com.sun.jersey.config.property.packages</param-name>
                    <param-value>webservice</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
        </servlet>
        <servlet-mapping>
            <servlet-name>Jersey REST Service</servlet-name>
            <url-pattern>/*</url-pattern>
        </servlet-mapping>
</web-app>

What is wrong actually? Thanks so much for your help.

1
  • What URL are you calling? Are you calling it with POST or GET? Commented Oct 3, 2014 at 3:01

1 Answer 1

1

Assuming that you're calling the correct URL, the problem most likely is that you're sending an HTTP GET request (you mention a blank page showing), while your method is annotated to respond to POST requests`.

Changing your annotation to @GET would then solve your problem:

@GET
@Path("/GetPhotos")
@Produces("application/json")
public String photo() { /* ... */ }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.