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:
Puede alguien ayudarme??? y decirme donde esta el error? {"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"}]}