SOAP Web服务返回错误的响应

时间:2011-10-20 14:04:26

标签: android .net sql-server web-services ksoap2

我正在使用localhost ..我在.NET中创建了一个Web服务,它接收用户名和密码并对用户进行身份验证。我的Web服务代码是:http://pastebin.com/GQGtKSqq和我的日志猫显示这个:http://pastebin.com/czs5Fbc3

我的日志cat显示输出是来自我的Web服务的catch语句的语句。它实际上应该在try块中显示“成功”的语句。 当我从我的Android应用程序调用Web服务时,我得到的响应与我在浏览器中运行Web服务时得到的响应相同。我已将用户名和密码存储在SQL数据库中。任何人都可以帮助解决我的问题。我的android代码如下:

       package com.demo;

       import org.ksoap2.SoapEnvelope;
       import org.ksoap2.serialization.SoapObject;
       import org.ksoap2.serialization.SoapPrimitive;
       import org.ksoap2.serialization.SoapSerializationEnvelope;  
       import org.ksoap2.transport.AndroidHttpTransport;
       import android.app.Activity;
       import android.app.ProgressDialog;
       import android.os.AsyncTask;
       import android.os.Bundle;
       import android.util.Log;
       import android.view.View;
       import android.view.View.OnClickListener;
       import android.widget.Button;
       import android.widget.EditText;

        public class Login extends Activity {
private static final String SOAP_ACTION = "http://tempuri.org/GetLoginDetails";
private static final String METHOD_NAME = "GetLoginDetails";
private static final String NAMESPACE = "http://tempuri.org/";
private static final String URL = "http://10.0.2.2/testlogin/Service1.asmx";

@Override
public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button signin = (Button) findViewById(R.id.regsubmitbtn);

        signin.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {

                    String user_id;
                    String password;
                        EditText etxt_user = (EditText) findViewById(R.id.usereditlog);
                        user_id = etxt_user.getText().toString();
                        EditText etxt_password = (EditText)  findViewById(R.id.pwdeditlog);
                        password = etxt_password.getText().toString();

                        new LoginTask().execute();
                }
             });
        }


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

        String auth = null;
         private final ProgressDialog dialog = new ProgressDialog(
                    Login.this);

          protected void onPreExecute() {
                  this.dialog.setMessage("Logging in...");
                  this.dialog.show();
          }

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

            auth = doLogin("hello", "hello");
            return null; // don't interact with the ui!
    }

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


      private String doLogin(String user_id, String password) {

          SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
            request.addProperty("user", user_id);
           request.addProperty("password", password);
              SoapSerializationEnvelope soapenvelope = new SoapSerializationEnvelope(
                    SoapEnvelope.VER11);
           soapenvelope.dotNet = true; // Set this variable to true for
          soapenvelope.setOutputSoapObject(request);

    AndroidHttpTransport httptransport = new AndroidHttpTransport(URL);
    try {
            httptransport.call(SOAP_ACTION, soapenvelope);
            SoapPrimitive resultstring = (SoapPrimitive) soapenvelope
                            .getResponse();
            Log.d("Authenticaion", resultstring+"");
            System.out.println(resultstring);

            return resultstring + "";

    } catch (Exception e) {
            e.printStackTrace();
    }
    return "";
         } 
     }

1 个答案:

答案 0 :(得分:1)

这是:我确实喜欢这个

// ksoap2 calling wcf
public SoapPrimitive soapPrimitiveData(String tablename,String strBusinessUnit, String strExecutive, String strTerritoryCode,String strUField1) throws IOException,XmlPullParserException {
    SoapPrimitive responsesData = null;
    SoapObject requestData = new SoapObject(NAMESPACE, METHOD_TABLEDATA); // set

    Date date = new Date();
    DateFormat formatter = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss");
    String strDate = formatter.format(date);

    requestData.addProperty("strBusinessUnit", strBusinessUnit);
    requestData.addProperty("strExecutive", strExecutive);
    requestData.addProperty("strTableName", tablename);
    requestData.addProperty("strDate", strDate);
    requestData.addProperty("strTerritoryCode", strTerritoryCode);
    requestData.addProperty("strUField1", strUField1);
    SoapSerializationEnvelope envelopes = new SoapSerializationEnvelope(SoapEnvelope.VER11); // put all required data into a soap//// envelope
    envelopes.dotNet = true;
    envelopes.setOutputSoapObject(requestData);
    AndroidHttpTransport httpTransport = new AndroidHttpTransport(APPURL);
    httpTransport.debug = true;

    try {
        httpTransport.call(SOAP_ACTION1, envelopes);
        responsesData = (SoapPrimitive) envelopes.getResponse();


    } 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 responsesData;
}

你的代码是正确的。

在你的'onpostexecute()中,你必须写下一个活动呼叫部分......

         @Override
    protected void onPostExecute(Void result) {

        if (this.dialog.isShowing()) {
            this.dialog.dismiss();
        }
        Toast.makeText(DownlaodTableActivity.this,"All The tables Downloaded Successfully",Toast.LENGTH_SHORT).show();


        if(comingFrom.equals("Success")){
            Intent i = new Intent(getBaseContext(), SettingScreenActivity.class);
            View vi = SettingActivityGroup.group.getLocalActivityManager().startActivity("SettingScreenActivity", i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView();
            SettingActivityGroup.group.replaceView(vi);


        }else{
            Intent showContent = new Intent(getApplicationContext(),MainActivity.class);
            startActivity(showContent);
        }
    }