Foros del Web » Programando para Internet » Android »

Como leer un Array JSON en un pho desde Android ¿Donde esta el error?

Estas en el tema de Como leer un Array JSON en un pho desde Android ¿Donde esta el error? en el foro de Android en Foros del Web. Hola amigos, estoy intentando leer un array un Codigo json que esta en un php y me estoy volviendo loco. Eclipse no me da errores ...
  #1 (permalink)  
Antiguo 13/09/2013, 10:20
 
Fecha de Ingreso: diciembre-2008
Mensajes: 91
Antigüedad: 15 años, 4 meses
Puntos: 1
Como leer un Array JSON en un pho desde Android ¿Donde esta el error?

Hola amigos, estoy intentando leer un array un Codigo json que esta en un php y me estoy volviendo loco.

Eclipse no me da errores pero el programa no funciona

Al ejecutarse no ocuurre nada u el Textview al que se supone que hau que escribir no funciona

Puede alguien ayudarme por favor???
Necesito leer arrys json en php, desde androiddd!!

graciaasss!

el xml del proyexto solo contiene un textview (textView1)


El codigo java

Código:
      package com.json.php;
    
    import android.app.Activity;
    import android.os.Bundle;
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import android.widget.TextView;
    import org.apache.http.HttpResponse;
    import org.apache.http.client.ClientProtocolException;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.impl.client.DefaultHttpClient;
    import org.json.JSONArray;
    import org.json.JSONException;
    import org.json.JSONObject;
    
    
    public class JSONExampleActivity extends Activity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            HttpClient httpclient = new DefaultHttpClient();
           HttpPost httppost = new HttpPost("http://iatek.eu/sys/getsms.php");
            
            TextView textView = (TextView)findViewById(R.id.textView1);
    		try {
    			
    			HttpResponse response = httpclient.execute(httppost);           
    			String jsonResult = inputStreamToString(response.getEntity().getContent()).toString();                      
    			JSONObject obj = new JSONObject(jsonResult);
    			JSONArray jsonArray = obj.getJSONArray("posts");
    
    			/*Para hacer prueba accedo a un registro concreto en este caso el 3*/
    			   JSONObject childJSONObject = jsonArray.getJSONObject(3);
    			     String username = childJSONObject.getString("username");
    			     String sms = childJSONObject.getString("sms");
    			     String fcat = childJSONObject.getString("fcat");
    			     textView.setText(""+sms+"--" + username);
    			
    			/* para hacer pruebas lo he comentado
    			for (int i = 0; i < jsonArray.length(); i++) 
    			{
    			     JSONObject childJSONObject = jsonArray.getJSONObject(i);
    			     String username = childJSONObject.getString("username");
    			     String sms = childJSONObject.getString("sms");
    			     String fcat = childJSONObject.getString("fcat");
    			     textView.setText(""+sms+"--" + username);
    			}*/
    
    			
    	    	
    		} 
    		catch (JSONException e) {
    			e.printStackTrace();
    		} 
    		catch (ClientProtocolException e) {
    			e.printStackTrace();
    		} 
    		catch (IOException e) {
    			e.printStackTrace();
    		}
    
    
           }
        private StringBuilder inputStreamToString(InputStream is) {
            String rLine = "";
            StringBuilder answer = new StringBuilder();
            BufferedReader rd = new BufferedReader(new InputStreamReader(is));
             
            try {
             while ((rLine = rd.readLine()) != null) {
              answer.append(rLine);
               }
            }
             
            catch (IOException e) {
                e.printStackTrace();
             }
            return answer;
           }
    }

Aqui esta el codigo JSON que radica en
iatek.eu/sys/getsms.php

Código:
    {"posts":[{"cid":"11","username":"Livi","sms":"ag","fto":"","fcat":"cat"},{"cid":"10","username":"Sumone","sms":"","fto":"","fcat":""},{"cid":"9","username":"R2D2","sms":"dw","fto":"wd","fcat":"wd"},{"cid":"5","username":"Roy","sms":"sa","fto":"sa","fcat":"sa"},{"cid":"12","username":"Charles","sms":"ag","fto":"","fcat":"cat"},{"cid":"13","username":"Clarck","sms":"age","fto":"","fcat":"cat"}]}
Puede alguien ayudarme??? y decirme donde esta el error?
__________________
basqueelectronics.blogspot.com.es
Compartiendo diseños desde la semana pasada

Etiquetas: json, php
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 17:42.