从parse.com读取数据不起作用

时间:2015-07-26 12:35:40

标签: c# parse-platform

我有这段代码来从parse.com读取JSON数据

 protected void Button4_Click(object sender, EventArgs e)
{
    string URL = "https://api.parse.com/1/classes/SecondObject";
   // string DATA = jsonString;
    string text;
    var request = WebRequest.Create(URL);
    request.Method = "POST";
    request.ContentType = "application/json";
    request.Headers.Add("X-Parse-Application-Id", "aa");
    request.Headers.Add("X-Parse-REST-API-Key", "bb");


    WebResponse response = request.GetResponse();
    Stream responsestream = response.GetResponseStream();
    StreamReader reader = new StreamReader(responsestream);

    //IEnumerable<parseo

}

不幸的是我得到了400个不好的请求。 我想要的是阅读所有存在的json,我的课程称为&#34; secondobject&#34;。

任何人都可以帮我解决这个问题吗?

谢谢。

3 个答案:

答案 0 :(得分:0)

当您创建对象时,您获得了一个完整的URL,其中包含Status: 201 Created Location: https://api.parse.com/1/classes/SecondObject/E234jdnsl 标头中SecondObject的特定实例的标识符。您需要访问该完整网址。

例如,当您发布对象时,您返回了以下标题

{
  "createdAt": [timestamp],
  "objectId": "E234jdnsl"
}

此外,你还收到了身体中的以下json:

E234jdnsl

var c=document.getElementById("myCanvas"); var cxt=c.getContext("2d"); var img=new Image() img.src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAYAAACqaXHeAAAABGdBTUEAAK/INwWK6QAAABl0RVh0U29mdHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAABAJSURBVHja7FtbbBxXGZ6Z9dq7tmPHceLYSZy0DUnTpiFtgqKKKkWgAkIqpXloEVAekKiEBEVFiMsTTwjxghBIfeAVqQgJBH0A8YbShkqoLSlqAZFQaHCuxM3NsXdt784cvu/s/4//PZl1dpuSVgoj/Tozs3M53/dfz5mzsXMuupW3JLrFt/8TcKsT0Fd08tVXX131plKp1GIvSfw+2ziOc9Hf7TlcM4JTU9jfgnYS7XrIIIQXp2jn0b6F9hTkJPbPQpb0ORqr2IaSpmmUZZlvm81m1Cmu3Xnnnd0R8A5tMWQ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msbfPdr+wrc/inQZeac/yDC/TVr1oyixB2/dOnS7+DjP67Vaoxw/VzaYEatWTjOeLsEEERZhC8ZMMJzDWhhBAHocZjjR65cueIgnE1Kg6WwbSu+9BOZnSGyYwD9hij7fv4B76mOj4+vhbfNojL9OdzvD5LJCHZZZq+0pTQNKT0ToNom6DWQEZEhSEVESeC1jeHh4a3QzIeQenbCIrhsZp4WYT6Q2MmBa1wgtAKpREt4Hhdi8r8Gc9D6y7C0P3K5nvStKaAXRbh6fF4WTM7Jny8aujKlWwK0gwQ3CuH630nImKwKr8pvffYTuTKODk/AGnYNDQ1tJ2kyb7fEaWYGc5lXCIfC6ibMIFxcAoUPlLHPFD8Lq/o74sw/cH9NiNf7U5GGEFEX4LPyR4sLXOrEa1wB2NUIiORFBD0lMm4soE8kXAkaSYe4KqQCEiYgW0DKRoAhmRVZENFmkjqbLEDqsJwL9Xr9DAqv04jwFwVs2SyZs5IJ+Q1DwH8gZ+SvN1d7JUA10icaJwlcYLBWjtUFyoaIJFgab8nI1JoxZhiCdrnuqCKrwROZT1iGdgl8AYbCFehLAq5kFnNlwfRcKsCtG9QE8EXR/mWNB28nCCYCjpYwKNqvBjGgPyCiFCyVSYLVaKutDbTk2etSQ6Q1edW6Bj2NAXUhoibENG80DZbMOn+VctD2BdZQREISpNWoIBC6Ai2HJDQNeLtvj+3171ghFAc+H5p8KVgBnhQcR9ebEisgIiTFEuOCvN/Rwoqw9vp/AfvgNAATd1jjG11H6928q+iDiStKozf1T1PX6dCqK/Cv86ybtsW3+t/n/yvAAHNTLm1nQ+TCAAAAAElFTkSuQmCC"; setTimeout(function(){cxt.drawImage(img,0,0);},2000);位是您特定实例的对象标识符。然后,您需要将WebRequest指向网址 https://api.parse.com/1/classes/SecondObject/E234jdnsl 以检索它。

答案 1 :(得分:0)

如评论中所述,您的代码正确率为99%。唯一的问题是,当您应该使用POST命令时,您正在使用GET命令来检索数据。

这里有一些稍微更新的代码,使用的GET命令应该适合您。我在最后添加了一些代码来将流读入StringBuilder并将其显示在文本框中,但您可以将其更改为您需要的任何内容。

private void Button4_Click(object sender, EventArgs e)
{   
    string URL = "https://api.parse.com/1/classes/SecondObject";

    var request = WebRequest.Create(URL);
    request.Method = "GET";
    request.ContentType = "application/json";
    request.Headers.Add("X-Parse-Application-Id", "YOUR_APP_ID");
    request.Headers.Add("X-Parse-REST-API-Key", "YOUR_REST_API_KEY");

    WebResponse response = request.GetResponse();
    Stream responsestream = response.GetResponseStream();
    StreamReader reader = new StreamReader(responsestream);

    StringBuilder sbTempData = new StringBuilder();
    while (reader.Peek() > -1)
    {
        sbTempData.AppendLine(reader.ReadLine());
    }

    txtResults.Text = sbTempData.ToString();
}

请注意,这将检索该类中的每个对象。如果您只想检索班级中的某些对象,则可以修改您的网址,如https://api.parse.com/1/classes/ClassName/ObjectID

有关详细信息,请参阅https://parse.com/docs/rest/guide#queries

答案 2 :(得分:0)

我已经解决了问题

    string URL = "https://api.parse.com/1/classes/SecondObject";
   // string DATA = jsonString;
    string text;
    var request = WebRequest.Create(URL);
    request.Method = "GET";
    request.ContentType = "application/json";
    request.Headers.Add("X-Parse-Application-Id", "aa");
    request.Headers.Add("X-Parse-REST-API-Key", "bb");


    WebResponse response = request.GetResponse();
    Stream responsestream = response.GetResponseStream();
    StreamReader reader = new StreamReader(responsestream);
    string strResponse = null;
    if (!reader.EndOfStream)
    {
        strResponse = reader.ReadToEnd();
    }

现在strResponse包含&#34; SecondObject&#34;中存在的所有数据。

谢谢