GET & POST operation using RESTSharp in WP7

186 views
Skip to first unread message

prashant

unread,
May 19, 2012, 7:01:35 PM5/19/12
to rest...@googlegroups.com
Hi I m new to Windows phone 7... I need to consume REST based Webservices build in JAVA.. I am using RESTSharp and facing problem in getting response using RESTSharp in WP7:
Below is the code :
var client = new RestClient() { BaseUrl = "http://XYZ.com", };
var request = new RestRequest() { Resource = "WebService/Pick/111111111", Method = Method.GET, RequestFormat = DataFormat.Json };
client.ExecuteAsync(request, response =>
 {
    string responseText = response.Content;
  });
For eg my URL is in this form : http://XYZ.com/WebService/Pick/111111111

I get responseText as empty. I am unable to figure out what I am doing wrong.

Moreover how can I perform POST operation, a sample code will help me better.

Thanks




Andy Cutright

unread,
May 19, 2012, 7:35:38 PM5/19/12
to rest...@googlegroups.com
First are you certain your call is being handled by the server & that the server is returning data? You should probably verify that first. 

What does the JSON look like that's returned? You can use json2csharp.com to generate a class that you can use for deserialize the response. The deserialized object will be in the response's Data property. 

You then use that class to parameterize the ExecuteAsync call, e.g. I have service that shortens a url, and returns 

{ "shorturl" : "http://x.y/26j9", "longurl" : "<original url>" } 

By default json2csharp will name the class representing the JSON response "RootObject": 

public class RootObject
{
    public string shorturl { get; set; }
    public string longurl { get; set; }
}


I typically rename that to reflect the object I'm deserialzing: 

public class ShortUrl
{
    public string shorturl { get; set; }
    public string longurl { get; set; }
}

When you make the call, parameterize it with that object, and then cast the response DATA property to the object type, assuming the call completed properly. 

client.ExecuteAsync<ShortUrl>(request, (resp , handle) => 
{
   if (resp.StatusCode == HttpStatusCode.OK)
  {
    ShortUrl su = (ShortUrl)resp.Data; 
  }
}


You can set Method = Method.POST afaik just as you set GET in your code above. 


Cheers,
Andy 

John Sheehan

unread,
May 21, 2012, 5:44:12 PM5/21/12
to rest...@googlegroups.com
Also check to see if there's anything in resp.ErrorException
Reply all
Reply to author
Forward
0 new messages