-1

I have two questions,

First, I am running an Android emulator that connects to Python server to download images from it. For some reason,the socket is not connecting to remote server. I am using IP address of the server and port which the server is listening to. If someone can help me, that will be great.

EDIT: I also exported the apk file and installed the app in my phone. It still does not work.

My code:

ip = InetAddress.getLocalHost();
InetAddress serverAddr = InetAddress.getByName("56.249.79.139");
int port2ConnectDefault = 8002;
Socket socket;
BufferedReader in;
PrintWriter out;
//System.out.print(ip.getHostAddress());
Log.v("Check_insert", "got in");
socket = new Socket(serverAddr, port2ConnectDefault);
Log.v("Check_insert", "got in2");
System.out.println("Connected to server...sending echo string");

I get "Check_insert", "got in" log but not "Check_insert", "got in2.

Second, Is there any way other then using sockets to have Android client download image from python server using IP address and port number ? I looked at okhttp but I could not figure out how this would work out in the python side.

Thanks a bunch!

Student
  • 39
  • 5
  • `I am running an Android emulator that connects to Python server `. Dont think so. Never saw an emulator trying to do such things. – greenapps Oct 30 '17 at 22:10
  • So an emulator can not connect to python server? – Student Oct 30 '17 at 22:20
  • because that is what I am having trouble with. – Student Oct 30 '17 at 22:44
  • I think your client app cannot connect to a server. – greenapps Oct 30 '17 at 23:10
  • why would it matter that you are connecting to a python server? What are you trying to do? Can an HTTP server work for you application? okhttp sounds like the way to go. You just need to setup an HTTP server. It could be a python HTTP server. – Jon Nov 08 '17 at 17:04
  • The Android client is connecting now after I did some port forwarding for emulator and restarted the python server. Thanks! – Student Nov 11 '17 at 08:11

1 Answers1

0

Since you didn't mention AsynTask or Thread, I think your first problem was probably the same as mine. Details is available here. In summary, it's because you were performing networking operations on the main thread, which is not allowed for later Android Versions. If you check the exception thrown from socket = new Socket(serverAddr, port2ConnectDefault);, it should say "android.os.NetworkOnMainThreadException". You can use Thread or AsyncTask to solve this problem. For example,

public class MainActivity extends AppCompatActivity {
    Socket  socket;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    socket = new Socket(ServerIP, Port);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}

PS: Android emulator can also connect to (remote) python server. Hope it helps.

Jie HE
  • 173
  • 1
  • 8