从android发送帖子数据

时间:2015-06-11 06:27:16

标签: java android rest

我正在向某个服务器发送POST请求。

我可以像这样从curl发送日期:

     curl -v -H 'Content-Type: application/json' -H 'Accept: application/json' -X POST https://ictexpo.herokuapp.com/users -d "{\"user\":{\"name\":\"Choity\"}}"

但是当我想从java发送相同的数据时,我得不到结果。

        String urlParameters = "{\"user\" : {\"name\" :  \"lssl\" }}";

        URL url2 = new URL(url);
        HttpsURLConnection connection = (HttpsURLConnection) url2.openConnection();

        connection.setRequestMethod("POST");
        connection.setRequestProperty("Content-Type", "application/json");
        connection.setRequestProperty("Accept","application/json");
        connection.setRequestProperty("Content-Length", "" + Integer.toString(urlParameters.getBytes().length));
        connection.setRequestProperty("Content-Language", "en-US");
        connection.setUseCaches(false);
        connection.setDoInput(true);



        //Send request
        DataOutputStream wr = new DataOutputStream (connection.getOutputStream());
        wr.writeBytes (urlParameters);
        wr.flush();
        wr.close();

        //Get Response

        InputStream is = connection.getInputStream();
        BufferedReader rd = new BufferedReader(new InputStreamReader(is));
        String line;
        StringBuffer response = new StringBuffer();
        while((line = rd.readLine()) != null) {
            response.append(line);
            response.append('\r');
        }
        rd.close();

任何人都可以告诉我为什么我得到了除了?

0 个答案:

没有答案