2016-11-16 9 views
0

My.htm 있음, 배열 var mytemp 서버쪽에 게시 할 배열 var 서버 쪽에서 검색하도록 노력하겠습니다.클라이언트 측에서 배열 var을 게시 할 때 Android에서 NanoHTTPD를 사용하여 서버 측에서 배열을 검색하는 방법은 무엇입니까?

다음 코드를 사용하면 D 1, D, 2, Tom'Dog 문자열 만 얻을 수 있습니다. 어떻게 서버 측 배열을 검색 할 수 있습니까? 감사!

BTW, 나는 아약스가 아닌 전체 게시물을하기를 희망하므로 $ .ajax()가 맞지 않는다고 생각합니다.

에판 Mowlaei으로 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); 
    } 
} 

: 감사합니다! 다음 코드는 괜찮습니까?

클라이언트 측 :

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; 
    } 

답변

1

나는 당신이 점점 것이 된 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);에서 해당 데이터를 구문 분석하고 그것의 배열을 만들 수 있습니다.

+0

감사합니다. 자세한 내용 코드를 알려주시겠습니까? – HelloCW

+0

@HelloCW 어느 부분에 더 많은 코드가 필요합니까? 나는 앤드 로이드 데브 (Android Dev)라고 생각하고, 앤드 로이드 파트 (Android part)에서 묻는 한 당신을 도울 수 있습니다. –

+0

final JSONObject json = new JSONObject (files.get ("postData")) 원인 오류 – HelloCW