从SOAP Web服务获取数据作为值数组

时间:2015-04-27 07:40:06

标签: java android web-services soap

我在android studio中有项目,我的数据库来自Web服务。

我使用下面的代码在表格中显示数据。但它给了我这个错误。

  

不幸的是,应用已停止。

任何人都可以帮我解决这个问题

我的代码:

Student.java

package com....;

import org.ksoap2.serialization.KvmSerializable;
import org.ksoap2.serialization.PropertyInfo;

import java.util.Hashtable;

/**
 * Created by SA on 4/27/2015.
 */
class Student implements KvmSerializable
{
String Course_ID;
    String Section;
    String Time;
    //String Days;
    public Student()
    {}
    public Student(String Course_ID,String Section,String Time){
        this.Course_ID=Course_ID;
        this.Section=Section;
        this.Time=Time;
       // this.Days=Days;
    }

    public Object getProperty(int arg0) {

        switch(arg0)
        {
            case 0:
                return Course_ID;
            case 1:
                return Section;
            case 2:
                return Time;

        }
        return null;
    }
    public int getPropertyCount() {
        return 3;
    }
    @SuppressWarnings("rawtypes")
    public void getPropertyInfo(int index, Hashtable arg1, PropertyInfo info) {
        switch(index)
        {
            case 0:
                info.type = PropertyInfo.STRING_CLASS;
                info.name= "Course_ID";
                break;
            case 1:
                info.type = PropertyInfo.STRING_CLASS;
                info.name = "Section";
                break;
            case 2:
                info.type = PropertyInfo.STRING_CLASS;
                info.name = "Time";
                break;


            default:
                break;
        }
    }
    public void setProperty(int index, Object value) {
        switch(index)
        {
            case 0:
                Course_ID = value.toString();
                break;
            case 1:
                Section = value.toString();
                break;
            case 2:
                Time = value.toString();
                break;


            default:
                break;
        }
    }
}

LOGIN.java

package com....;
import java.io.IOException;
import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.util.ArrayList;
import java.util.ArrayList;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import java.util.Arrays;
import java.util.Hashtable;
import org.ksoap2.serialization.KvmSerializable;
import org.ksoap2.serialization.PropertyInfo;
import android.app.Activity;

import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.TableRow.LayoutParams;


public class LOGIN extends Activity
{
    EditText editText= (EditText) findViewById(R.id.editText);
    boolean timeoutexcep=false,httpexcep=false,generalexcep=false;
    Student [] personList2;
    TableLayout tl1;
    String[] Course_ID,Section,Time,Days;
    int i;
    String SID="201001810";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        new propdetail().execute();
    }

    class propdetail extends AsyncTask<Void, Void, Void> {

        private final ProgressDialog dialog = new ProgressDialog(LOGIN.this);

        @Override
        protected void onPreExecute() {
            this.dialog.setMessage("Loading data");
            this.dialog.show();
        }

        @Override
        protected Void doInBackground(Void... unused) {

            final String NAMESPACE = "http://tempuri.org/";
            final String URL = "http://192.168.1.101/SAWSANS/connect.asmx";
            final String SOAP_ACTION = "http://tempuri.org/GetECources";
            final String METHOD_NAME = "GetECources";

            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

            request.addProperty("SID",SID);


            Student C = new Student();
            PropertyInfo pi = new PropertyInfo();
            pi.setName("Student");
            pi.setValue(C);
            pi.setType(C.getClass());
            request.addProperty(pi);
            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            envelope.dotNet = true;
            envelope.setOutputSoapObject(request);
            envelope.addMapping(NAMESPACE, "Student", new Student().getClass());
            HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
            androidHttpTransport.debug = true;

            try {
                androidHttpTransport.call(SOAP_ACTION, envelope);
                SoapObject response = (SoapObject) envelope.getResponse();

                Log.i("SelfAdvising", request.toString());
                System.out.println("check dddddddddddddd" + response);

                envelope.addMapping(NAMESPACE,"Student",new Student().getClass());
                androidHttpTransport.call(SOAP_ACTION, envelope);
                personList2 = new Student[response.getPropertyCount()];

                for (int j = 0; j < personList2.length; j++) {
                    SoapObject so = (SoapObject) response.getProperty(j);
                    Student person2 = new Student();
                    person2.Course_ID = so.getProperty(0).toString();
                    person2.Section= so.getPropertyAsString(1).toString();
                    person2.Time=so.getPropertyAsString(6).toString();
                   // person2.Days=so.getPropertyAsString(7).toString();
                    personList2[j] = person2;
                }

                Course_ID= new String[personList2.length];
                Section = new String[personList2.length];
                Time = new String[personList2.length];
                //Days = new String[personList2.length];

                for (int i = 0; i < personList2.length; i++)
                {
                    Section[i] = Arrays.asList(personList2[i].Section).toString();
                    Time[i] = Arrays.asList(personList2[i].Time).toString();
                    Course_ID[i] = Arrays.asList(personList2[i].Course_ID).toString();
                   // Days[i] = Arrays.asList(personList2[i].Days).toString();

                    System.out.println(Course_ID[i]);
                    System.out.println(Section[i]);
                    System.out.println(Time[i]);
                   // System.out.println(Days[i]);
                }
            }
            catch(SocketTimeoutException e){
                timeoutexcep=true;
                e.printStackTrace();
            }
            catch(ConnectException e){
                httpexcep=true;
                e.printStackTrace();
            }
            catch (Exception e) {
                generalexcep=true;
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            if (this.dialog.isShowing()) {
                this.dialog.dismiss();
            }

            if(timeoutexcep){
                Toast.makeText(LOGIN.this, "Unable to connect to server, Please try again later",Toast.LENGTH_LONG).show();
            }
            else if(httpexcep){
                Toast.makeText(LOGIN.this, "Please check your Internet connection",Toast.LENGTH_LONG).show();
            }
            else if(generalexcep){
                Toast.makeText(LOGIN.this, "Please try later",Toast.LENGTH_LONG).show();
            }
            else {
                tableview();
            }
            timeoutexcep=false;httpexcep=false;generalexcep=false;
        }
    }
    public void tableview(){

        tl1 = (TableLayout) findViewById(R.id.tl);
        for (i = 0; i < personList2.length; i++)
        {
            final TableRow tr = new TableRow(this);
            LayoutParams lp = new LayoutParams(150,LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);

            tr.setId(i);

            tr.setLayoutParams(lp);
            lp.setMargins(0, 20, 0,0);

            final TextView tvLeft = new TextView(this);
            tvLeft.setLayoutParams(lp);
            tvLeft.setText(Course_ID[i]);

            final TextView tvCenter = new TextView(this);
            tvCenter.setLayoutParams(lp);
            tvCenter.setText(Section[i]);

            final TextView tvRight = new TextView(this);
            tvRight.setLayoutParams(lp);
            tvRight.setText(Time[i]);

            //final TextView tvend = new TextView(this);
            //tvend.setLayoutParams(lp);
            //tvend.setText(Days[i]);

            tr.addView(tvLeft);
            tr.addView(tvCenter);
            tr.addView(tvRight);
            //tr.addView(tvend);

            tl1.addView(tr, new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_login, menu);
        return true;
    }
}

0 个答案:

没有答案