Java使用REST将参数传递给parse.com云代码函数

时间:2015-07-06 18:17:04

标签: java parse-platform httpurlconnection

您好我在将数据传递到我的parse.com云代码功能时遇到了很多问题。我正在尝试使用Parse.com REST服务调用我的云代码。以下是我的代码:

var doc = XDocument.Parse(xml);

foreach (var bar in doc.Descendants("Bar"))
{
    bar.Value = bar.Value.Trim();
}

using (var reader = doc.CreateReader())
{
    var obj = serializer.Deserialize(reader);
}

我从服务器获得响应并取得了成功。我正在尝试打印出用户名,但我得到的就是:

private static void pushWithParse() throws IOException
{
    String url = "https://api.parse.com/1/functions/testCloudCode";

    HttpURLConnection connection = null;  
      try 
      {
        //Create connection
        URL urlRequest = new URL(url);
        System.out.println(urlRequest.toString());

        connection = (HttpURLConnection)urlRequest.openConnection();
        connection.setRequestMethod("POST");
        connection.setRequestProperty("X-Parse-Application-Id", "appID");
        connection.setRequestProperty("X-Parse-REST-API-Key", "appKey");
        connection.setRequestProperty("Content-Type","application/json; charset=UTF-8");
        connection.setRequestProperty("Accept-Charset", "UTF-8");
        connection.setDoOutput(true);

        OutputStream out = connection.getOutputStream();
        Writer writer = new OutputStreamWriter(out, "UTF-8");

        writer.write("username");
        writer.write("=");
        writer.write(URLEncoder.encode("joey", "UTF-8"));

        InputStreamReader isr = new InputStreamReader(connection.getInputStream(), "UTF-8");
        BufferedReader rd = new BufferedReader(isr);
        StringBuilder response = new StringBuilder(); 
        String line;
        while((line = rd.readLine()) != null) {
          response.append(line);
          response.append('\r');
        }
        writer.close();
          out.close();
        rd.close();
        System.out.println(response.toString());

      }
      catch (Exception e)
      {
            e.printStackTrace();

      }
      finally
      {
            if(connection != null)
            {
              connection.disconnect(); 
            }
      }

}

我的云代码:

Input: {}
Result: Yay need to fix push NOTIFICATIONs
I2015-07-06T17:45:25.223Z]___________________________________________________
I2015-07-06T17:45:25.224Z]No Message provided
I2015-07-06T17:45:25.225Z]No Message provided

我曾尝试过关于在Java中传递数据的各种帖子,但没有运气仍然没有显示消息。提前谢谢。

0 个答案:

没有答案