正在从WCF服务读取android中的对象列表

本文关键字:对象 列表 android 读取 WCF 服务 | 更新日期: 2023-09-27 18:21:12

我正在构建一个应用程序来检索用户事务的列表。但我不知道该怎么做。

我试图返回一个列表,但不知道如何从那里继续(读取数据)

如果我阅读对象列表的方法是错误的,请建议我,谢谢。

这是我的ImmobileService代码:

[ServiceContract]
public interface ImobileServices
{
    [OperationContract]
    [WebInvoke(
        Method = "POST",
        UriTemplate = "getTransaction",
        BodyStyle = WebMessageBodyStyle.WrappedRequest,
        ResponseFormat = WebMessageFormat.Json,
        RequestFormat = WebMessageFormat.Json)]
    List<salesOrder> retrieveTransaction(salesOrder salesorder);
}
[DataContract]
public class salesOrder
{
    [DataMember(Name = "NRIC")]
    public string NRIC
    {
        get;
        set;
    }
    [DataMember(Name = "ItemName")]
    public string ItemName
    {
        get;
        set;
    }
    [DataMember(Name = "Quantity")]
    public string Quantity 
    {
        get;
        set;
    }
    [DataMember(Name = "ItemPrice")]
    public string ItemPrice
    {
        get;
        set;
    }
    [DataMember(Name = "ItemDescription")]
    public string ItemDescription
    {
        get;
        set;
    }
    [DataMember(Name = "Date")]
    public string Date
    {
        get;
        set;
    }
}

我的手机服务代码:

    public List<salesOrder> retrieveTransaction(salesOrder salesorder)
    {
        List<salesOrder> newList = new List<salesOrder>();
        salesOrder order1 = new salesOrder();
        order1.ItemName = "item 1";
        order1.ItemDescription = "this is item 1";
        order1.ItemPrice = "40";
        order1.Quantity = "1";
        order1.Date = DateTime.Now.ToString();
        salesOrder order2 = new salesOrder();
        order2.ItemName = "item 2";
        order2.ItemDescription = "this is item 2";
        order2.ItemPrice = "30";
        order2.Quantity = "1";
        order2.Date = DateTime.Now.ToString();
        newList.Add(order1);
        newList.Add(order2);
        return newList;
    }

我的安卓代码:

