PDA

Ver la Versión Completa : ayuda con json y toast al consultar datos


dariciel
06/06/14, 21:08:51
hola buen dia ando realizando una aplicacion de busquedas de usaurio mediante su id de empleado y pues quisiera saber como puedo hacer para mostrar un toast cuando un usuario ingrese un id inecsistente y muestre el toast que no existe, mis datos los tengo almacenado mediante json
este es mi codigo.


public class Busqueda3 extends Activity {

TextView buscarArtista, nombreArtista, modulosArtista, txtview;
ListView listViewModulos;
ListView listView1;
ListView listView2;
Button buscar;

@Override
protected void onCreate(Bundle savedInstanceState) {
final Busqueda3Api api = new Busqueda3Api(getApplicationContext());




super.onCreate(savedInstanceState);
setContentView(R.layout.activity_busqueda3);

buscarArtista = (TextView) findViewById(R.id.buscarArtista3);
nombreArtista = (TextView) findViewById(R.id.nombreArtista3);
txtview = (TextView) findViewById(R.id.textView3);
buscar = (Button) findViewById(R.id.buscar3);
listViewModulos = (ListView) findViewById(R.id.listViewModulos3);




buscar.setOnClickListener(new View.OnClickListener() {




@Override
public void onClick(View v) {
InputMethodManager inputMethodManager = (InputMethodManager)getSystemService(Context.INPUT _METHOD_SERVICE);

inputMethodManager.hideSoftInputFromWindow(buscarA rtista.getWindowToken(), 0);
final Artista artista = api.buscarArtista(buscarArtista.getText().toString ());

nombreArtista.setText(artista.getNombre());






JSONArray jmodulos = artista.getModulos();
try {
int length = jmodulos.length();
JSONObject modulo = null;
List<String> listContents = new ArrayList<String>(length);
List<String> listContent = new ArrayList<String>(length);
List<String> listContentss = new ArrayList<String>(length);
for (int i = 0; i < length; i++)
{

modulo = jmodulos.getJSONObject(i);
listContents.add(modulo.getString("modulo"));
listContent.add(modulo.getString("grupo"));



}

ListView myListView = (ListView) findViewById(R.id.listViewModulos3);
myListView.setAdapter(new ArrayAdapter<String>(Busqueda3.this, R.layout.item1, listContents));

ListView myListView1 = (ListView) findViewById(R.id.listView31);
myListView1.setAdapter(new ArrayAdapter<String>(Busqueda3.this, R.layout.item2, listContent));


}
catch (Exception e) {
e.printStackTrace();
}

}

});

}

}

kriogeN
06/06/14, 21:20:16
Supongo que el JSON lo obtienes de algún WebService mediante REST.

Lo correcto en este caso es que el WebService no te devolviese 200 (OK), sino que en su lugar te devolviese 404 (NOT FOUND). Pero suponiendo que siempre te devuelve 200 indicando que el servicio se ha ejecutado correctamente, en ese caso supongo que con comprobar que el JSON no tiene ningún elemento es suficiente.

Por cierto, te recomiendo que para trabajar con JSON utilices la librería GSON de Google, ya que agiliza muchísimo el trabajo, sobre todo cuando quieres extender el contenido que viene en el JSON. También puedes serializar, es decir, convertir un objeto en un JSON.

NiCH
11/06/14, 13:47:41
Las mismas dudas que kriogeN. (http://www.htcmania.com/member.php?u=381391)

¿Usas alguna librería o tienes algún método que use HttpClient + HttpGet a pelo?

Yo compruebo que el código que devuelve, lo puedes recuperar a pelo por ejemplo:

HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(url_del_ws);
httpget.addHeader("accept", "application/json");
HttpResponse response = httpclient.execute(httpget);
StatusLine httpstat = response.getStatusLine();
int statcode = httpstat.getStatusCode();

Suerte.

Dexafree
12/06/14, 22:05:53
Yo desde que descubrí ION (https://github.com/koush/ion) no he vuelto a montarme un HTTPClient a mano para esto xD

Y respecto a la duda del compi, coincido con kiogeN, lo mas facil es comprobar la longitud del array (en caso de ser array) o si tiene algun elemento