当前位置:   article > 正文

Android 本地客户端搭建小型服务器_android 本地服务器

android 本地服务器

Android 搭建Http服务器,接收各端的 HTTP 请求访问以及 挂载HTML网页

  • 依赖库
implementation 'org.nanohttpd:nanohttpd:2.3.1'
  • 1
  • 服务器实现
public class HttpServer extends NanoHTTPD {
private static final int HTTP_PORT = 8080;//端口,尽量设置一个不常规的
  HttpServer() {
        super(HTTP_PORT); 
        }
    }
 /**
     * 数据接收
     *
     * @param session
     * @return
     */
    @Override
    public Response serve(IHTTPSession session) {
        return parseRequest(session);
    }



    /**
     * 数据解析
     * @param session
     * @return
     */
    private NanoHTTPD.Response parseRequest(NanoHTTPD.IHTTPSession session) {
        NanoHTTPD.Response response = null;
        if (session.getMethod() == NanoHTTPD.Method.GET) {
            response = parseGetRequest(session);
        } else if (session.getMethod() == NanoHTTPD.Method.POST) {
            response = parsePostRequest(session);
        } else {
            response = responseForNotFound();
        }
        return response;
    }


 /**
     * 解析post请求
     *  /xxx/接口1:代表 两端定义的访问接口,例如:/user/info
     * @param session
     * @return
     */
    private Response parsePostRequest(IHTTPSession session) {
        final Map<String, String> files = parseBody(session);
        NanoHTTPD.Response response = null;
        String paramsMiss = "";
        Map<String, String> params = session.getParms();
        if (session.getParms() == null) {
            return responseParamsNotFound(ResourceUtil.getString(R.string.server_params_error));
        }
        switch (session.getUri()) {
            case "/xxx/接口1":
         
              //根据自己业务返回不同的需求
                response = responseForConfig(HttpResult.getConfigResult());
                break;
            case "/xxx/接口2":
               response=responseForConfig(HttpResult.getNetConfig(mEthernetHelper));
                break;
            default:
                response = responseForNotFound();
                break;
        }
        return response;
    }


   /**
     * 返回指令未知
     *
     * @return
     */
    private NanoHTTPD.Response responseForNotFound() {
        NanoHTTPD.Response.Status status = NanoHTTPD.Response.Status.NOT_FOUND;
        final String mimeType = "text/html";
        return NanoHTTPD.newFixedLengthResponse(status, mimeType, "NOT_FOUND");
    }

/**
*返回指定的错误信息
*/
rivate NanoHTTPD.Response responseParamsNotFound(String msg) {
        NanoHTTPD.Response.Status status = NanoHTTPD.Response.Status.NOT_FOUND;
        final String mimeType = "text/html";
        return NanoHTTPD.newFixedLengthResponse(status, mimeType, msg);
    }


/**
     *返回json串 
     *result:可以是任意Bean转成的字符串,
     * @return
     */
    private Response responseForConfig(String result) {
        NanoHTTPD.Response.Status status = NanoHTTPD.Response.Status.OK;
        String mimeType = "application/json;charset=UTF-8";
        LogHelper.v(TAG, "正在向客户端发送数据 -> " + result);
        return NanoHTTPD.newFixedLengthResponse(status, mimeType, result);
    }



  /**
     * 解析get 请求
     *
     * @param session
     * @return
     */
    private Response parseGetRequest(IHTTPSession session) {
        return getHtmlFileStream(session);
    }