private class sendPostData extends AsyncTask<String, Void, String>
{
    @Override
    protected String doInBackground(String... params) {
        // TODO Auto-generated method stub
        HttpPost request = new HttpPost(SERVICE_URI + "/getTransaction");
        request.setHeader("Accept", "application/json");            
        request.setHeader("Content-type", "application/json");
        JSONStringer salesorder;
        try {
            salesorder = new JSONStringer()
                .object()
                    .key("salesorder")
                        .object()                               
                            .key("NRIC").value(params[0])
                        .endObject()
                    .endObject();
        StringEntity entity = new StringEntity(salesorder.toString());
        request.setEntity(entity);
        // Send request to WCF service
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpResponse response = httpClient.execute(request);
        HttpEntity responseEntity = response.getEntity();
        // Read response data into buffer
        char[] buffer = new char[(int)responseEntity.getContentLength()];
        InputStream stream = responseEntity.getContent();
        InputStreamReader reader = new InputStreamReader(stream);
        reader.read(buffer);
        stream.close();
        //JSONObject salesOrderObject= new JSONObject(new String(buffer));
        }
        catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        refreshListView();
        return null;
    }

最后是我的日志:

07-06 19:30:48.035: W/dalvikvm(21473): threadid=12: thread exiting with uncaught exception (group=0x40e20930)
07-06 19:30:48.059: E/AndroidRuntime(21473): FATAL EXCEPTION: AsyncTask #1
07-06 19:30:48.059: E/AndroidRuntime(21473): java.lang.RuntimeException: An error occured while executing doInBackground()
07-06 19:30:48.059: E/AndroidRuntime(21473):    at android.os.AsyncTask$3.done(AsyncTask.java:299)
07-06 19:30:48.059: E/AndroidRuntime(21473):    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
07-06 19:30:48.059: E/AndroidRuntime(21473):    at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
07-06 19:30:48.059: E/AndroidRuntime(21473):    at java.util.concurrent.FutureTask.run(FutureTask.java:239)
07-06 19:30:48.059: E/AndroidRuntime(21473):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
07-06 19:30:48.059: E/AndroidRuntime(21473):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
07-06 19:30:48.059: E/AndroidRuntime(21473):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
07-06 19:30:48.059: E/AndroidRuntime(21473):    at java.lang.Thread.run(Thread.java:856)
07-06 19:30:48.059: E/AndroidRuntime(21473): Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
07-06 19:30:48.059: E/AndroidRuntime(21473):    at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:4746)
07-06 19:30:48.059: E/AndroidRuntime(21473):    at android.view.ViewRootImpl.focusableViewAvailable(ViewRootImpl.java:2588)
07-06 19:30:48.059: E/AndroidRuntime(21473):    at android.view.ViewGroup.focusableViewAvailable(ViewGroup.java:608)
07-06 19:30:48.059: E/AndroidRuntime(21473):    at android.view.ViewGroup.focusableViewAvailable(ViewGroup.java:608)
07-06 19:30:48.059: E/AndroidRuntime(21473):    at android.view.ViewGroup.focusableViewAvailable(ViewGroup.java:608)
07-06 19:30:48.059: E/AndroidRuntime(21473):    at android.view.ViewGroup.focusableViewAvailable(ViewGroup.java:608)
07-06 19:30:48.059: E/AndroidRuntime(21473):    at android.view.ViewGroup.focusableViewAvailable(ViewGroup.java:608)
07-06 19:30:48.059: E/AndroidRuntime(21473):    at android.view.ViewGroup.focusableViewAvailable(ViewGroup.java:608)
07-06 19:30:48.059: E/AndroidRuntime(21473):    at android.view.View.setFlags(View.java:8417)
07-06 19:30:48.059: E/AndroidRuntime(21473):    at android.view.View.setFocusableInTouchMode(View.java:5790)
07-06 19:30:48.059: E/AndroidRuntime(21473):    at android.widget.AdapterView.checkFocus(AdapterView.java:717)
07-06 19:30:48.059: E/AndroidRuntime(21473):    at android.widget.ListView.setAdapter(ListView.java:463)
07-06 19:30:48.059: E/AndroidRuntime(21473):    at com.example.isellcrazyitem.Transaction.refreshListView(Transaction.java:65)
07-06 19:30:48.059: E/AndroidRuntime(21473):    at com.example.isellcrazyitem.Transaction.access$0(Transaction.java:62)
07-06 19:30:48.059: E/AndroidRuntime(21473):    at com.example.isellcrazyitem.Transaction$sendPostData.doInBackground(Transaction.java:153)
07-06 19:30:48.059: E/AndroidRuntime(21473):    at com.example.isellcrazyitem.Transaction$sendPostData.doInBackground(Transaction.java:1)
07-06 19:30:48.059: E/AndroidRuntime(21473):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
07-06 19:30:48.059: E/AndroidRuntime(21473):    at java.util.concurrent.FutureTask.run(FutureTask.java:234)
07-06 19:30:48.059: E/AndroidRuntime(21473):    ... 4 more
07-06 19:30:48.957: D/AndroidRuntime(21473): Shutting down VM
07-06 19:30:48.957: W/dalvikvm(21473): threadid=1: thread exiting with uncaught exception (group=0x40e20930)
07-06 19:30:48.957: I/Process(21473): Sending signal. PID: 21473 SIG: 9

正在从WCF服务读取android中的对象列表

您有这个

    refreshListView();

您可能正在从doInbackground更新ui,这是不可能的。您需要从ui线程更新ui。

您可以使用runOnUiThread,也可以在onPostExecute中执行。