簡體   English   中英

使用Web服務或ASP.NET的Android登錄和注冊使用KSoap2拒絕傳遞的字符串

[英]Android Login & Registration using Web service or ASP.NET denying a passed strings with KSoap2

我正在創建一個Android應用程序,該應用程序將通過使用SOAP Web服務在androidASP.NET之間進行通信,而我正在使用KSoap2做到這一點。 我要做的是注冊一個帳戶,該帳戶需要一些用戶信息以及“ 注冊碼 ”,然后我使用連接網絡服務來連接到ASP的網絡服務。

我的ASP.NET IIS工作正常。

知道價值正在傳遞到Web服務上。 可能在.net上發送值並在android的sql服務器上插入。

登記碼

public class Register extends Activity {
EditText edit_username;
EditText edit_displayName;
EditText edit_email;
EditText edit_password;
EditText edit_confirmPassword;
Button btn_register, btn_login;
Button btn_reset;
String user;
String disp, pass, email, conpass;
boolean RegisterStatus;
static boolean errored = false;
ProgressBar webservicePG;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.register);
    edit_username = (EditText) findViewById(R.id.edit_username);
    edit_displayName = (EditText) findViewById(R.id.edit_name);
    edit_email = (EditText) findViewById(R.id.edit_email);
    edit_password = (EditText) findViewById(R.id.edit_password);
    edit_confirmPassword = (EditText) findViewById(R.id.edit_confirmPassword);
    btn_register = (Button) findViewById(R.id.btn_register);
    btn_login = (Button) findViewById(R.id.reg_back_btn);
    btn_reset = (Button) findViewById(R.id.btn_reset);

    btn_reset.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            edit_username.setText("");
            edit_displayName.setText("");
            edit_email.setText("");
            edit_password.setText("");
            edit_confirmPassword.setText("");
        }
    });
    btn_login.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            Intent i = new Intent(getApplicationContext(), Login.class);
            startActivity(i);
        }
    });
    btn_register.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            if (edit_username.getText().length() != 0
                    && edit_username.getText().toString() != "") {
                if (edit_displayName.getText().length() != 0
                        && edit_displayName.getText().toString() != "") {

                    if (edit_email.getText().length() != 0
                            && edit_email.getText().toString() != "") {

                        if (edit_password.getText().length() != 0
                                && edit_password.getText().toString() != "") {

                            if (edit_confirmPassword.getText().length() != 0
                                    && edit_confirmPassword.getText()
                                            .toString() != "") {

                                user = edit_username.getText().toString();
                                disp = edit_displayName.getText()
                                        .toString();
                                pass = edit_password.getText().toString();
                                email = edit_email.getText().toString();
                                conpass = edit_confirmPassword.getText()
                                        .toString();
                                // statusTV.setText("");
                                // Create instance for AsyncCallWS
                                AsyncCallWS task = new AsyncCallWS();
                                // Call execute
                                task.execute();
                            }
                            // If Password text control is empty
                            else {
                                // statusTV.setText("Please enter Password");
                                Toast.makeText(getApplicationContext(),
                                        "Please enter Conform Password",
                                        Toast.LENGTH_LONG).show();
                                edit_confirmPassword
                                        .setError("Cannot Be Empty");
                            }
                            // If Username text control is empty

                        } else {
                            // statusTV.setText("Please enter Username");
                            Toast.makeText(getApplicationContext(),
                                    "Please enter Password",
                                    Toast.LENGTH_LONG).show();
                            edit_password.setError("Cannot Be Empty");
                        }
                    } else {
                        // statusTV.setText("Please enter Username");
                        Toast.makeText(getApplicationContext(),
                                "Please enter Email Address",
                                Toast.LENGTH_LONG).show();
                        edit_email.setError("Cannot Be Empty");
                    }
                } else {
                    // statusTV.setText("Please enter Username");
                    Toast.makeText(getApplicationContext(),
                            "Please enter Dispalayname", Toast.LENGTH_LONG)
                            .show();
                    edit_displayName.setError("Cannot Be Empty");
                }
            } else {
                // statusTV.setText("Please enter Username");
                Toast.makeText(getApplicationContext(),
                        "Please enter Username", Toast.LENGTH_LONG).show();
                edit_username.setError("Cannot Be Empty");
            }

        }
    });
}

private class AsyncCallWS extends AsyncTask<String, Void, Void> {
    @Override
    protected Void doInBackground(String... params) {
        // Call Web Method
        RegisterStatus = WebService.invokeLoginWS(user, disp, email, pass,
                conpass, "AuthenticateUser");
        return null;
    }

