我是新手。从服务器读取 json 的 Web 服务可以工作,但是当我在 Android 中通过 json 创建新记录时,它会发送,但我收到 null。
代码:
HttpPost post = new HttpPost("http://192.168.42.194/app/web/app.php/android/buscar");
post.setHeader("content-type", "application/json;charset=UTF-8");
try {
JSONStringer dato = new JSONStringer().object().key("cadena").value("valor").endObject();
StringEntity entidad = new StringEntity(dato.toString());
post.setEntity(entidad);
HttpClient cliente = new DefaultHttpClient();
HttpResponse respuesta = cliente.execute(post);
String obtiene = EntityUtils.toString(respuesta.getEntity());
JSONObject object2 = new JSONObject(obtiene);
String mensaje = object2.getString("cod");
Log.e("LOG", mensaje.toString());
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
alertDialogBuilder.setTitle("Your Title");
alertDialogBuilder.setMessage("Enviado "+ mensaje);
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
} catch (Exception ex) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
alertDialogBuilder.setTitle("Error");
alertDialogBuilder.setMessage("Enviar " + query.getText());
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}