2011-08-08 20 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(....){....} 
関連する問題