开发者

How to send the JSON object to server

I have created the connection to server using "Https" protocol. Here is my code...

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.c开发者_如何转开发onnect();
//String respMessage = con.getResponseMessage();
//int respCode = con.getResponseCode();
}catch(....){....}

Now i have to send my JSON object to server over that connection. How can i do that? Please Help me. Thanks in advance.


Maybe this could be helpful... I don't know the specific use for your code.

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(....){....}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