Deseo enviar el texto JSON {} a un servicio web y leer la respuesta. ¿Cómo puedo hacer esto desde Android? ¿Cuáles son los pasos tales como la creación de peticiones de objetos, establecer cabeceras de contenido, etc.¿Cómo enviar un objeto JSON sobre HttpClient Request con Android?
Mi código es aquí
public void postData(String result,JSONObject obj) {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpParams myParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(myParams, 10000);
HttpConnectionParams.setSoTimeout(myParams, 10000);
String json=obj.toString();
try {
HttpPost httppost = new HttpPost(result.toString());
StringEntity se = new StringEntity(obj.toString());
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
httppost.setEntity(se);
HttpResponse response = httpclient.execute(httppost);
String temp = EntityUtils.toString(response.getEntity());
Log.i("tag", temp);
} catch (ClientProtocolException e) {
} catch (IOException e) {
}
}
lo error que he hecho PLZ me corregir porque me muestra un error de solicitud incorrecta pero cuando i insertas en el cartel que me muestra el estado de éxito como 200 OK
Intente configurar este tipo 'entity.setContentType (" application/json; charset = UTF-8 "); \t \t \t \t entity.setContentEncoding (nueva BasicHeader (HTTP.CONTENT_TYPE, \t \t \t \t \t \t "application/json; charset = UTF-8")); ' – MKJParekh
@Soni Sin su trabajo no está bien para mí, lo intenté pero me muestra el error de solicitud incorrecta –
@Soni, el único error que se ha cometido es no configurar Header como este en la siguiente publicación httppost.setHeader ("Content-type", "application/json"); –