1. 程式人生 > >android利用ksoap2返回複雜資料,資料集(dataset)

android利用ksoap2返回複雜資料,資料集(dataset)

在讀這篇文章之前建議你先讀一下上一篇文章android如何使用ksoap2對sql server的操作實現登陸,原理是一樣的只是返回的資料不同而已。
web端程式碼:
//database.cs檔案利用ADO.NET技術
public DataSet GetDataSet(string strSql, string tableName)
        {
            DataSet dataSet = new DataSet();
            OleDbConnection conn = new OleDbConnection(ConnectionString);
            OleDbDataAdapter dataAdapter = new
OleDbDataAdapter(strSql, conn); dataAdapter.Fill(dataSet, tableName); return dataSet; //返回這個資料集 } public void execsql(string strSql) { OleDbConnection dbconn = new OleDbConnection(ConnectionString);//資料庫連線 OleDbCommand comm = new
OleDbCommand(strSql, dbconn);//定義並初始化命令物件 dbconn.Open();//開啟連線 comm.ExecuteNonQuery();//執行命令 dbconn.Close();//關閉連線 }

server1.asmx.cs檔案

[WebMethod(Description = "選擇題練習")]
        public DataSet choice(string chapter, string diff)//選擇題練習
        {
            string
strsql = "select * from multiple_choice where chapter = '" + chapter + "' and hard = '" + diff + "'"; DataSet dataSet = new DataSet(); //建立資料據 dataSet = database.GetDataSet(strsql, "usernamelist"); return dataSet; }

這裡寫圖片描述這裡寫圖片描述
這裡寫圖片描述

這裡寫圖片描述
android端程式碼:

public class SecondActivity extends ActionBarActivity {    
    private SoapObject result;          //返回xml
    private String num;                 //題號
    private String details;             //題幹
    private String A;                   //選項
    private String B;
    private String C;
    private String D;
    private String answer;              //答案
    private int i = 0;
    private int qnum;                   //返回資料集中的條數
    private Button button1;             //按鈕
    private Button button2;
    private Button button3;
    private RadioButton radio0;         //單選框
    private RadioButton radio1;
    private RadioButton radio2;
    private RadioButton radio3;
    private TextView textView2;
    private RadioGroup radioGroup1;     //組合單選框

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);// 呼叫父類 方法
        requestWindowFeature(Window.FEATURE_NO_TITLE);// 隱藏標題欄
        setContentView(R.layout.mc);// 載入一個佈局

        String nameSpace = "http://tempuri.org/";                       //獲取伺服器基本資訊
        String methodName = "choice";
        String endPoint = "http://192.168.1.102:8001/Service1.asmx";
        String soapAction = "http://tempuri.org/choice";
        String chapter = Select.chapter;
        String diff = Select.hard;
        radioGroup1 = (RadioGroup) findViewById(R.id.radioGroup1);
        MyThread t = new MyThread(nameSpace, methodName, endPoint, soapAction,
                chapter, diff, i);                                                      // 新執行緒
        t.start();
        try {
            t.join();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        TextView textView1 = (TextView) findViewById(R.id.textView1);                   
        textView1.setText(num + "、" + details);     //獲取題目資訊
        radio0 = (RadioButton) findViewById(R.id.radio0);
        radio0.setText("A、" + A);
        radio1 = (RadioButton) findViewById(R.id.radio1);
        radio1.setText("B、" + B);
        radio2 = (RadioButton) findViewById(R.id.radio2);
        radio2.setText("C、" + C);
        radio3 = (RadioButton) findViewById(R.id.radio3);
        radio3.setText("D、" + D);
        textView2 = (TextView) findViewById(R.id.textView2);

        button2 = (Button) findViewById(R.id.button2);                      //檢視答案按鈕
        button2.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                if (radio0.isChecked()) {                       //判斷使用者解答是否正確
                    textView2.setText("");
                    if (answer.equals("A")) {
                        textView2.setText("答案正確!");
                    } else {
                        textView2.setText("答案錯誤!正確答案為:" + answer);
                    }
                } else if (radio1.isChecked()) {
                    textView2.setText("");
                    if (answer.equals("B")) {
                        textView2.setText("答案正確!");
                    } else {
                        textView2.setText("答案錯誤!正確答案為:" + answer);
                    }
                } else if (radio2.isChecked()) {
                    textView2.setText("");
                    if (answer.equals("C")) {
                        textView2.setText("答案正確!");
                    } else {
                        textView2.setText("答案錯誤!正確答案為:" + answer);
                    }
                } else if (radio3.isChecked()) {
                    textView2.setText("");
                    if (answer.equals("D")) {
                        textView2.setText("答案正確!");
                    } else {
                        textView2.setText("答案錯誤!正確答案為:" + answer);
                    }
                }

            }
        });
        button1 = (Button) findViewById(R.id.button1);                      //下一題按鈕
        button1.setOnClickListener(new OnClickListener() {

            public void onClick(View arg0) {

                // TODO Auto-generated method stub
                textView2.setText("");          
                radioGroup1.clearCheck();
                if (i < qnum-1) {                               //獲取伺服器中儲存的下一題的資訊
                    String nameSpace = "http://tempuri.org/";
                    String methodName = "choice";
                    String endPoint = "http://192.168.1.102:8001/Service1.asmx";
                    String soapAction = "http://tempuri.org/choice";
                    String chapter = Select.chapter;
                    String diff = Select.hard;
                    i++;
                    MyThread t = new MyThread(nameSpace, methodName, endPoint,
                            soapAction, chapter, diff, i);// 新執行緒
                    t.start();
                    try {
                        t.join();
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    TextView textView1 = (TextView) findViewById(R.id.textView1);
                    textView1.setText(num + "、" + details);
                    RadioButton radio0 = (RadioButton) findViewById(R.id.radio0);
                    radio0.setText("A、" + A);
                    RadioButton radio1 = (RadioButton) findViewById(R.id.radio1);
                    radio1.setText("B、" + B);
                    RadioButton radio2 = (RadioButton) findViewById(R.id.radio2);
                    radio2.setText("C、" + C);
                    RadioButton radio3 = (RadioButton) findViewById(R.id.radio3);
                    radio3.setText("D、" + D);

                } else {
                    Toast.makeText(SecondActivity.this, "題目已做完!",
                            Toast.LENGTH_SHORT).show();
                }
            }
        });
        button3 = (Button) findViewById(R.id.button3);                          //上一題按鈕
        button3.setOnClickListener(new OnClickListener() {

            public void onClick(View arg0) {

                // TODO Auto-generated method stub
                textView2.setText("");          
                radioGroup1.clearCheck();
                if (i < qnum&&i>=1) {
                    String nameSpace = "http://tempuri.org/";       //獲取伺服器中上一題的題目資訊
                    String methodName = "choice";
                    String endPoint = "http://192.168.1.102:8001/Service1.asmx";
                    String soapAction = "http://tempuri.org/choice";
                    String chapter = Select.chapter;
                    String diff = Select.hard;
                    i--;
                    MyThread t = new MyThread(nameSpace, methodName, endPoint,
                            soapAction, chapter, diff, i);// 新執行緒
                    t.start();
                    try {
                        t.join();
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    TextView textView1 = (TextView) findViewById(R.id.textView1);
                    textView1.setText(num + "、" + details);
                    RadioButton radio0 = (RadioButton) findViewById(R.id.radio0);
                    radio0.setText("A、" + A);
                    RadioButton radio1 = (RadioButton) findViewById(R.id.radio1);
                    radio1.setText("B、" + B);
                    RadioButton radio2 = (RadioButton) findViewById(R.id.radio2);
                    radio2.setText("C、" + C);
                    RadioButton radio3 = (RadioButton) findViewById(R.id.radio3);
                    radio3.setText("D、" + D);
                    if (radio0.isChecked()) {
                        textView2.setText("");
                        if (answer.equals("A")) {
                            textView2.setText("答案正確!");
                        } else {
                            textView2.setText("答案錯誤!正確答案為:" + answer);
                        }
                    } else if (radio1.isChecked()) {
                        textView2.setText("");
                        if (answer.equals("B")) {
                            textView2.setText("答案正確!");
                        } else {
                            textView2.setText("答案錯誤!正確答案為:" + answer);
                        }

                    } else if (radio2.isChecked()) {
                        textView2.setText("");
                        if (answer.equals("C")) {
                            textView2.setText("答案正確!");
                        } else {
                            textView2.setText("答案錯誤!正確答案為:" + answer);
                        }

                    } else if (radio3.isChecked()) {
                        textView2.setText("");
                        if (answer.equals("D")) {
                            textView2.setText("答案正確!");
                        } else {
                            textView2.setText("答案錯誤!正確答案為:" + answer);
                        }
                    }

                } else {                                        //若已經是第一題,則提示資訊
                    Toast.makeText(SecondActivity.this, "同學,這已經第一題了哦",
                            Toast.LENGTH_SHORT).show();
                }
            }
        });

    }

    // 通過Runnable介面和Thread類,得到執行緒返回值(子執行緒的具體實現)
    private class MyThread extends Thread {                                                 

        private String nameSpace;
        private String methodName;
        private String endPoint;
        private String soapAction;
        private String chapter;
        private String diff;
        private int i;

        public MyThread(String nameSpace, String methodName, String endPoint,
                String soapAction, String chapter, String diff, int i) {
            this.nameSpace = nameSpace;
            this.methodName = methodName;
            this.endPoint = endPoint;
            this.soapAction = soapAction;
            this.chapter = chapter;
            this.diff = diff;
            this.i = i;
        }

        @Override
        public void run() {
            result = getRemoteInfo(nameSpace, methodName, endPoint, soapAction,
                    chapter, diff, i);
        }

    }

    public SoapObject getRemoteInfo(String nameSpace, String methodName,
            String endPoint, String soapAction, String chapter, String diff,
            int i) {
        SoapObject rpc = new SoapObject(nameSpace, methodName);
        rpc.addProperty("chapter", chapter);
        rpc.addProperty("diff", diff);
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                SoapEnvelope.VER11);
        envelope.bodyOut = rpc;
        envelope.dotNet = true;
        envelope.setOutputSoapObject(rpc);
        HttpTransportSE transport = new HttpTransportSE(endPoint);
        try {
            // 呼叫WebService
            transport.call(soapAction, envelope);
        } catch (Exception e) {
            e.printStackTrace();
        }

        // 獲取返回的資料
        try {
            result = (SoapObject) envelope.getResponse();
        } catch (SoapFault e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        SoapObject table = (SoapObject) result.getProperty("diffgram");

        SoapObject table1 = (SoapObject) table.getProperty("NewDataSet");
        qnum = table1.getPropertyCount();
        SoapObject table2 = (SoapObject) table1.getProperty(i);

        num = (String) table2.getPropertyAsString("num");
        details = (String) table2.getPropertyAsString("details");
        A = (String) table2.getPropertyAsString("A");
        B = (String) table2.getPropertyAsString("B");
        C = (String) table2.getPropertyAsString("C");
        D = (String) table2.getPropertyAsString("D");
        answer = (String) table2.getPropertyAsString("answer").trim();
        return result;
    }
}
 相信如果你看過我上一篇文章的話,這一篇你只需要看看最後這幾行程式碼。

