json - Intent inside AsyncTask (Not accessible in scope) -
first of all, want apologize bad english.
i'm using asynctask class, called main_activity, , when in postexecute call de intent, error "not enclosing instance type accesible in scope"
package com.example.pfc; import android.os.bundle; import android.app.activity;`enter code here` import android.content.intent; import android.view.menu; import android.view.view; import android.widget.button; import android.widget.textview; public class principal extends activity { private button botonsalir; private button botonatras; private button botonclientes; private button botonmaquinas; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_principal); botonsalir = (button) findviewbyid(r.id.botonsalir); botonatras = (button) findviewbyid(r.id.botonatras); botonclientes = (button) findviewbyid(r.id.botonclientes); botonmaquinas = (button) findviewbyid(r.id.botonmaquinas); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.activity_principal, menu); return true; } public void atras(view view){ finish(); } public void listaclientes(view view){ sendclientes send = new sendclientes(); send.execute("clientes"); } public void listamaquinas(view view){ sendmaquinas send = new sendmaquinas(); send.execute("servidores"); } }
and asynctask "send":
package com.example.pfc; import java.io.ioexception; import java.util.list; import java.util.concurrent.timeoutexception; import org.json.jsonexception; import org.json.jsonobject; import android.content.intent; import android.os.asynctask; import com.rabbitmq.client.amqp; import com.rabbitmq.client.address; import com.rabbitmq.client.basicproperties; import com.rabbitmq.client.channel; import com.rabbitmq.client.connection; import com.rabbitmq.client.connectionfactory; import com.rabbitmq.client.consumercancelledexception; import com.rabbitmq.client.queueingconsumer; import com.rabbitmq.client.rpcclient; import com.rabbitmq.client.shutdownsignalexception; public class sendclientes extends asynctask <string,void,void> { private string requestqueuename = "rpc_queue"; private string replyqueuename; private queueingconsumer consumer; protected void doinbackground(string... p) { string mensaje = p[0]; connectionfactory cfactory = new connectionfactory(); connection connection; //json object jsonobject json = new jsonobject(); try { json.put("request", mensaje); } catch (jsonexception e) { // todo auto-generated catch block e.printstacktrace(); } //conexion try { address[] addrs= new address[1]; addrs[0] = address.parseaddress("84.126.242.244"); connection = cfactory.newconnection(addrs); cfactory.setport(5672); channel channel = connection.createchannel(); replyqueuename = channel.queuedeclare().getqueue(); consumer = new queueingconsumer(channel); channel.basicconsume(replyqueuename, true,consumer); string corrid = java.util.uuid.randomuuid().tostring(); //propiedades enviadas al servidor amqp.basicproperties props = new amqp.basicproperties() .builder() .correlationid(corrid) .replyto(replyqueuename) .build(); channel.basicpublish("", replyqueuename, props, json.tostring().getbytes()); //esperando respuesta del servidor while (true) { queueingconsumer.delivery delivery = consumer.nextdelivery(); if (delivery.getproperties().getcorrelationid().equals(corrid)) { string respuesta = new string(delivery.getbody()); break; } } //eliminamos la cola del servidor, tras haber recibido el mensaje channel.queuedelete(replyqueuename); //cerramos canal y conexion channel.close(); connection.close(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (shutdownsignalexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (consumercancelledexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (interruptedexception e) { // todo auto-generated catch block e.printstacktrace(); } // todo auto-generated method stub return null; } //capturamos la respuesta en el posexecute protected void onpostexecute(string respuesta) { //lanzamos el intent la actividad de usuarios, poniendo en el intent la respuesta del servidor. intent intent = new intent(principal.this, activity_usuarios.class); //error "no enclosing instance of type principal accessible in scope" }
}
thanks in advance.
bye!
i think providing wrong context in intent. suggest make sendclientes asynctask private within principal activity. or create class has sendclientes inner private class, , provide context through constructor (if want principal , sendclientes classes seperated).
similar problem: no enclosing instance of type mainactivity accessible in scope
Comments
Post a Comment