Wednesday, 18 September 2013

posting json object to rest web service through android client

posting json object to rest web service through android client

I think my android client is unable to send data to my web service, this
is my client code:
value1 = json_obj.getString("dbname");
value2 = json_obj.getString("dbuser");
value3 = json_obj.getString("dbpwd");
HttpClient httpClient = new DefaultHttpClient();
HttpPost post = new HttpPost("****/api/v1/user/");
post.setHeader("content-type", "application/json;
charset=UTF-8");
JSONObject dato = new JSONObject();
dato.put("dbname", value1);
dato.put("dbuser", value2);
dato.put("dbpwd", value3);
StringEntity entity = new StringEntity(dato.toString());
post.setEntity(entity);
HttpResponse resp = httpClient.execute(post);
String respStr = EntityUtils.toString(resp.getEntity());
and my web service's model for receiving the object is:
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class User {
public String getDBname() {
return dbName;
}
public void setDBname(String database) {
this.dbName = database;
}
public String getUserName() {
return dbUser;
}
public void setUserName(String username) {
this.dbUser = username;
}
public String getPass() {
return dbPwd;
}
public void setPass(String password) {
this.dbPwd = password;
}
public User() {
dbName = "";
dbUser = "";
dbPwd = "";
}
public User(String dbname, String dbuser, String dbpwd) {
this.dbName = dbname;
this.dbUser = dbuser;
this.dbPwd = dbpwd;
}
private String dbName;
private String dbUser;
private String dbPwd;
}
@POST side is:
@Path("/v1/user/")
public class V1_user {
@Context
UriInfo uriInfo;
@Context
Request request;
@POST
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Produces(MediaType.APPLICATION_JSON)
public User postPerson(
MultivaluedMap<String, String> personParams
) {
String dbName = personParams.getFirst(DB_NAME);
String userName = personParams.getFirst(USER_NAME);
String password = personParams.getFirst(PASSWORD);
System.out.println("Storing posted " + dbName + " " + userName + " "
+ password);
user.setDBname(dbName);
user.setUserName(userName);
user.setPass(password);
System.out.println("person info: " + user.getDBname() + " " +
user.getUserName() + " " + user.getPass());
return user;
}
}
The web service side works on another android client, but this one I
couldn't get working, what am I doing wrong? Why doesn't the web service
print the result to the console? Thanks in advance.

No comments:

Post a Comment