0

我尝试使用来自 Web 服务的数据 json 以 xamarin 形式显示列表视图

这是我的数据 json:

{"isSuccess":true,"resultMessage":"","data":[{"password":"MnwPzyBdAZBrDV6xMHoIzg","userid":4,"name":"Budi","tanggal_lahir":"09/03/1996 00:00:00","email":"budi123@gmail.com","tanggal_join":"01/20/2020 15:08:07","username":"budi001"},
{"password":"PFjQJkorLvR+ilWUWdq1Yw","userid":5,"name":"Hermawan","tanggal_lahir":"09/07/1996 00:00:00","email":"mawan@gmail.com","tanggal_join":"01/23/2020 15:08:07","username":"mawan123"}]}

这是我的编码网络服务:

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public void getAllUsers()
    {
        DbAccessConnection conn = getActiveConnection();

        if (conn == null)
            return;

        try
        {
            beginTransaction(conn);

            MimoUser users = new MimoUser(conn);
            DataSet ds = new DataSet();
            string tbName = "MimoUser";

            users.getAll(ds, tbName);
            DataTable dtTbl = ds.Tables[tbName];

            List<Dictionary<String, object>> lsOfDtTbl = Tools.convertDataTableToList(dtTbl);

            commitTransaction(conn);
            Responder.writeResponse(true, "", lsOfDtTbl );
            //commitTransaction(conn);
        }
        catch (Exception ex)
        {
            rollbackTransaction(conn);
            Responder.writeResponse(false, ex.Message);
        }
    }

这是我的 xamarin 形式的编码

接口:
IUserApi.cs

public interface IUserApi
{
    [Get("/getAllUsers")]
    Task<List<User>> getAllUsers();
}

模型:
User.cs

public class User
{
    public object password { get; set; }
    public int userid { get; set; }
    public string name {get; set; }
    public object tanggal_lahir { get; set; }
    public object email { get; set; }
    public object tanggal_join { get; set; }
    public string username { get; set; }
}

数据返回结果.cs

public class DataReturnResult
{
    public Boolean isSuccess;
    public String resultMessage;
    public object data;

    public static DataReturnResult createReturnResult(Boolean isSuccess,String resultMessage,object data = null)
    {
        DataReturnResult result = new DataReturnResult();
        result.isSuccess = isSuccess;
        result.resultMessage = resultMessage;
        result.data = data;
        return result;
    }


    public static String createReturnResultJson(Boolean isSuccess,String resultMessage,object data = null)
    {
        DataReturnResult result = createReturnResult(isSuccess, resultMessage, data);
        return JsonConvert.SerializeObject(result);
    }
}

主页.xaml

<ContentPage.Content>
    <StackLayout
        Padding="20">

        <Button 
            Text="Get User"
            Clicked="OnUserClicked"
            BackgroundColor="Black"
            TextColor="White"
            HorizontalOptions="FillAndExpand"/>

        <ListView
            x:Name="ListUser">

            <ListView.ItemTemplate>
                <DataTemplate>
                    <TextCell
                        Text="{Binding name}"/>
                </DataTemplate>
            </ListView.ItemTemplate>

        </ListView>


    </StackLayout>
</ContentPage.Content>

MainPage.xaml.cs

public partial class MainPage : ContentPage
{
    public MainPage()
    {
        InitializeComponent();
    }

    async void OnUserClicked(object sender,EventArgs ex)
    {

        var apiResponse = RestService.For<IUserApi>("http://192.168.0.188/appwebservice/appwebservice.asmx");
        var listUser = await apiResponse.getAllUsers();

        ListUser.ItemsSource = listUser;
    }

}

单击“获取用户”按钮时收到错误消息。这是一条消息错误:

Refit.ApiException: 'Response status code does not indicate success: 500 (Internal Server Error).'

我尝试更改代码 IUserApi.cs Task<List<DataReturnResult>> getAllUsers(),我从 Get User 收到相同的错误消息

你能帮我解决一下吗?

4

0 回答 0