我正在構建一個應用程序來檢索用戶事務的列表。但我不知道該怎麼做。從WCF服務中讀取Android對象列表
我試圖返回一個列表,但不知道如何從那裏繼續(讀取數據)
如果我的方式來閱讀對象的名單有誤,請告訴我謝謝。
這裏是我的Imobileservice代碼:
[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;
}
}
而且我mobileService代碼:
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;
}
我的Android代碼:
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;
}
最後我的logcat:
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
其實logcat中的錯誤是因爲你在doInBackground中調用了refreshListView(),這是禁止的,因爲doInBackground在後臺線程中運行,你的listview必須從ui線程更新。將您的refreshListView()放在您的asyncTask中的onPostExecute覆蓋內,看看如何去... – Christos
錯誤消失了,但我仍然不知道如何從WCF服務讀取我的列表 –
這就是爲什麼我沒有提供答案因爲我沒有對Android做過這樣的事情,所以我只是對我在logcat中注意到的事情發表評論。不過,這裏有很多專家,答案很快就會有信心:) – Christos