android.os.NetworkOnMainThreadException json android -
this question has answer here:
in below code got error when running android project sending json cloudant.com second layout, quiero mandar json desde android en un segundo layout pero me manda el error android.os.networkonmainthreadexception desde el logcat
//obtenemos una referencia al locationmanager locmanager = (locationmanager)getsystemservice(context.location_service); //obtenemos la última posición conocida final location loc = locmanager.getlastknownlocation(locationmanager.gps_provider); // buton ejecuta btnactualizar.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { // comenzarlocalizacion(); httppost request = new httppost( "https://tonatiuhnava.cloudant.com/android/" ); request.addheader( "content-type", "application/json" ); request.addheader( "authorization", "basic " + base64.encodetostring( "tonatiuhnava:70n471uh".getbytes(), base64.no_wrap)); jsonobject cadena = new jsonobject(); if(loc != null) { try{ cadena.put("nombre", "tonatiuhnava");//le asignamos los datos que necesitemos cadena.put("category", new integer(01)); cadena.put("event", new integer(201)); cadena.put("fecha",curtime); //cadena.put("fecha", currentdate.togmtstring()); cadena.put("longitud", new double(loc.getlongitude())); cadena.put("latitud", new double(loc.getlatitude())); } catch ( jsonexception e ) { log.d( "wazaaa" , "error json" ); } try{ request.setentity( new stringentity( cadena.tostring() ) ); } catch ( exception e ){ log.d( "wazaaa" , "error entity" ); } defaulthttpclient connection = new defaulthttpclient(); try{ log.d( "wazaaa" , "haciendo request"); httpresponse execute = connection.execute( request ); log.d( "wazaaa" , "request hecho"); byte[] bytes = new byte[1000]; int numread = 0; stringbuilder x = new stringbuilder(); inputstream stream = execute.getentity().getcontent(); stream.read( bytes ); x.append( new string( bytes, 0, numread ) ); log.d( "wazaaa" , execute.getstatusline().getreasonphrase() ); } catch (clientprotocolexception e) { // todo auto-generated catch block log.e( "wazaaa" , "client error" ); e.printstacktrace(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); log.e( "internet please :..(" , "io error" ); } log.d( "wazaaa" , "fin"); // termina if }else { context context = getapplicationcontext(); charsequence text = "espere que cargue coordenadas..."; int duration = toast.length_short; toast toast = toast.maketext(context, text, duration); toast.show(); } }
blocking requests can not run on ui thread, should run on separate thread. use asynctask. read this.
Comments
Post a Comment