相關推薦

android利用ksoap2返回複雜資料資料dataset

在讀這篇文章之前建議你先讀一下上一篇文章android如何使用ksoap2對sql server的操作實現登陸,原理是一樣的只是返回的資料不同而已。 web端程式碼: //database.cs檔案利用ADO.NET技術 public DataSet G

RDLC 表格和文字框實現多資料單備註簡單

RDLC 中表格和文字框實現單備註,多資料的表現格式 將文字框放在表的旁邊,但是這個文字框只是對錶資料的一個備註 由於備註內容部分不能放在表的重複單元格中(因為這樣會讓備註內容對每一條資料都重複一次),故而只能用文字框,而文字框設定邊框後,又不能達到對所有重複資料進行單備註

Android利用BroadcastReceiver接收本地廣播與跨專案APP接收廣播

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.broadcastrece

909422229_ajaxFileUpload 上傳返回結果docmeent資料解析失敗的問題

一、ajaxFileUpload 上傳使用。 問題: 檔案上傳成功後無法獲取後臺的資料,返回資料列印是ducment。 事例:一些業務性程式碼已經遮蔽,只展示問題程式碼。 下面程式碼在列印返回的資料不符,後臺成功會返回一個 1的標記,但是列印結果是docment物件。 $.a

Android通過Parcelable傳遞複雜的物件資料和簡單的物件資料

