This article has participated in the activity of “New person creation Ceremony”, and started the road of digging gold creation together.

The effect

demand

There is a function on the mobile phone, but I need to use the computer to control when to start this function, and then search on the Internet and find that AndServer can meet the requirements. I just implemented a simple function, if you are interested, you can study it in depth.

The address is github.com/yanzhenjie/…

steps
  1. The introduction of
implementation 'com. Yanzhenjie: andserver: 1.0.2'
Copy the code
  1. To apply for permission
<uses-permission android:name="android.permission.INTERNET" />
Copy the code
  1. Initialize the server
    private void initServer(a) {
        AssetManager assetManager = getAssets();
        WebSite webSite = new AssetsWebsite(assetManager, "");

        AndServer andServer = new AndServer.Build()
                .website(webSite)
                .timeout(30 * 1000)
                .port(1234)
                .registerHandler("login".new loginRequest())
                .registerHandler("search".new searchRequest())
                .listener(mListener)
                .build();
        server = andServer.createServer();
        server.start();
    }
Copy the code
  1. The main code
    /** * listen event */
    private Server.Listener mListener = new Server.Listener() {

        @Override
        public void onStarted(a) {
            Log.e(TAG, "onStarted: ");
            tvTips.setText("Service started successfully");
        }

        @Override
        public void onStopped(a) {
            Log.e(TAG, "onStopped: ");
        }

        @Override
        public void onError(Exception e) {
            Log.e(TAG, "onError: " + e.getMessage());
            tvTips.setText("Service startup failed:"+e.getMessage()); }};public class searchRequest implements RequestHandler{
        @Override
        public void handle(HttpRequest request, HttpResponse response, HttpContext context) throws HttpException, IOException {

           runOnUiThread(new Runnable() {
               @Override
               public void run(a) {
                   Toast.makeText(MainActivity.this."Photos", Toast.LENGTH_SHORT).show(); }}); StringEntity stringEntity =new StringEntity("Photos"."utf-8"); response.setEntity(stringEntity); }}public class loginRequest implements RequestHandler {

        @Override
        public void handle(HttpRequest request, HttpResponse response, HttpContext context) throws HttpException, IOException {

            Map<String, String> params = HttpRequestParser.parse(request);

            // Request params.
            final String userName = params.get("name");
            final String password = params.get("pwd");
            // Chinese needs decoding
            final String userName1 = Uri.decode(userName);
            final String password1 = Uri.decode(password);
            Log.e(TAG, "handle: " + userName);
            runOnUiThread(new Runnable() {
                @Override
                public void run(a) { tvUsername.setText(userName1); tvPwd.setText(password1); }}); StringBuilder sb =new StringBuilder();
            sb.append("Username:" + userName1);
            sb.append("\n");
            sb.append("Password:" + password1);
            StringEntity stringEntity = new StringEntity(sb.toString(), "utf-8"); response.setEntity(stringEntity); }}Copy the code