title: RabbitMQ/Android - how to setup a connection?
body: <p>I'm trying to make an Android application which would consist of communication (simply passing a fixed message) between devices using message brokers (I chose RabbitMQ). For a couple of days I've been struggling with setting up the connection. The client side is not ready yet, I'd like to make my server work firstly.</p>
  
  <p>Here is a piece of my code:</p>
  
  <pre><code>public class HomeView extends Fragment {
  ImageButton imageButton;
  private String mText;
  private View mContent;
  
  public HomeView() { }
  
  @Override
  public void onCreate(@Nullable Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
  }
  
  @Nullable
  @Override
  public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
      mContent = inflater.inflate(R.layout.home_view, container, false);
      configureImageButton();
      setupServer();
      return mContent;
  }
  
  private void configureImageButton() {
      imageButton = (ImageButton) getmContent().findViewById(R.id.imageButton);
      ImageButton.OnClickListener listener = new ImageButton.OnClickListener() {
          @Override
          public void onClick(View v) {
              Toast.makeText(getActivity(), "message", Toast.LENGTH_SHORT).show();
              // message to server will be sent here
          }
      };
      imageButton.setOnClickListener(listener);
  }
  
  Thread setupServerThread;
  
  ConnectionFactory factory = new ConnectionFactory();
  void setupServer() {
      setupServerThread = new Thread(new Runnable() {
          @Override
          public void run() {
              while(true) {
                  try {
                      Connection connection = null;
                      factory.setUri("amqp://guest:guest@localhost:5672/");
                      connection = factory.newConnection();
                      final Channel channel = connection.createChannel();
                      channel.queueDeclare(RPC_QUEUE_NAME, true, false, false, null);
                      channel.basicQos(1);
  
                      Consumer consumer = new DefaultConsumer(channel) {
                          @Override
                          public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                              AMQP.BasicProperties replyProps = new AMQP.BasicProperties
                                      .Builder()
                                      .correlationId(properties.getCorrelationId())
                                      .build();
  
                              String response = "";
  
                              try {
                                  String message = new String(body,"UTF-8");
                                  int n = Integer.parseInt(message);
                                  response += n.toString());
                              }
                              catch (RuntimeException e){
                                  e.printStackTrace();
                              }
                              finally {
                                  channel.basicPublish( "", properties.getReplyTo(), replyProps, response.getBytes("UTF-8"));
                                  channel.basicAck(envelope.getDeliveryTag(), false);
                              }
                          }
                      };
  
                      channel.basicConsume(RPC_QUEUE_NAME, false, consumer);
  
                      while(true) {
                          try {
                              Thread.sleep(100);
                          } catch (InterruptedException _ignore) {}
                      }
                  } catch (Exception e1) {
                      e1.printStackTrace();
                      try {
                          Thread.sleep(5000);
                      } catch (InterruptedException e) {
                          break;
                      }
                  }
              }
          }
      });
      setupServerThread.start();
  }
  }
  </code></pre>
  
  <p>The problem is that after this line:</p>
  
  <pre><code>connection = factory.newConnection();
  </code></pre>
  
  <p>the connection is null and the "Connection refused" Exception is thrown. 
  When I ran the same code as a simple Java program, everything worked fine. </p>
  
  <p>Please take a look, I would very very much appreciate any kind of help!</p>