- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在 My.htm 中,我将数组变量 mytemp 发布到服务器端,我希望在服务器端检索数组变量。
如果我使用以下代码,我只会得到一个字符串 D 1,D,2,Tom'Dog,我如何在服务器端检索数组?谢谢!
顺便说一句,我希望做一个完整的帖子,而不是 ajax,所以我认为 $.ajax() 不适合。
My.htm
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script src="js/jquery1.10.2.min.js?isassets=1" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
document.querySelector('#myformID').addEventListener('submit', event => {
if (!confirm('Do you want to delete selected images?')) {
event.preventDefault();
}
});
$("#myformID").submit( function(eventObj) {
var mytemp=new Array();
mytemp.push("D 1");
mytemp.push("D,2");
mytemp.push("Tome'Dog");
$('<input />').attr('type', 'hidden')
.attr('name', "myCw")
.attr('value',mytemp)
.appendTo('#myformID');
return true;
});
});
</script>
</head>
<body>
<div id="container">
<form action='' method='post' enctype='multipart/form-data' id="myformID">
<input type='file' name='myfilename' /> Please select a file
<input type='submit'name='submit' value='Delete Selecetd Items' />
</form>
</div>
</body>
</html>
服务器端
public class HttpServer extends NanoHTTPD {
private Context mContext;
public HttpServer(Context myContext) throws IOException {
super(PublicParFun.GetWebPort(myContext));
this.mContext=myContext;
start(NanoHTTPD.SOCKET_READ_TIMEOUT);
}
private Response POST(IHTTPSession session) {
Utility.LogError("Handle Post");
try {
Map<String, String> files = new HashMap<String, String>();
session.parseBody(files);
Set<String> keys1 =session.getParms().keySet() ;
for(String key: keys1){
String name = key;
String loaction =session.getParms().get(key);
}
} catch (Exception e) {
Utility.LogError("This is an error "+e.getMessage() );
System.out.println("i am error file upload post ");
e.printStackTrace();
}
return newFixedLengthResponse(Response.Status.OK, NanoHTTPD.MIME_PLAINTEXT,"I'm Hello!");
}
@Override
public Response serve(IHTTPSession session) {
String uri = session.getUri();
Method method = session.getMethod();
MURLPar mURLPar=new MURLPar(mContext);
FileFolderHelper.SetMURLParValue(mURLPar,session);
if (mURLPar.isassets.value!=null && mURLPar.isassets.value.equals("1")){
return GetResponseInputByAssetsFile(uri);
}
if (Method.POST.equals(method)) {
return POST(session);
}
String s=FileFolderHelper.GetStringTemplate(mContext,mContext.getString(R.string.WebImageBase));
return newFixedLengthResponse(s);
}
}
致 Erfan Mowlaei:谢谢!下面的代码可以吗?
客户端:
var mytemp=new Array();
mytemp.push("D 1");
mytemp.push("D, 2");
mytemp.push("D'3");
$("#myformID").submit( function(eventObj) {
$('<input />').attr('type', 'hidden')
.attr('name', "myCw")
.attr('value', JSON.stringify(mytemp))
.appendTo('#myformID');
return true;
});
服务器端
public ArrayList<String> jsonStringToArray(String jsonString) throws JSONException {
ArrayList<String> stringArray = new ArrayList<String>();
JSONArray jsonArray = new JSONArray(jsonString);
for (int i = 0; i < jsonArray.length(); i++) {
stringArray.add(jsonArray.getString(i));
}
return stringArray;
}
最佳答案
我假设您得到的是 JsonObject,或者您应该发送 JsonObject 以便能够获取它并将其转换为服务器端的数组。如果你以 JsonArray 形式发送数据,你可以在 NanoServer 中像这样获取它:
@Override
public Response serve(IHTTPSession session) throws JSONException {
//Log.e("Tag", "request received!");
Map<String, String> files = new HashMap<String, String>();
Method method = session.getMethod();
if (Method.POST.equals(method) || Method.PUT.equals(method)) {
try {
session.parseBody(files);
} catch (IOException ioe) {
try {
return new Response(Response.Status.INTERNAL_ERROR, MIME_PLAINTEXT, "SERVER INTERNAL ERROR: IOException: " + ioe.getMessage());
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
Log.e("UnsupportedEncodingException", e.getMessage());
}
} catch (ResponseException re) {
try {
return new Response(re.getStatus(), MIME_PLAINTEXT, re.getMessage());
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
Log.e("UnsupportedEncodingException", re.getMessage());
}
}
}
final JSONObject json = new JSONObject(files.get("postData"));
//Log.e("MyData", json.toString());
responseInterface.OnResponse(json); // my interface to pass data to other classes
return newFixedLengthResponse("Success!");
}
然后稍后在 responseInterface.OnResponse(JsonObject json);
中,您可以解析该数据并从中创建一个数组。
关于android - 当我在客户端发布数组 var 时,如何在 Android 中使用 NanoHTTPD 在服务器端检索数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40623211/
在 index.html 中,使用外部 css 和图像 src 的路径用于从文件夹请求 css 图像。然而,图像未加载且 css 样式未应用于页面。 import java.io.*; import
如何确定发起请求的 IP 地址?我当前的代码: @Override public Response serve(String uri, Method method, Map headers,
我正在关注this post因为我想用 NanoHTTPD 处理 POST 请求。当我将其放入 Android Studio 中的 MainActivity 类中时,出现错误: 'Response(f
我尝试使用 NanoHTTPD 服务器 在浏览器上显示图像,但始终没有显示任何内容。这是我的服务方法的一部分: else if(uri.contains(".png")){ Small
使用 nanohttpd,我创建了一个网页,上面有按钮,单击按钮时,我使用不同的 URI 对 nanohttpd 进行 ajax get 调用。 屏幕上的不同按钮发送不同的参数值。 但在服务器停止看到
我看了很多帖子,但找不到我的问题的答案...... 所以我可以在我的设备上启动网络服务器,当我尝试上传文件时,浏览器显示“上传成功”,但我在我的设备上找不到该文件,我不知道它是否已上传到设备.我已经设
我玩的是NanoHTTPD和基于它的WebServer。要更新我的代码(应用程序)中的任何对象,我可以使用 GET/POST 方法。但是我怎样才能创建动态页面呢?例如,我在光盘上有 html 页面,它
这个问题真的非常愚蠢,因为我没有使用 Maven 的经验。 我对 NanoHTTPD 作为项目中的嵌入式 Web 服务器感兴趣,但是在下载源代码并对其进行测试后,我收到以下错误: 无法在项目 nano
我使用下面的代码使用 NanoHTTPD 上传文件,但没有上传任何东西,也没有给我临时路径。 代码是: new Response( HTTP_OK, MIME_HTML, ""
我正在尝试使用带有嵌入式 Nanohttpd 服务器的 Web View 。我可以让两者都工作,但不能彼此合作。 我可以将 WebWiew 请求发送到其他未嵌入的代理,这有效。或者我可以从 Firef
我编写了一个java桌面应用程序,它使用嵌入式NanoHTTPD Web服务器接受http请求https://github.com/NanoHttpd/nanohttpd ,在收到http请求后,我的
我想制作一个使用nanohttp创建Web服务器的Android应用程序,此代码不会给我错误,但是当我在端口8000上的浏览器中输入IP地址时,它说连接超时,请帮助任何帮助将被应用这是代码.. pac
目前我需要在 Android 应用程序中使用本地 http 文件服务器,因此我决定使用 NanoHttpd SimpleWebServer。但我遇到了这样的异常: java.lang.NullPoin
我已经在 java 中使用 nanohttpd 实现了一个 websocket 服务器。我可以从网页中的 js 访问 websocket 服务器。它很好用。 但是,现在我想创建一个基于 Java 的客
我已经实现了 nanohttpd 服务器 nano我的目标是根据我的条件将请求转发到不同的域。 我的代码是这样的 package CreateServer; import java.io.File;
我想从服务器播放 mp3 文件服务器端代码: if (uri.contains("mp3")) { FileInputStream fis = null; try {
我需要创建一个能够为设备的本地文件提供服务的本地服务器。我找到了这个 library看起来它能够满足我的需求。 我已经在这个项目中尝试过示例,它工作正常!但它发布 .html 页面,我需要图片。 我在
我正在使用 NanoHTTPD 实现自定义 Web 服务器。我有扩展 NanoHTTPD 的 BaseServer 类: public class BaseServer extends NanoHTT
我已经将一个文件放在 NanoHTTPD 服务器上,如下所示: private class WebServer extends NanoHTTPD { public WebServer()
我正在使用 NanoHTTPD 播放我的 sdcard 中的音频文件,它工作正常。 如果我输入 http://172.16.6.75:8080在我的浏览器中它开始播放,但我想播放像 http://17
我是一名优秀的程序员,十分优秀!