Programación y Desarrollo para Android Subforo exclusivo para temas de programación de software para PDAs y desarrollo de aplicaciones, interfaces, etc bajo Android


 
Herramientas
  #1  
Viejo 06/06/14, 21:08:51
Avatar de dariciel
dariciel dariciel no está en línea
Usuario novato en la web
Mensajes: 1
 
Fecha de registro: nov 2013
Mensajes: 1
Modelo de smartphone: lg optimus l9
Tu operador: Movistar
Mencionado: 0 comentarios
Tagged: 0 hilos
Post ayuda con json y toast al consultar datos

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();
}

}

});

}

}
Responder Con Cita


  #2  
Viejo 06/06/14, 21:20:16
Avatar de kriogeN
kriogeN kriogeN no está en línea
Colaborador/a
Mensajes: 4,637
Compra y venta: (1)
 
Fecha de registro: oct 2010
Localización: Murcia
Mensajes: 4,637
Modelo de smartphone: Samsung Galaxy S7 Edge SM-G935F
Versión de ROM: CM13 - CM 11
Tu operador: Vodafone
Mencionado: 60 comentarios
Tagged: 3 hilos
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.
Responder Con Cita
  #3  
Viejo 11/06/14, 13:47:41
Avatar de NiCH
NiCH NiCH no está en línea
Miembro del foro
Mensajes: 55
 
Fecha de registro: feb 2011
Localización: Galicia
Mensajes: 55
Modelo de smartphone: Nexus 4
Versión de ROM: Stock ROM 4.4.3
Tu operador: Pepephone
Mencionado: 2 comentarios
Tagged: 0 hilos
Las mismas dudas que kriogeN.

¿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.
Responder Con Cita
  #4  
Viejo 12/06/14, 22:05:53
Avatar de Dexafree
Dexafree Dexafree no está en línea
Mr. FAQMan
Mensajes: 8,021
Compra y venta: (1)
 
Fecha de registro: dic 2008
Mensajes: 8,021
Modelo de smartphone: Samsung Galaxy S i9000 + Galaxy Tab 10.1 WiFi
Versión de ROM: Android 4.1.1 Jelly Bean
Versión de Radio: KF1
Tu operador: Movistar
Mencionado: 65 comentarios
Tagged: 2 hilos
Yo desde que descubrí ION (https://github.com/koush/ion) no he vuelto a montarme un HTTPClient a mano para esto

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
Responder Con Cita
Respuesta

Estás aquí
Regresar   HTCMania > Todo sobre Android > Programación y Desarrollo para Android


Reglas de Mensajes
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Las caritas están On
Código [IMG] está On
Código HTML está Off

Saltar a Foro



Hora actual: 20:48:46 (GMT +1)

Cookies settings
Powered by vBulletin™
Copyright © vBulletin Solutions, Inc. All rights reserved.
 
HTCMania: líderes desde el 2007