二次驗證:絕對有效 轉:https://blog.csdn.net/u014614038/article/details/56279075 Android推薦可以通過Parcelable來傳遞自定義的資料(),比如以下的資料體: public class GradeInfoForSear

定義棧的資料結構請在該型別中實現一個能夠得到棧中所含最小元素的min函式時間複雜度應為O1

import java.util.Stack; public class Solution { private Stack<Integer> min_stack=new Stack<Integer>(); private Stack<Integer&

定義棧的資料結構請在該型別中實現一個能夠得到棧中所含最小元素的min函式時間複雜度應為O1

/** 思路:利用兩個棧來實現,一個主棧正常壓棧出棧,一個輔助棧用來儲存主棧所有值的最小值, 壓棧時,當壓入的值比輔助棧棧頂值大時,主棧正常壓棧,輔助棧不壓棧,小於等於二者都壓棧; 出棧時,當主棧和輔助棧棧頂元素不相等時,主棧正常出棧,輔助棧不出棧。 */ class Sol

定義棧的資料結構請在該型別中實現一個能夠得到棧中所含最小元素的min函式 時間複雜度應為O1

import java.util.Stack; /** * 定義棧的資料結構,請在該型別中實現一個能夠得到棧中所含最小元素的min函式 * (時間複雜度應為O(1))。 */ public class Solution { static Stack<Integer> da

Android:使用Gson解析複雜的JSON資料

JSON(JavaScript Object Notation) 是一種輕量級的資料交換格式,目前廣泛使用。本文主要講解android如何解析複雜格式的JSON資料,適合android初學者和初步接觸JSON的人。 1.JSON相關介紹: 首先了解一下JSO

利用伺服器返回header來傳輸資料

提醒:本文最後更新於 3750 天前,文中所描述的資訊可能已發生改變,請謹慎使用。 在Ajax程式設計時,經常需要從服務端獲取資料。通常情況下,我們是直接把要傳輸的資料放在response正文中,再用responseText或者responseXML來得到內容。最近偶然發現,有時候也可以把資料放在h

定義棧的資料結構請在該型別中實現一個能夠得到棧最小元素的min函式。時間複雜度都是O1

定義棧的資料結構,請在該型別中實現一個能夠得到棧最小元素的min函式。要求:使得時間複雜度都是O(1) 完成如下的函式: import java.util.Stack; public class Solution { public void pus

資料結構---設計一個棧push, pop, min 時間複雜度都是 O1

普通的棧,push, pop 操作的複雜度是 O(1), 但是如果要找出其中的最小值,則需要 O(N)的時間。 題目要求 min 複雜度也是 O(1), 做法便是 空間換時間,每一步棧的最小值都用一個

利用JQGrid動態刪除多行資料行號即刪除/增加資料變化的問題

利用JQGrid刪除多行資料: 所選中行號依次為 1、2、4 利用FireBug 檢視由 js 動態生成的 html 標籤可以驗證: 可以看出每行的行號就是它的 id號 點選刪除後,如果會動態變化那麼繳費記錄編號為3 的這行資料行號應該變為 1 : Html:

php+js 防止被抓包篡改資料資料簽名校驗

簽名金鑰,這個是自己生成的,需要客戶端+服務端一致。 <?php /** * 獲取簽名 * @param $data 提交的資料 * @param $key 安全金鑰 * @return bool */ function signature($data, $key) {

演算法基礎:資料型別基礎結構

基礎概念 一、資料型別 基本資料型別一般長度 (注意以下的 long long 實際上指的是 unsigned long long 型別) (long long 型別數值範圍是-9223372036854775808 ~ 9223372036854775807)差不多範圍是

資料運營】在運營中為什麼文字分析遠比數值型分析重要?一個實際案例五點分析

https://www.pmcaff.com/article/index/408451832537216?from=profile 本文是《資料分析中,文字分析遠比數值型分析重要!》的下篇,以一個實際案例來聊聊文字分析在實際運營中如何落地。行為脈絡如下:先簡要講述文字分析的分支---情緒分析的基本原

arcengine獲取gdb中所有的資料資料

   FileGDBWorkspaceFactoryClass fac = new FileGDBWorkspaceFactoryClass();  IWorkspace workspace = fac.OpenFromFile(pathname, 0); IE

資料晉級之路5HadoopSparkStorm綜合比較

大資料框架:Spark vs Hadoop vs Storm 目錄 Hadoop Spark Storm   大資料時代,TB級甚至PB級資料已經超過單機尺度的資料處理,分散式處理系統應運而生。 知識預熱 「專治不明覺厲」之“大資料

Oracle遠端資料建物化檢視materialized建立簡單記錄以及DBLINK的建立

目的:實現遠端資料庫訪問及其相應表的定時同步 一、遠端資料庫dblink的建立 select * from dba_db_links; select * from user_sys_privs;--查詢使用者許可權 1、檢視scott使用者是否具備建立database link 許可權