    @Override
    // Once WebService returns response
    protected void onPostExecute(Void result) {
        // Make Progress Bar invisible
        webservicePG.setVisibility(View.INVISIBLE);
        Intent intObj = new Intent(Register.this, Login.class);
        // Error status is false
        if (!errored) {
            // Based on Boolean value returned from WebService
            if (RegisterStatus) {
                // Navigate to Home Screen
                startActivity(intObj);
            } else {
                // Set Error message
                Toast.makeText(getApplicationContext(),
                        "Register Failed, try again", Toast.LENGTH_LONG)
                        .show();
            }
            // Error status is true
        } else {
            // /statusTV.setText("Error occured in invoking webservice");
            Toast.makeText(getApplicationContext(),
                    "Error occured in invoking webservice",
                    Toast.LENGTH_LONG).show();
        }
        // Re-initialize Error Status to False
        errored = false;
    }

    @Override
    // Make Progress Bar visible
    protected void onPreExecute() {
        webservicePG.setVisibility(View.VISIBLE);
    }

    @Override
    protected void onProgressUpdate(Void... values) {
    }

  }
 }

網絡服務代碼

public class WebService {
// Namespace of the Webservice - can be found in WSDL
private static String NAMESPACE = "http://tempuri.org/";
// Webservice URL - WSDL File location
private static String URL = "http://192.168.3.3/LoginCheck/Service.asmx";// Make
                                                                            // sure
                                                                            // you
                                                                            // changed
                                                                            // IP
                                                                            // address
// SOAP Action URI again Namespace + Web method name
private static String SOAP_ACTION = "http://tempuri.org/";

public static boolean invokeLoginWS(String userName, String dispalyname,
        String Email, String password, String conpassWord,
        String webMethName) {
    boolean loginStatus = false;
    // Create request
    SoapObject request = new SoapObject(NAMESPACE, webMethName);
    // Property which holds input parameters
    PropertyInfo unamePI = new PropertyInfo();
    PropertyInfo passPI = new PropertyInfo();
    // Set Username
    unamePI.setName("username");
    // Set Value
    unamePI.setValue(userName);
    // Set dataType
    unamePI.setType(String.class);
    // Add the property to request object
    request.addProperty(unamePI);
    passPI.setName("dispalyname");
    // Set dataType
    passPI.setValue(dispalyname);
    // Set dataType
    passPI.setType(String.class);// Set Password
    passPI.setName("Email");
    // Set dataType
    passPI.setValue(Email);
    // Set dataType
    passPI.setType(String.class);
    // Add the property to request object
    request.addProperty(passPI);

    // Set Password
    passPI.setName("password");
    // Set dataType
    passPI.setValue(password);
    // Set dataType
    passPI.setType(String.class);
    // Add the property to request object
    request.addProperty(passPI);
    // Set dataType
    // Set Password
    passPI.setName("conpassWord");
    passPI.setValue(conpassWord);
    // Set dataType
    passPI.setType(String.class);
    // Add the property to request object
    request.addProperty(passPI);
    // Create envelope
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
            SoapEnvelope.VER11);
    envelope.dotNet = true;
    // Set output SOAP object
    envelope.setOutputSoapObject(request);
    // Create HTTP call object
    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

    try {
        // Invoke web service
        androidHttpTransport.call(SOAP_ACTION + webMethName, envelope);
        // Get the response
        SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
        // Assign it to boolean variable variable
        loginStatus = Boolean.parseBoolean(response.toString());

    } catch (Exception e) {
        // Assign Error Status true in static variable 'errored'
        Login.errored = true;
        e.printStackTrace();
    }
    // Return booleam to calling object
    return loginStatus;
 }
}

從android發送到服務器的請求可以在服務器日志中列出。 如果發送請求時沒有收到任何錯誤,則服務器可能會收到該錯誤。 否則,請檢查Internet連接,並確保在清單中添加Internet許可,還請參考ksoap lib至Java構建路徑

首先檢查您的WSDL文件,並檢查您的NAMESPACE = "http://tempuri.org/"是否正確? 檢查您的SOAP_ACTION = "http://tempuri.org/"是否正確? 在您的代碼中,在聲明NAMESPACE和SOAP_ACTION之前可以進行注釋,請遵循注釋和WSDL文件中的NAMESPACE和SOAP_ACTION。

SOAP Action = URI again Namespace + Web method name

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM