【发布时间】: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,没有任何歧义!