NanoHTTPD Android 接口 ,充当服务端

public class HttpService extends NanoHTTPD {
    final String SEARCH_IMAGE = "/getImageFrame";
    final String SEARCH_IMAGE_INPUT_STREAM = "/getImageStream";

    public HttpService(int port) {
        super(port);
    }

    @Override
    public Response serve(IHTTPSession session) {
        String response = "";
        Log.e("收到的请求", session.getUri());
        if (session.getMethod() == Method.GET) {
            if(TextUtils.equals(session.getUri(),SEARCH_IMAGE_INPUT_STREAM)){
                try{
                    FileInputStream fis = null;
                    File file = new File("");
                    if (file.exists()){
                        fis = new FileInputStream(file);
                        Log.d("", "File exists: " + file.getAbsolutePath());
                    } else {
                        Log.d("", "File doesn't exist!");
                    }
                    return Response.newChunkedResponse(Status.OK, "image/jpeg", fis);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
            response = doGetRequest(session);
        }
        Log.e("返回结果", response);
        return Response.newFixedLengthResponse(Status.OK, "application/json;charset=UTF-8", response);
    }

    public void startServer() {
        try {

            this.start();
            Log.e("HttpService", "start");
        } catch (IOException e) {
            e.printStackTrace();
            Log.e("HttpService", "start fail");
        }
    }

    public void stopServer() {
        this.stop();
        Log.e("HttpService", "stop");
    }

    public String doGetRequest(IHTTPSession session) {
        switch (session.getUri()) {
            case SEARCH_IMAGE:
                return getImage(session);
            default:
                break;
        }
        return "";
    }


    private String getImage(IHTTPSession session) {
        JSONObject object = new JSONObject();
        try {
            object.put("code", 0);
            object.put("msg", "成功");
            object.put("data", MyApplication.getApplication().getMCurrentImageFrameBase64().getValue());

        } catch (JSONException e) {
            e.printStackTrace();
        }
        return object.toString();
    }



    public Map<String, String> getGetParam(String str) {
        Map<String, String> map = new HashMap<>();
        if (TextUtils.isEmpty(str)) {
            return map;
        }
        String[] agrs = str.split("&");
        for (String it : agrs) {
            String[] s = it.split("=");
            map.put(s[0], s[1]);
        }
        return map;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值