为KSOAP2投射SoapObject的问题

时间:2015-05-14 17:40:16

标签: java android asp.net list android-ksoap2

好的,所以我试图使用KSOAP2将列表对象从ASP传递给Android。我完全能够连接到Web服务,我已经测试过该服务确实返回了一个基本的bool,但是我需要服务来返回一个填充了登录变量的列表对象。我收到此错误:soapPrimitiveData()上的错误org.ksoap2.SoapFault无法强制转换为org.ksoap2.serialization.SoapObject

Java:

public class MainActivity extends ActionBarActivity {
private final String NAMESPACE = "http://tempuri.org/";
private final String URL = "http://www.mycompanysURL.net/CompanyService/ReportingService.asmx";

String user_id;
String password;

TextView text;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button signin = (Button) findViewById(R.id.btnLogin);
    text = (TextView) findViewById(R.id.txtOut);
    signin.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {


            EditText etxt_user = (EditText) findViewById(R.id.txtUser);
            user_id = etxt_user.getText().toString();
            EditText etxt_password = (EditText) findViewById(R.id.txtPass);
            password = etxt_password.getText().toString();

            new LoginTask().execute();

        }
    });
}  

private boolean doLogin(String user_id, String password) {
    String ip = Utils.getIPAddress(true);

    boolean result = false;
    final String SOAP_ACTION = "http://tempuri.org/GetLogin";
    final String METHOD_NAME = "GetLogin";
    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

    request.addProperty("User", user_id);
    request.addProperty("Pass", password);
    request.addProperty("ip",ip);

    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    envelope.dotNet = true;
    envelope.setOutputSoapObject(request);

    // Make the soap call.
    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
    try {
        androidHttpTransport.call(SOAP_ACTION, envelope);

        // Get the SoapResult from the envelope body.
        //Error seems to happen here.
        SoapObject resultRequestSOAP = (SoapObject) envelope.bodyIn;

        SoapObject root = (SoapObject) resultRequestSOAP.getProperty(0);
        SoapObject s_deals = (SoapObject) root.getProperty("FOO_DEALS");

        for (int i = 0; i < s_deals.getPropertyCount(); i++)
        {
            Object property = s_deals.getProperty(i);
            if (property instanceof SoapObject)
            {
                SoapObject category_list = (SoapObject) property;
                String LoginID = category_list.getProperty("Login_ID").toString();
                String UserID = category_list.getProperty("UserID").toString();
                String Login_Access = category_list.getProperty("Login_Access").toString();
                String Login_CompID = category_list.getProperty("Login_CompID").toString();
                String Login_ProfileID = category_list.getProperty("Login_ProfileID").toString();
                String Login_DisplayName = category_list.getProperty("Login_DisplayName").toString();
                String Login_CustomerType = category_list.getProperty("Login_CustomerType").toString();
                String active = category_list.getProperty("active").toString();

                if (active == "1") {
                    result = true;
                    text.setText("Logged In");
                } else {

                    text.setText("Not Logged In");
                }
            }
        }
    } catch (SocketException ex) {
        Log.e("Error : ", "Error on soapPrimitiveData() " + ex.getMessage());
        ex.printStackTrace();
    } catch (Exception e) {
        Log.e("Error : ", "Error on soapPrimitiveData() " + e.getMessage());
        e.printStackTrace();
    }
    return result;

}


private class LoginTask extends AsyncTask<Void, Void, Void> {

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

    protected void onPreExecute() {

        this.dialog.setMessage("Logging in...");
        this.dialog.show();

    }


    protected Void doInBackground(final Void... unused) {

        boolean auth = doLogin(user_id, password);
        System.out.println(auth);

        return null;
    }


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

ASP:

 [WebMethod]
 public List<LoginObject> GetLogin(string User, string Pass, string ip)
 {
    SqlConnection conn = new SqlConnection(my_db.credentials);
    SqlDataReader rdr = null;
    List<LoginObject> LoginList = new List<LoginObject>();

    bool login_in_ok = false;

    string date = DateTime.Now.ToString();
    string UserID1 = "";
    string Login_ID1 = "";
    string Login_Access1 = "";
    string Login_CompID1 = "";
    string Login_ProfileID1 = "";
    string Login_DisplayName1 = "";
    string Login_CustomerType1 = "";
    string active1 = "";

    try
    {
        conn.Open();
        SqlCommand cmd = new SqlCommand("usp_user_login", conn);

        cmd.Parameters.Add(new SqlParameter("@login", User));
        cmd.Parameters.Add(new SqlParameter("@pwd", Pass));
        cmd.Parameters.Add(new SqlParameter("@ip_addr", ip));
        cmd.Parameters.Add(new SqlParameter("@local_datetime", date));

        cmd.CommandType = CommandType.StoredProcedure;

        rdr = cmd.ExecuteReader();

        while (rdr.Read())
        {                
            UserID1 = rdr["UserID"].ToString();
            Login_ID1 = rdr["ID"].ToString();
            Login_Access1 = rdr["Access"].ToString();
            Login_CompID1 = rdr["CompanyID"].ToString();
            Login_ProfileID1 = rdr["ProfileID"].ToString();
            Login_DisplayName1 = rdr["Lname"].ToString() + " " + rdr["FName"].ToString();
            Login_CustomerType1 = rdr["login_customer_type"].ToString();
            active1 = rdr["Active"].ToString();                
        }           

    }
    finally
    {
        if (rdr != null)
        {
            rdr.Close();
        }

        if (conn != null)
        {
            conn.Close();
        }
    }          

    login_in_ok = active1 == "1" || active1 == "True" ? true : false;

    if (login_in_ok)
    {
        //Dataset for Andriod Login. 
        LoginList.Add(new LoginObject 
        { 
            Login_ID = Login_ID1, 
            UserID = UserID1, 
            Login_Access = Login_Access1,
            Login_CompID = Login_CompID1,
            Login_ProfileID = Login_ProfileID1,
            Login_DisplayName = Login_DisplayName1,
            Login_CustomerType = Login_CustomerType1,
            active = active1
        });
    }
    else
    {
        //Empty Set for Android to register
        LoginList.Add(new LoginObject
        {
            Login_ID = "",
            UserID = "",
            Login_Access = "0",
            Login_CompID = "",
            Login_ProfileID = "",
            Login_DisplayName = "",
            Login_CustomerType = "",
            active = active1
        });
    }

    return LoginList;
}

任何帮助都会很棒。

1 个答案:

答案 0 :(得分:0)

实际上这是有效的,我的存储过程存在问题......当其他开发人员改变事物并且不通知任何人时,必须喜欢它。