私が持っている簡単なアプリケーションを実行しようとしています:
(サーバー) - ジャージー 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();
}
}
前もって感謝します。