M. Erfan Mowlaei
M. Erfan Mowlaei

Reputation: 1436

How to send data through GET response as a android server

There's a scenario which I need to be server as android phone and a module connects to me and requests data through GET method (TCP/IP connection). The part of connecting is easy, my problem is that how I should handle this GET method?

class AsyncServerThread extends AsyncTask<Void, Void, Void> {

    @Override
    protected Void doInBackground(Void... voids) {
        Socket socket = null;
        try {
            serverSocket = new ServerSocket(SERVERPORT);
            Log.e("ServerThread", "Connected");
        } catch (final IOException e) {
            Log.e("serverSocketExc", "?" + e.getMessage());
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(getApplicationContext(), "serverSocketExc: " +
                                    "?" + e.getMessage(),
                            Toast.LENGTH_LONG).show();
                }
            });
            e.printStackTrace();
        }
        Log.e("ServerThread", "listening for new connections");
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(getApplicationContext(), "ServerThread: listening for new connections.",
                        Toast.LENGTH_LONG).show();
            }
        });
        while (shallContinue) {

            try {

                final Socket tempSocket = socket = serverSocket.accept();

                if (tempSocket.isConnected()) {
                    Log.e("socket", "accepted new Connection");
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(getApplicationContext(), "accepted new Connection.",
                                    Toast.LENGTH_LONG).show();
                        }
                    });

                    /*CommunicationThread commThread = new CommunicationThread(socket);
                    new Thread(commThread).start();*/
                    Log.e("AsyncServerThread", "going to create client thread");
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            new AsyncClientThread().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, tempSocket);
                        }
                    });

                    out = new PrintWriter(new BufferedWriter(
                            new OutputStreamWriter(socket.getOutputStream())),
                            true);
                } else {
                    Log.e("socket", "no new Connection");
                }

            } catch (IOException e) {
                Log.e("accept", "?" + e.getMessage());
                e.printStackTrace();
            }
        }
        Log.e("ServerThread", "ended listening for new connection");
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(getApplicationContext(), "ServerThread: ended listening for new connection.",
                        Toast.LENGTH_LONG).show();
            }
        });
        try {
            serverSocket.close();
        } catch (IOException e) {
            Log.e("close", "?" + e.getMessage());
            e.printStackTrace();
        }
        return null;
    }
}

Upvotes: 0

Views: 165

Answers (1)

dsharew
dsharew

Reputation: 10665

The words GET, POST, PUT..etc are found on the HTTP Protocol world.
But you created a server socket not http server ( There is a difference between creating a low level server socket and http server socket).

So I had a similar scenario before few days and I used nanohttpd. It is an elegant library that allows you to run tiny httpd web server on your android device.

Sample code:

  public class HttpWebServer extends NanoHTTPD {

    int port;

    public HttpWebServer(int port) throws IOException {
        super(port);
        this.port = port;
    }

    public void startServer() throws IOException{

        start();
        Log.d(LOG_TAG, "visit http://localhost:" + port );

    }

    @Override
    public Response serve(IHTTPSession session) {


        Log.d(LOG_TAG, "Got http request.");

        //gets method type; GET, POST, PUT..etc
        Method method = ((HTTPSession) session).method


    }


}

How to add dependency?

dependencies {
    compile 'com.nanohttpd:nanohttpd-webserver:2.1.1'
}

Upvotes: 2

Related Questions