I decided to study java and the creation of Android applications on it. So, Reg activ with all sorts of conditions:

public class Reg extends Activity{ TextView InfoText; Button Reg; Button Cancel; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.lregistration); InfoText = (TextView) findViewById(R.id.InfoText);//Π˜Π½Ρ„ΠΎΡ€ΠΌΠΈΡ€ΡƒΡŽΡ‰ΠΈΠΉ тСкст Reg = (Button) findViewById(R.id.reg);//Кнопка рСгистрации Cancel = (Button) findViewById(R.id.Cancel);//Назад OnClickListener Click = new OnClickListener() { public void onClick(View v){ switch (v.getId()) { case R.id.reg: /*Код ΠΏΡ€ΠΈ Π½Π°ΠΆΠ°Ρ‚ΠΈΠΈ ΠΊΠ½ΠΎΠΏΠΊΠΈ РСгистрация*/ /*--------------------------------------------------------------*/ ` 

CONservice.getCon ();

  InfoText.setText("olol"); /*--------------------------------------------------------------*/ break; case R.id.Cancel: /*Код ΠΏΡ€ΠΈ Π½Π°ΠΆΠ°Ρ‚ΠΈΠΈ ΠΊΠ½ΠΎΠΏΠΊΠΈ Назад*/ break; }}}; Reg.setOnClickListener(Click); }; }; 

File connecter: CONservice.java:

 package service; import java.net.Socket; import java.net.UnknownHostException; import java.io.*; public class CONservice { static AffableThread mSecondThread; public static void main(String[] args) { getCon(); } public static void getCon(){ mSecondThread = new AffableThread(); mSecondThread.start(); } } class AffableThread extends Thread { @Override public void run() { try{ Socket socket = new Socket("127.0.0.1", 5555); }catch(UnknownHostException e){}catch(IOException e){} } } 

That is, there is a certain screen with a button, and when you click on it, an attempt should be made to connect to the server and display the text "olol" on the screen. The server is running and waiting for connections, we launch the application through the emulsion, press the button - and voila - there is no connection, no errors, and only a vague olol inscription. It seems like not a psychic guessing that there is in the trunk. Help, good people))

  • Permission to use Internet added? - Vladyslav Matviienko
  • @metalurgus added ACCESS_NETWORK_STATE, CHANGE_NETWORK_STATE, INTERNET in the manifest. The same is the same - VladEv1L
  • so wait, you are trying to connect to 127.0.0.1-5555, that is, to port 5555 of the emulator itself. Do you listen to something on the same emulator on this port? Does he have where to connect? - Vladyslav Matviienko February
  • @metalurgus A local server with port 5555 was launched. When connected, it displays the console! Client !. The server is running, waiting for connections, verified. - VladEv1L
  • @ VladEv1L, where is the local server running? on a computer, or on an Android emulator? - Vladyslav Matviienko

1 answer 1

The emulator, as well as any device with network support, acts as a localhost for itself and has the address 127.0.0.1. You run the server on your computer. Think logically. The emulator is a virtual machine. What happens if a virtual machine is installed, for example, Windows, and request the address 127.0.0.1 on it? Do you really think that she will knock on the host OS? No, she will request her own IP. The same is the Android emulator.

  • Poshamanil, in the end I got what I wanted, it all worked - VladEv1L