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;
    }
}
 相信如果你看過我上一篇文章的話,這一篇你只需要看看最後這幾行代碼。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章