【问题标题】:Why is the value of a private var different from the value of a local var in NanoHTTPD?为什么私有 var 的值与 NanoHTTPD 中本地 var 的值不同?
【发布时间】:2016-10-26 02:36:54
【问题描述】:

在我看来,private String uri 的值和 local var String newURI 的值在任何时候都是一样的。

但其实我测试了很多次,有时候两个值不一样,为什么? NanoHTTPD 发生了什么?

猜猜

我猜函数public Response serve(IHTTPSession session)是进程public class HttpServer extends NanoHTTPD中的多线程, 不同的线程操作这两个值,所以有时 uri 和 newURL 有不同的值,对吧?

public class HttpServer extends NanoHTTPD {

   private String uri;          

   private void GetURLAndParValue(IHTTPSession session){        
        uri = session.getUri(); //I pass the value to private var uri               
   }    


   @Override
    public Response serve(IHTTPSession session) {

        GetURLAndParValue(session);               
        String newURI= session.getUri(); // I pass the value to local newURI       

        MResponseInput mResponseInput=new MResponseInput();  

        Utility.LogError("New0:"+this.uri+" : "+newURI);

        String parDiskPath=session.getParms().get(mContext.getString(R.string.ParDiskIndex));
        String parIsAssets=session.getParms().get(mContext.getString(R.string.ParIsAssets));

        Utility.LogError("New1:"+this.uri+" : "+newURI);

        if (newURI.endsWith("/")){

            if (newURI.length()==1) {

                String homePage = mContext.getString(R.string.WebHomePageHtml);
                mResponseInput= GetMResponseInputByHtmlFile(mContext,homePage);

                String s=HttpHelper.HomePage(mContext);
                return newFixedLengthResponse(s);
            }else{                
                String temp=HttpHelper.ListFolderByName(mContext,session);              
                return newFixedLengthResponse(temp);        
            }
        }else{

            String mimeTypeForFile = getMimeTypeForFile(newURI).trim().toLowerCase();
            Utility.LogError("New2:"+this.uri+" : "+newURI);

            if (parIsAssetsValue!=null&&parIsAssetsValue.equals("1")){                       
                return GetResponseInputByAssetsFile(newURI);
            }

            if (mimeTypeForFile.equals("text/html")){
                String fileName=PublicParFun.RemoveFirstBackslash(newURI).toLowerCase();             
            }

        }

        return newFixedLengthResponse(Response.Status.OK,mResponseInput.mimeTypeForFile,mResponseInput.inputStream, mResponseInput.Size);

    }





}

【问题讨论】:

  • 不确定你在这里问什么。你能澄清一下吗?
  • 为什么private var uri的值有时会和local var newURI的值不同?
  • 为什么private var uri的值有时会和local var newURI的值不同
  • 我相信这个答案也适合你的问题,stackoverflow.com/a/2442540/794088
  • 谢谢!但是 stackoverflow.com/a/2442540/794088 不适合我的问题,我在函数 GetURLAndParValue(IHTTPSession session) 中传递私有字符串 uri,没有任何歧义!

标签: android nanohttpd


【解决方案1】:

那是因为 NanoHTTPD 是多线程的。

每个客户端套接字都会生成一个跟踪特定 IHTTPSession 的线程,并最终调用 NanoHTTPD#serve(IHTTPSession)。

这意味着如果您同时收到 2 个请求,NanoHTTPD#serve(IHTTPSession) 会同时调用两次。 如果您对并发有任何了解,那么您已经知道自己做错了什么。

NanoHTTP 的设计目的是让您只需覆盖 NanoHTTPD#serve(IHTTPSession) 即可让网络服务器正常工作。 您不应该在服务器中存储与会话相关的数据,因为显然服务器很可能在任何给定时间都有多个处于活动状态的会话。

这种工作方式很快会在 v3.0.0 中发生变化,因此变得更加简单。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-02-28
    • 2019-02-19
    • 1970-01-01
    • 1970-01-01
    • 2021-01-11
    • 2015-02-05
    • 2020-03-02
    • 2014-06-20
    相关资源
    最近更新 更多