2017-08-17 7 views
-5

Wenn ich ein JSON-Objekt analysieren, wird es leicht und im richtigen Format analysiert. Aber sobald ich das gleiche an die Server-Seite sende, gehen Werte verloren.JSON-Daten fehlen beim Senden an Server

JSONArray check0 = new JSONArray(); 
    JSONArray check1 = new JSONArray(); 
    JSONArray check2 = new JSONArray(); 

    check0.put(v11); 
    check0.put(v12); 
    check0.put(v13); 

    check1.put(c11); 
    check1.put(c12); 
    check1.put(c13); 

    check4.put(t11); 
    check4.put(t12); 
    check4.put(t13); 


    draft.put("check2",id11); 
    draft.put("check3",t11); 

    draft.put("check0",check0); 
    draft.put("check1",check1); 
    draft.put("check4",check4);  

wo v11, v12 ....... t12, t13 sind String-Variablen.

Mein Code für das Senden von Daten:

URL url = new URL("http://10.0.2.2:8080/initial"); 
HttpURLConnection connection = (HttpURLConnection)url.openConnection(); 
connection.setRequestMethod("POST"); 
connection.setRequestProperty("USER-AGENT", "Mozilla/5.0"); 
connection.setRequestProperty("ACCEPT-LANGUAGE", "en-US,en;0.5"); 
connection.setDoOutput(true); 
DataOutputStream dStream = new 
DataOutputStream(connection.getOutputStream()); 
System.out.println(draft.toString()); 
dStream.writeBytes(draft.toString()); 

System.out:

{"Check1":["5","4","4"],"Check0":["6","44","4"],"Check2":"17082017123406","Check4":"1228123682","Check3":["4","4","4"]} 

-Code der Post-Anforderung zu erhalten:

app.post('/initial', function(req, res){ 

console.log("POST received: "+ JSON.stringify(req.body)); 

res.end("cool"); 
}); 

Console Ausgabe POST erhalten:

{"{\"Check1\":":{"\"5\",\"4\",\"4\"":{"\"6\",\"44\",\"4\"":{"\"4\",\"4\",\"4\"":""}}}}

+2

finden Sie einige Code teilen, so kann jemand den Fehler beurteilen. – Nitesh

+0

Ich habe den Code aktualisiert, bitte helfen Sie mir. –

+0

teilen Sie bitte den Code, wo Sie die Anfrage json erstellen. – Nitesh

Antwort

0

versuchen, die folgende

con.setRequestProperty("Content-Type", "application/json; charset=UTF-8"); 

Beispiel mit

private JSONObject uploadToServer() throws IOException, JSONException { 
     String query = "https://example.com"; 
     String json = "{\"key\":1}"; 

     URL url = new URL(query); 
     HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
     conn.setConnectTimeout(5000); 
     conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8"); 
     conn.setDoOutput(true); 
     conn.setDoInput(true); 
     conn.setRequestMethod("POST"); 

     OutputStream os = conn.getOutputStream(); 
     os.write(json.getBytes("UTF-8")); 
     os.close(); 

     // read the response 
     InputStream in = new BufferedInputStream(conn.getInputStream()); 
     String result = org.apache.commons.io.IOUtils.toString(in, "UTF-8"); 
     JSONObject jsonObject = new JSONObject(result); 


     in.close(); 
     conn.disconnect(); 

     return jsonObject; 
} 
Verwandte Themen