Foros del Web » Programando para Internet » Android »

Problemas al obtner valores del json

Estas en el tema de Problemas al obtner valores del json en el foro de Android en Foros del Web. Hola y de antemano gracias a todos. Tengo este codigo: @import url("http://static.forosdelweb.com/clientscript/vbulletin_css/geshi.css"); Código Java: Ver original /**  * Author: Ravi Tamada  * URL: www.androidhive.info  * ...
  #1 (permalink)  
Antiguo 25/11/2014, 09:04
Avatar de Maverick2786  
Fecha de Ingreso: diciembre-2012
Mensajes: 107
Antigüedad: 11 años, 4 meses
Puntos: 1
Problemas al obtner valores del json

Hola y de antemano gracias a todos.
Tengo este codigo:
Código Java:
Ver original
  1. /**
  2.  * Author: Ravi Tamada
  3.  * URL: www.androidhive.info
  4.  * twitter: http://twitter.com/ravitamada
  5.  * */
  6. package com.example.androidhive.library;
  7.  
  8. import java.io.BufferedReader;
  9. import java.io.IOException;
  10. import java.io.InputStream;
  11. import java.io.InputStreamReader;
  12. import java.io.UnsupportedEncodingException;
  13. import java.util.List;
  14.  
  15. import org.apache.http.HttpEntity;
  16. import org.apache.http.HttpResponse;
  17. import org.apache.http.NameValuePair;
  18. import org.apache.http.client.ClientProtocolException;
  19. import org.apache.http.client.entity.UrlEncodedFormEntity;
  20. import org.apache.http.client.methods.HttpPost;
  21. import org.apache.http.impl.client.DefaultHttpClient;
  22. import org.json.JSONException;
  23. import org.json.JSONObject;
  24.  
  25. import android.util.Log;
  26.  
  27. public class JSONParser {
  28.  
  29.     static InputStream is = null;
  30.     static JSONObject jObj = null;
  31.     static String json = "";
  32.  
  33.     // constructor
  34.     public JSONParser() {
  35.  
  36.     }
  37.  
  38.     public JSONObject getJSONFromUrl(String url, List<NameValuePair> params) {
  39.  
  40.         // Making HTTP request
  41.         try {
  42.             // defaultHttpClient
  43.             DefaultHttpClient httpClient = new DefaultHttpClient();
  44.             HttpPost httpPost = new HttpPost(url);
  45.             httpPost.setEntity(new UrlEncodedFormEntity(params));
  46.  
  47.             HttpResponse httpResponse = httpClient.execute(httpPost);
  48.             HttpEntity httpEntity = httpResponse.getEntity();
  49.             is = httpEntity.getContent();
  50.  
  51.         } catch (UnsupportedEncodingException e) {
  52.             e.printStackTrace();
  53.         } catch (ClientProtocolException e) {
  54.             e.printStackTrace();
  55.         } catch (IOException e) {
  56.             e.printStackTrace();
  57.         }
  58.  
  59.         try {
  60.             BufferedReader reader = new BufferedReader(new InputStreamReader(
  61.                     is, "iso-8859-1"), 8);
  62.             StringBuilder sb = new StringBuilder();
  63.             String line = null;
  64.             while ((line = reader.readLine()) != null) {
  65.                 sb.append(line + "\n");
  66.             }
  67.             is.close();
  68.             json = sb.toString();
  69.             Log.e("JSON_getJSONFromUrl", json);
  70.         } catch (Exception e) {
  71.             Log.e("Buffer Error", "Error converting result " + e.toString());
  72.         }
  73.  
  74.         // try parse the string to a JSON object
  75.         try {
  76.             jObj = new JSONObject(json);
  77.             System.out.println("JsonParser_try3 "+jObj);
  78.         } catch (JSONException e) {
  79.             Log.e("JSON Parser", "Error parsing data " + e.toString());
  80.         }
  81.  
  82.         // return JSON String
  83.         return jObj;
  84.     }
  85. }

donde obtengo este resultado:
Código LogCat:
Ver original
  1. 11-25 15:03:49.942: E/JSON_getJSONFromUrl(921): {"tag":"syncronize","success":1,"error":0,"uid":"546ccc4d8846e1.73207896","user":{"name":"Pablo","email":"[email protected]","created_at":"2014-11-19 10:58:53","updated_at":null}}{"tag":"syncronize","success":1,"error":0,"uid":"546cd113ebddc7.01724635","user":{"name":"a","email":"aa","created_at":"2014-11-19 11:19:15","updated_at":null}}
  2.  
  3. 11-25 15:03:49.962: I/System.out(921): JsonParser_try3 {"uid":"546ccc4d8846e1.73207896","error":0,"user":{"created_at":"2014-11-19 10:58:53","email":"[email protected]","updated_at":null,"name":"Pablo"},"success":1,"tag":"syncronize"}
  4.  
  5. 11-25 15:03:49.972: I/System.out(921): JSON_GET_ALL_USER1 {"uid":"546ccc4d8846e1.73207896","error":0,"user":{"created_at":"2014-11-19 10:58:53","email":"[email protected]","updated_at":null,"name":"Pablo"},"success":1,"tag":"syncronize"}

Como puede ver en el momento que ejecuto: "Log.e("JSON_getJSONFromUrl", json);"`tengo todos los valores, pero al momento de ejecutar: "jObj = new JSONObject(json);" y despues imprimo la variable "System.out.println("JsonParser_try3 "+jObj);" ya solo tengo el primero, he buscado y me han dicho que utilice el jsonarray , pero no tengo ni idea de como hacerlo, he buscado algunos ejemplos y todos me dan error

alguien me puede ayudar o darme un ejemplo?
Gracias

PD: Esta misma pregunta la hice en : http://stackoverflow.com/questions/2...30634_27113754
  #2 (permalink)  
Antiguo 25/11/2014, 09:49
Avatar de Manuellama  
Fecha de Ingreso: noviembre-2006
Ubicación: Zafra, Extremadura, Spain, Spain
Mensajes: 174
Antigüedad: 17 años, 5 meses
Puntos: 10
Respuesta: Problemas al obtner valores del json

http://mrbool.com/how-to-use-json-to...lication/28944

No sé si te servirá esto.
  #3 (permalink)  
Antiguo 29/11/2014, 11:22
 
Fecha de Ingreso: noviembre-2014
Ubicación: Venezuela
Mensajes: 7
Antigüedad: 9 años, 4 meses
Puntos: 0
Respuesta: Problemas al obtner valores del json

Hola el uso de JSONArray es asi

JSONArray json = new JSONArray(String_resultado_json);

for (int i = 0; i < json.length(); i++) {

JSONObject obj = json.getJSONObject(i);
String Variable= obj.getString("NombreCampoJson");
}

Debes tener esto dentro de un AsyncTask para que no halla problemas s
Saludos

Etiquetas: androi, app, json, list
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 02:37.