简体   繁体   中英

How to connect android app to LAMP server

I have created a php server script to be executed with LAMP (Linux, Apache, MySQL Php)

<?php
    // set some variables
    $host = "127.0.1.1";
    $port = 3651;

    // don't timeout!
    set_time_limit(0);

    // create socket
    $socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");

    // bind socket to port
    $result = socket_bind($socket, $host, $port) or die("Could not bind to socket\n");

    while(true) {
        // start listening for connections
        $result = socket_listen($socket, 3) or die("Could not set up socket listener\n");
        echo "after listen\n";
        // accept incoming connections
        // spawn another socket to handle communication

        $spawn = socket_accept($socket) or die("Could not accept incoming connection\n");
        echo "after accept\n";
        $pid = pcntl_fork();
        if ($pid == -1) {
            die('could not fork');
        } else if ($pid) {
            // we are the parent
        } else {
            // we are the child
            // Use $spawn for communication as you see fit
            // exit();
            // read client input
            $input = socket_read($spawn, 1024) or die("Could not read input\n");
            var_dump($input);
            // clean up input string
            //$input = trim($input);
            var_dump(json_decode($input));

            // reverse client input and send back
            $output = strrev($input) . "\n";
            socket_write($spawn, $output, strlen ($output)) or die("Could not write output\n");
            socket_close($spawn);
        }  
    }
    socket_close($socket);
?>

I executed it on terminal using command php test_server.php I checked my private ip using ifconfig as 192.168.0.105 I cannot setup a connection with this file from my android mobile app whose code is as

private String HttpPost(String myUrl) throws IOException {

        URL url = new URL(myUrl);
        Log.e("HTTPpost","url Object created");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        Log.e("HTTPpost","connection opened");
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", "application/json; charset=utf-8");
        setPostRequestContent(conn, objectToBeSent);
        conn.connect();
        Log.e("HTTPpost","connected");
        InputStream is = conn.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder result = new StringBuilder();
        String line;
        while((line = reader.readLine()) != null) {
            Log.e("HTTPpost",line);
            result.append(line);
        }
        return result.toString();
    }
    private void setPostRequestContent(HttpURLConnection conn,
                                       JSONObject jsonObject) throws IOException {
        OutputStream os = conn.getOutputStream();
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
        writer.write(jsonObject.toString());
        Log.i(context.toString(), jsonObject.toString());
        writer.flush();
        writer.close();
        os.close();
    }

where I passed 'myurl' to 'HttpPost' function as http://192.168.0.105/test_server.php:3651 My server output remains after listen and have not shown any sign of socket acceptance Android apps debugging logs are

2020-08-11 22:45:18.833 17839-17978/com.example.testapplication E/HTTPpost: url Object created
2020-08-11 22:45:18.850 17839-17978/com.example.testapplication E/HTTPpost: connection opened

I have set the network permissions as

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

I have also verified that both my mobile and laptop are connected to the same wifi.

Phease help and provide some idea about how to proceed.

The client using port 80 connects with the apache server and your php script is started. The php script is doing nothing with the posted data and is echoing nothing.

So it does not communicate with the client.

The only thing it does is starting a server listening on port 3651. But it only listens as there will never be a client trying to connect to that port.

Your HttpUrlConnection will not do that as it uses port 80.

Impossible setup.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM