2011-08-08 69 views
0

我已經使用「Https」協議創建了與服務器的連接。這裏是我的代碼...如何將JSON對象發送到服務器

String httpsURL = "https://abc.com/auth/login/"; 
HttpsURLConnection con = null; 
try{ 
URL url = new URL(httpsURL); 
con = (HttpsURLConnection) url.openConnection(); 
con.setRequestMethod("POST"); 
con.setDoInput(true); 
con.setDoOutput(true); 
con.connect(); 
//String respMessage = con.getResponseMessage(); 
//int respCode = con.getResponseCode(); 
}catch(....){....} 

現在我必須通過該連接發送我的JSON對象到服務器。我怎樣才能做到這一點?請幫幫我。提前致謝。

回答

1

也許這可能會有幫助...我不知道你的代碼的具體用途。

String httpsURL = "https://abc.com/auth/login/"; 
HttpsURLConnection con = null; 
try{ 
    URL url = new URL(httpsURL); 
    con = (HttpURLConnection) url.openConnection(); 
    con.setRequestMethod("POST"); 
    con.setDoOutput(true); 
    //con.setDoInput(true); 
    con.setUseCaches(false); 
    con.setRequestProperty("Content-Type", "application/json"); 
    con.setRequestProperty("Accept", "application/json"); 
    con.setRequestProperty("Content-Length", Integer.toString(jsonValue.length())); 
    con.getOutputStream().write(jsonValue.getBytes()); 
    con.getOutputStream().flush(); 
    con.connect(); 

    if (con.getResponseCode() != HttpsURLConnection.HTTP_OK) { 
     throw new Exception("POST method failed: " + con.getResponseCode() + "\t" + con.getResponseMessage()); 
    } else { 
     InputStream responseContent = (InputStream) con.getContent(); 
    } 
    ... 
}catch(....){....} 
相關問題