[英]Communicating by JSON Rest Server (Jersey) and Android
我正在尝试做一个简单的应用程序,其中:
(服务器)-泽西RESTful
(客户端)-Android
我正在尝试使他们通过JSON进行通信,我已经看过几种材料,但是我仍然做不到。
遵守我的守则。 如果有人可以帮助我。 我很高兴
服务器代码:
@POST
@Consumes({ MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_JSON })
public void consomeJson(Gson json) {
System.out.println(json);
}
客户代码:
public void onBtnSalvarClicked(View view)
{
TextView t = (TextView) findViewById(R.id.text);
TextView nome = (TextView) findViewById(R.id.txtNome);
TextView login = (TextView) findViewById(R.id.txtLogin);
TextView senha = (TextView) findViewById(R.id.txtSenha);
Usuario usuario = new Usuario();
usuario.setNome(nome.getText().toString());
usuario.setLogin(login.getText().toString());
usuario.setSenha(senha.getText().toString());
Gson gson = new Gson();
params.put("var", gson.toJson(usuario));
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://192.168.1.2:8080/rest/hello");
try{
StringEntity se = new StringEntity(gson.toString());
httppost.setEntity(se);
HttpResponse response = httpclient.execute(httppost);
}
catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
提前致谢。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.