开发者

Calling a web service from an Android client

开发者 https://www.devze.com 2023-04-12 23:42 出处:网络
I\'m doing my first Android app and first WCF service at the same time. I have the following service contract:

I'm doing my first Android app and first WCF service at the same time.

I have the following service contract:

[ServiceContract]
    public interface ILoginService
    {

        [OperationContract]
        string Login(string username, string password);

        // TODO: Add your service operations here
    }

I want to call this from my Android app and pass in the username and password parameters. 1. Is this operation contract right for the job? 2. Can someone please point me to a tutorial / code sample of how I can call thi开发者_如何学运维s WCF service via Android?

Thank you!

Edit: I might be a little closer, but I'm still lost. Here is what I have so far: The Contract:

[OperationContract]
        [WebGet(ResponseFormat= WebMessageFormat.Json,
            UriTemplate="/LoginService/?username={username}&password={password}")]
        Response Login(string username, string password);

Then I call the service from Android with:

public LoginResponse RemoteLogin(String username, String password)
    {
        loginParameters = "/LoginService/username=" + username + "&password=" + password;


        HttpClient httpclient = new DefaultHttpClient();
        HttpGet httpget = new HttpGet(serviceAddress + loginParameters);

        HttpResponse response;

        LoginResponse loginResponse = null;

        try
        {
            response = httpclient.execute(httpget);

            HttpEntity entity = response.getEntity();

            if(entity != null)
            {
                InputStream instream = entity.getContent();
                String result = convertStreamToString(instream);
                JSONObject json = new JSONObject(result);

                // Parsing
                JSONArray nameArray = json.names();
                JSONArray valArray = json.toJSONArray(nameArray);

                loginResponse = new LoginResponse(valArray.getBoolean(1), valArray.getString(0), valArray.getString(2));


                instream.close();
            }

        }
        catch(Exception e)
        {
            loginResponse = new LoginResponse();
            String sDummy = e.toString();
        }
        return loginResponse;
    }

I have no idea what's wrong now.


Didn't really solve the issue, just kind of side-stepped it. I removed anything to do with ServiceModel from the web.config and added a ServiceHostFactory directive to the SVC file. That did the trick. Now everything is working. It must have been some kind of configuration issue.

0

精彩评论

暂无评论...
验证码 换一张
取 消