0

I am new to web services. I am building an android app and I'm trying to send a post request to the web service but I'm not sure what the correct format is.

Can I send it in XML type, do I have to modify the POST method in order to access it from my app?

This is the POST method in the REST:

@POST
@Override
@Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON,MediaType.TEXT_XML})
public void create(Users entity) {
    super.create(entity);
}

This is the class that corresponds to our table. I am using Oracle 11g database, Glassfish server 4.1.1 and Netbeans. I am trying to call PUT from my android application. Can someone suggest a way to do that?

import java.io.Serializable;
import javax.persistence.Basic; 
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;

@Entity
@Table(name = "USERS")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "Users.findAll", query = "SELECT u FROM Users u"),
@NamedQuery(name = "Users.findByUserId", query = "SELECT u FROM Users u WHERE u.userId = :userId"),
@NamedQuery(name = "Users.findByUsername", query = "SELECT u FROM Users u WHERE u.username = :username"),
@NamedQuery(name = "Users.findByPassword", query = "SELECT u FROM Users u WHERE u.password = :password"),
@NamedQuery(name = "Users.findByEmail", query = "SELECT u FROM Users u WHERE u.email = :email")})


public class Users implements Serializable {

private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@NotNull
@Column(name = "USER_ID")
private Short userId;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 30)
@Column(name = "USERNAME")
private String username;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 20)
@Column(name = "PASSWORD")
private String password;

@Basic(optional = false)
@NotNull
@Size(min = 1, max = 40)
@Column(name = "EMAIL")
private String email;
@OneToOne(cascade = CascadeType.ALL, mappedBy = "users")
private Profiles profiles;

public Users() {
}

public Users(Short userId) {
    this.userId = userId;
}

public Users(Short userId, String username, String password, String email) {
    this.userId = userId;
    this.username = username;
    this.password = password;
    this.email = email;
}

public Short getUserId() {
    return userId;
}

public void setUserId(Short userId) {
    this.userId = userId;
}

public String getUsername() {
    return username;
}

public void setUsername(String username) {
    this.username = username;
}

public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

public Profiles getProfiles() {
    return profiles;
}

public void setProfiles(Profiles profiles) {
    this.profiles = profiles;
}

@Override
public int hashCode() {
    int hash = 0;
    hash += (userId != null ? userId.hashCode() : 0);
    return hash;
}

@Override
public boolean equals(Object object) {
    // TODO: Warning - this method won't work in the case the id fields are not set
    if (!(object instanceof Users)) {
        return false;
    }
    Users other = (Users) object;
    if ((this.userId == null && other.userId != null) || (this.userId != null && !this.userId.equals(other.userId))) {
        return false;
    }
    return true;
}

@Override
public String toString() {
    return "medicalplanner.Users[ userId=" + userId + " ]";
}

}
sherlockx
  • 11
  • 2

2 Answers2

0

you can use REST or SOAP protocols for this. JSON format is more suitable than this. If you need more security, use SOAP. But SOAP is slower than the JSON format. You can see the differences here :
SOAP vs REST (differences)
And there are some libraries for creating JSON or XML format of class. I am suggesting Jackson library. https://github.com/FasterXML/jackson . You can check the GSON library also. https://github.com/google/gson

Community
  • 1
  • 1
Abdullah Tellioglu
  • 1,434
  • 1
  • 10
  • 26
0

You can absolutely do what you've got listed - I have JAX-RS applications that take either JSON or XML.

Can you tell us more about the error? It honestly sounds like you've got an error in your XML that makes the parser confused. Can you paste the XML that you're sending.

stdunbar
  • 16,263
  • 11
  • 31
  • 53
  • Right now I can't do any more requests because I got an internal server error after I changed a POST method and I have no errors in the log, so I don't know how to fix it. I also changed it back to how it was before and I get the same error. – sherlockx Apr 13 '16 at 21:43
  • This is the format I'm trying to send user@smth.com 1 pass user – sherlockx Apr 14 '16 at 10:51