/**
     * 解析访问的mintype类型
     * @param session
     * @return
     */
    private Response getHtmlFileStream(IHTTPSession session) {
        String uri = session.getUri();
        String filename = uri.substring(1);
        boolean is_ascii = true;
        if (uri.equals("/")) {
            filename = "index.html";
        }

        String mimetype = "text/html";
        if (filename.contains(".html") || filename.contains(".htm")) {
            mimetype = "text/html";
            is_ascii = true;
        } else if (filename.contains(".js")) {
            mimetype = "text/javascript";
            is_ascii = true;
        } else if (filename.contains(".css")) {
            mimetype = "text/css";
            is_ascii = true;
        } else if (filename.contains(".gif")) {
            mimetype = "text/gif";
            is_ascii = false;
        } else if (filename.contains(".jpeg") || filename.contains(".jpg")) {
            mimetype = "text/jpeg";
            is_ascii = false;
        } else if (filename.contains(".png")) {
            mimetype = "image/png";
            is_ascii = false;
        } else if (filename.contains(".svg")) {
            mimetype = "image/svg+xml";
            is_ascii = false;
        } else {
            filename = "index.html";
            mimetype = "text/html";
        }
        if (is_ascii) {
            return loadHtml(filename, mimetype);
        } else {
            return loadOrder(filename, mimetype);
        }


    }


   /**
     * 加载非html 类型
     *
     * @param filename
     * @param mimetype
     * @return
     */
    private Response loadOrder(String filename, String mimetype) {
        AssetManager assetManager = BasicApplication.getAppContext().getAssets();
        InputStream isr;
        try {
            isr = assetManager.open(filename);
            return newFixedLengthResponse(Response.Status.OK, mimetype, isr, isr.available());
        } catch (IOException e) {
            e.printStackTrace();
            return newFixedLengthResponse(Response.Status.OK, mimetype, "");
        }
    }


 /**
     * 加载html
     *
     * @param filename
     * @param mimetype
     * @return
     */
    private Response loadHtml(String filename, String mimetype) {
        AssetManager assetManager = BasicApplication.getAppContext().getAssets();
        //通过AssetManager直接打开文件进行读取操作
        StringBuilder response = new StringBuilder();
        String line = "";
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new InputStreamReader(assetManager.open(filename, AssetManager.ACCESS_BUFFER)));

            while ((line = reader.readLine()) != null) {
                response.append(line);
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
            return newFixedLengthResponse(Response.Status.OK, mimetype, "");
        }
        return newFixedLengthResponse(Response.Status.OK, mimetype, response.toString());

    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211

到此,一个既有post请求也有 HTML网页访问 的HTTP服务器 搭建完毕。

  • 返回Result
   /**
     * 此类属于完全自定义,根据自己业务写
   
     */
public class HttpResult<T> {

    private static final int RESULT_CODE_SUCCESS = 0;
    private static final int RESULT_CODE_FILE = 1;
    /**
     * 状态码
     */
    public int code;
    /**
     * 返回结果信息
     */
    public String msg;

    /**
     * 预留
     */
    /**
     * 结果信息
     **/
    public T results;


    /**
     * 获取成功
     *
     * @return
     */
    public static final String getSuccess() {
        final HttpResult httpResult = new HttpResult<>();
        httpResult.code = RESULT_CODE_SUCCESS;
        httpResult.msg = ResourceUtil.getString(
                R.string.server_result_success);
        return JsonUtil.objectToJson(httpResult);
    }

   /**
     * 请求失败
     *
     * @return
     */
    public static String getFail(String paramsMiss) {
        final HttpResult httpResult = new HttpResult<>();
        httpResult.code = RESULT_CODE_FILE;
        httpResult.msg = ResourceUtil.getString(
                R.string.server_result_fail) + (TextUtils.isEmpty(paramsMiss) ? "" : ("," + paramsMiss));
        return JsonUtil.objectToJson(httpResult);
    }

}



  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 启动服务
    一般来说,NanoHttp 服务器在Android 上最好放在Service 上去启动或停止 如下:
ublic class HttpService extends Service {

    private HttpServer httpServer;

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        httpServer = new HttpServer();
        try {
            httpServer.start(30000);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onDestroy() {
        if (httpServer != null) {
            httpServer.stop();
        }
        super.onDestroy();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return Service.START_STICKY;
    }

    /**
     * 启动服务
     *
     * @param context
     */
    public static final void startService(Context context) {
        Intent intent = new Intent(context,
                HttpService.class);
        context.startService(intent);
    }


    /**
     * 关闭服务
     */
    public static void stopService(Context context) {
        Intent intent = new Intent(context, HttpService.class);
        context.stopService(intent);
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53

以上就是关于一个简单的NanoHTTP 搭建的场景

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/喵喵爱编程/article/detail/780074
推荐阅读
相关标签
  

闽ICP备14008679号