从web服务集中返回响应领域 [英] Set return response fields from webservice

查看:165
本文介绍了从web服务集中返回响应领域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用一个web服务与它由几个字段的名称,即,年龄,性别,city.Now我已经写了一个Web服务的基础上的名字,并返回到我的活动而返回true或false <小型数据库/ p>

现在在这里我需要输出(ie..I需要显示该用户的其他领域的年龄,性别和城市)的各种textviews。

这是我想要做的:

 &LT;的WebMethod&GT; _
公共职能GetUserDetails(BYVAL code作为字符串)作为字符串    昏暗康恩作为的SqlConnection
    康恩= ConnectionManager.GetConnection()
    conn.Open()
    昏暗DsCheckUser作为新的数据集()
    昏暗NEWCMD作为的SqlCommand = conn.CreateCommand()    newCmd.CommandType = CommandType.Text
    newCmd.CommandText =&AMP选择的年龄,从定义UserDetails其中username ='性;用户名和放大器; '    昏暗的SQLDA作为新的SqlDataAdapter(NEWCMD)
    sqlda.Fill(DsCheckUser)
    取回您的DB的结果。
    如果DsCheckUser.Tables(0).Rows.Count&GT; 0,则
        返回TRUE
    其他
        返回False
    万一
    conn.Close()
    '退货地址
结束功能

和OnPostExecute:

 保护无效onPostExecute(虚空结果){
        //错误状态是假的
        如果(!误码){
            //基于布尔值从返回的WebService
            如果(codeStatus){
                            //这里我需要设置基于用户名的UserDetails,并在每一个人的TextView显示它们。
            }其他{
                //设置错误信息
                Toast.makeText(getBaseContext(),用户名未找到,Toast.LENGTH_SHORT).show();
            }
        //错误状态是真实的
        }其他{
        Toast.makeText(getBaseContext(),Toast.LENGTH_SHORT错误调用Web服务出错).show();
        }
        //重新初始化错误状态为False
        出错= FALSE;
    }

因此​​,基于codestatus是否属实,我需要从我的webmethod获得剩余的年龄,性别和城市领域,并在我的TextView显示它们。

这是我的doInBackground:

  @覆盖
        保护无效doInBackground(字符串... PARAMS){
            //调用Web方法
            codeStatus = ScanWebService.invokeScanWS(EDITTEXT code,GetBar codeDetails);
            返回null;
        }


解决方案

好吧,我想我明白你现在所需要的。

doInBackground

从外观上来看,我会说你最好的选择将要首先拨打电话到的WebMethod 键,你可以很容易地检查返回值真正。如果它返回真正,你可以对的WebMethod 第二个电话并保存在<$ c中的返回值$ C>字符串。然后,您可以解析字符串,并保存字符串 S IN领域,您声明无论是在开始的的AsyncTask 活动(如果你需要的分数之外的值的AsyncTask 那么显然你声明它们在活动

onPostExecute

您可以轻松地只是把您从 doInBackground 存储的值的字段,设置文本的的TextView 那儿。

我没有与的WebMethod s或任何太多的经验,所以我不能帮你,但是(虽然它似乎像返回地址行不可达code)。

I am trying to use a webservice with a small database which consists of few fields namely name, age, sex, city.Now I have written a webservice which return either true or false based on name and returns to my activity.

Now here I need to output(ie..I need to show the other fields age,sex and city of that user) in various textviews.

This is what I'm trying to do:

 <WebMethod> _
Public Function GetUserDetails(ByVal code As String) As String

    Dim conn As SqlConnection
    conn = ConnectionManager.GetConnection()
    conn.Open()
    Dim DsCheckUser As New DataSet()
    Dim newCmd As SqlCommand = conn.CreateCommand()

    newCmd.CommandType = CommandType.Text
    newCmd.CommandText = "Select age, sex from userdetails where username='" & username & "' "

    Dim sqlda As New SqlDataAdapter(newCmd)
    sqlda.Fill(DsCheckUser)
    'Getting Back result from your DB.
    If DsCheckUser.Tables(0).Rows.Count > 0 Then
        Return True
    Else
        Return False
    End If
    conn.Close()
    'Return address
End Function

And OnPostExecute:

protected void onPostExecute(Void result) {
        //Error status is false
        if(!errored){
            //Based on Boolean value returned from WebService
            if(codeStatus){
                            //Here I need to set the userdetails based on username and display them in each individual textview.
            }else{
                //Set Error message
                Toast.makeText(getBaseContext(), "Username not found", Toast.LENGTH_SHORT).show();
            }
        //Error status is true  
        }else{
        Toast.makeText(getBaseContext(), "Error occured in invoking webservice", Toast.LENGTH_SHORT).show();
        }
        //Re-initialize Error Status to False
        errored = false;
    }

So based on codestatus whether it is true I need to get the remaining age, sex and city fields from my webmethod and display them in my textviews.

And here is my doInBackground:

 @Override
        protected Void doInBackground(String... params) {
            //Call Web Method
            codeStatus = ScanWebService.invokeScanWS(editTextCode,"GetBarcodeDetails");
            return null;
        }

解决方案

Ok, I think I understand what you need now.

In doInBackground

From the looks of it, I would say your best bet would to firstly, make the call to the WebMethod and you can easily check if the returned value is true or false. If it returned true, you can make the second call to the WebMethod and save the returned value in a String. You can then parse the String and save the Strings in fields that you declared either at the beginning of the AsyncTask or Activity (if you need the values outside of the score of the AsyncTask then obviously you declare them in the Activity.

In onPostExecute

You can easily just take the fields that you stored the values from doInBackground and set the text to the TextViews there.

I don't have much experience with WebMethods or whatever so I can't help you with that, however (though, it does seem like the return address line is unreachable code).

这篇关于从web服务集中返回响应领域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