gpt4 book ai didi

android - 当我在客户端发布数组 var 时,如何在 Android 中使用 NanoHTTPD 在服务器端检索数组?

转载 作者:行者123 更新时间:2023-11-30 00:53:03 24 4
gpt4 key购买 nike

在 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com