Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RestSharp in Xamarin for Android not giving any response

I am currently experimenting with Mono for android at the moment I have a Rest based service installed at the following URL

http://localhost:8080//api/mobileclient?email=aakpenyi3%40yahoo.com&phone=80604456789

The server is IIS8.0 and service is built using asp.net web API.

When I make a request using Curi or any of the Plugin based Rest Clients. All is well and the response comes as expected

However I like to use this in a Mobile client

I have the following code using Xamarin for Android

    public class RequestBrokerClass
{
    const string baseUrl = "http://partnerens.com/api/mobileclient/";


    public T Execute<T>(RestRequest request) where T : new()
    {
        var client = new RestClient ();
        client.BaseUrl = baseUrl;
        var response = client.Execute<T> (request);
        if (response.ErrorException != null) {
            const string message = "Error contacting partnerens servers";
            var exception = new ApplicationException (message, response.ErrorException);
            throw exception;
        }
        return response.Data;

    }

}

and the following excerpt using the above code. (Please note that email has been URL encoded).

public IEnumerable<PartnershipRecords> GetItemFromApi(string email, string phoneNumber)
    {

        var request = new RestRequest ();
        request.Method = Method.GET;
        request.AddParameter ("email", email);
        request.AddParameter ("phone", phoneNumber);
        var items = new RequestBrokerClass ().Execute<List<PartnershipRecords>> (request);
        return items;

    }

However my problem is that Response is always empty if made using the Mobile client but not empty if made using a REST Client.

like image 973
Seth IK Avatar asked Dec 01 '25 02:12

Seth IK


1 Answers

OK I got it fixed here is what i did

I added the following to the AndroidManifest.xml

    <uses-permission android:name="android.permission.INTERNET" />

And voila, server responds appropriately

like image 70
Seth IK Avatar answered Dec 04 '25 00:12

Seth IK



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!