Java 通过JSON Rest服务器(Jersey)和Android进行通信
我正在尝试做一个简单的应用程序,我有: (服务器) -休闲运动衫 (客户) -安卓 我试图让他们通过JSON进行通信,我已经看到了一些资料,但我仍然无法做到这一点 遵循我的密码。如果有人能帮我。我很高兴 服务器代码:Java 通过JSON Rest服务器(Jersey)和Android进行通信,java,android,rest,jersey,gson,Java,Android,Rest,Jersey,Gson,我正在尝试做一个简单的应用程序,我有: (服务器) -休闲运动衫 (客户) -安卓 我试图让他们通过JSON进行通信,我已经看到了一些资料,但我仍然无法做到这一点 遵循我的密码。如果有人能帮我。我很高兴 服务器代码: @POST @Consumes({ MediaType.APPLICATION_JSON }) @Produces({ MediaType.APPLICATION_JSON }) public void consomeJson(Gson 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();
}
}
提前感谢。看起来您正在UI线程上发出HTTP请求,Android不会喜欢。你至少应该使用一个。更多关于网络运营的信息 此外,请确保在清单中包含网络权限:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
还可以查看Android的新截击库。它应该使网络请求更简单。有一个教程和一些更多的信息