将使用javax.ws.rs。*和javax.xml.bind。*编写的RESTful Web服务客户端转换为工作

时间:2019-07-09 11:36:53

标签: java android rest web-services

我编写了这个宁静的Web服务客户端,以使用JAX-RS Web服务。我想在android上使用它,但是android不支持javax.ws或javax.xml.bind库。有没有一种简单的方法就可以将代码转换为可在android上工作而无需进行彻底检修?如果没有,我可以使用什么简单的android restful webservice API?

package databaseAccess;
import databaseAccess.Recipe.recipeType;
import databaseAccess.UserProfile.skillLevel;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

public class DatabaseService {

private static final String BASE_URI = 
"http://localhost:8080/RecipeAppDatabaseService/webresources";
private Client client;

public DatabaseService() {            
    client = ClientBuilder.newClient();
}

/**
 * validate if correct password (on sign-in)
 * @return if correct - the user's profile. if incorrect username or 
    password - returns null
 */
public UserProfile validateSignIn(String email, String password) {
    WebTarget webTarget = 
    client.target(BASE_URI).path("userProfile/signIn/" + email + "/" + 
                                 password);
    return webTarget.request().get(UserProfile.class);
}

/**
 * add a new user to the database (on first sign-up)
 */
public Response addUser(UserProfile user, String password) {
    WebTarget webTarget = client.target(BASE_URI).path("userProfile/add/" 
                          + password);
    Response response = 
 webTarget.request().post(Entity.entity(user,MediaType.APPLICATION_XML));
    return response;
}

/**
 * delete account
 */
public Response deleteUser(String email) {
    WebTarget webTarget = client.target(BASE_URI).path("userProfile/" + 
    email);
    Response response = webTarget.request().delete();
    return response;
}

/**
 * get User profile based on a given email
 * @return user profile
 */
public UserProfile getUser(String email) {
    WebTarget webTarget = client.target(BASE_URI).path("userProfile/" + 
    email);
    return webTarget.request().get(UserProfile.class);
}

/**
 * add a follower to a user
 */
public Response addFollower(String userEmail, String followerEmail) {
    WebTarget webTarget = client.target(BASE_URI).path("userProfile/" + userEmail + "/" + followerEmail);
    Response response = webTarget.request().get();
    return response;
}

/**
 * add a follower to a user
 */
public Response deleteFollower(String userEmail, String followerEmail) {
    WebTarget webTarget = client.target(BASE_URI).path("userProfile/" + 
    userEmail + "/" + followerEmail);
    Response response = webTarget.request().delete();
    return response;        
}

...

package databaseAccess;

import java.io.Serializable;
import java.util.ArrayList;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class UserProfile implements Serializable {

public enum skillLevel{BEGINNER, INTERMEDIATE, PRO}

private String email;
private String firstName;
private String lastName;
private skillLevel cookingSkills;
@XmlElement
private ArrayList<String> cuisines;
private String country;
@XmlElement
private ArrayList<String> followers; // emails of followers
@XmlElement
private ArrayList<String> followerOf; // emails of people the user 
 follows

public UserProfile() {
    followers = new ArrayList<>();
    followerOf = new ArrayList<>();
    cuisines = new ArrayList<>();
}
 ...

0 个答案:

没有答案