Json no funciona con HttpPost probablemente alrededor de setEntity

Estoy utilizando este código para enviar esto a mi archivo php. El archivo busca lo que viene así.

file_put_contents('dump.txt', "POST: \n" . print_r($_POST, true) . "\n\n\n GET: \n" . print_r($_GET, true)); 

Estoy enviando el json como este:

 public void postData(String url,JSONObject json) { HttpClient httpclient = new DefaultHttpClient(); String object = "?data=" +json.toString(); System.out.println("object:" + object); try { HttpPost httppost = new HttpPost(url.toString()); httppost.setHeader("Content-type", "application/json"); System.out.println("url:" + url.toString()); StringEntity se = new StringEntity(object); System.out.println("json:" + json.toString()); se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); httppost.setEntity(se); HttpResponse response = httpclient.execute(httppost); String temp = EntityUtils.toString(response.getEntity()); System.out.println("response:" + response); int statusCode = response.getStatusLine().getStatusCode(); System.out.println("Statuscode " + statusCode); if(statusCode==200) { Toast.makeText(LbsGeocodingActivity.this, "Verstuurd", Toast.LENGTH_LONG).show(); } else if(statusCode!=200) { Toast.makeText(this, statusCode, Toast.LENGTH_LONG).show(); } /* try { String responseBody = EntityUtils.toString(response.getEntity()); Toast.makeText(this, responseBody, Toast.LENGTH_LONG).show(); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }*/ } catch (ClientProtocolException e) { } catch (IOException e) { } } 

Los archivos de registro son los siguientes:

 01-08 09:36:00.278: I/System.out(1124): object:?data={"id":"69403","timestamp":"08-01-2013 09:36:00","longitude":"-122.084095","latitude":"37.422005"} 01-08 09:36:00.298: I/System.out(1124): json:{"id":"69403","timestamp":"08-01-2013 09:36:00","longitude":"-122.084095","latitude":"37.422005"} 01-08 09:36:01.038: I/System.out(1124): response:org.apache.http.message.BasicHttpResponse@412d1b38 01-08 09:36:01.038: I/System.out(1124): Statuscode 200 

He dejado fuera la url por razones de seguridad ..

Pero estoy recibiendo un archivo vacío en dump.txt

 POST: Array ( ) GET: Array ( ) 

EDITAR

Algo está mal con la setEntity (o en algún lugar por ahí), porque cuando ya pegar? Data = en la url puedo obtener esto desde el archivo PHP:

 POST: Array ( ) GET: Array ( [data] => ) 

la forma en que envío JSON con mi DefaultHttpClient en mi aplicación funciona perfectamente:

 public static HttpUriRequest createPostForJSONObject( JSONObject params, String url) { HttpPost post = new HttpPost(url); post.setEntity(createStringEntity(params)); return post; } private static HttpEntity createStringEntity(JSONObject params) { StringEntity se = null; try { se = new StringEntity(params.toString(), "UTF-8"); se.setContentType("application/json; charset=UTF-8"); } catch (UnsupportedEncodingException e) { Log.e(TAG, "Failed to create StringEntity", e); exception = e; } return se; } 

para ejecutar esta solicitud en tu código todo lo que necesitas hacer es:

 public void postData(String url,JSONObject json) { HttpClient httpclient = new DefaultHttpClient(); try { HttpPost post = createPostForJSONObject(json, url); HttpResponse response = httpClient.execute(post); // DO YOUR THING } catch Exception { // HANDLE EXCEPTION } } 
  • Cómo agregar una matriz de cadenas en un objeto JSON?
  • El archivo google-services.json falta en la carpeta raíz del módulo. El complemento de servicios de Google no puede funcionar sin él
  • Citas de relleno en JSONObject
  • JSON Cadena devuelta desde el servicio web SOAP que no contiene registros para la tabla
  • Poner byte en JSON en android
  • Org.codehaus.jackson.map.exc.UnrecognizedPropertyException: Campo no reconocido "id" (Criterios de clase), no marcado como ignorable
  • ¿Cómo recuperar datos de la base de datos de firebase que está teniendo matriz anidada y estructura de objetos?
  • ¿Cuál es la mejor manera de tratar con el objeto JSON?
  • Conversión de JSONArray a matriz normal
  • Android analiza la serie Json de cadenas
  • ¿Cómo obtener una cadena de respuesta de Retrofit sin procesar?
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.