我无法从Android应用程序登录到.asmx webservice [英] I am unable to Login to the .asmx webservice from Android Application

查看:68
本文介绍了我无法从Android应用程序登录到.asmx webservice的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试登录到我的Android应用程序在线的.asmx网站我创建了三个类如下



CheckDNLoginActivity.java

HomeActivity.java

WebService.java如下:



这是我的CheckDNLoginActivity.java





I am trying to log in to the .asmx website which is online from my android application i created three classes as follows

CheckDNLoginActivity.java
HomeActivity.java
WebService.java as follows:

Here is my CheckDNLoginActivity.java


import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;

public class CheckDNLoginActivity extends Activity {
//Set Error Status
static boolean errored = false;
Button b;
TextView statusTV;
EditText userNameET , passWordET;   
ProgressBar webservicePG;
String editTextUsername;
boolean loginStatus;
String editTextPassword;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //Name Text control
    userNameET = (EditText) findViewById(R.id.editText1);
    passWordET = (EditText) findViewById(R.id.editText2);
    //Display Text control
    statusTV = (TextView) findViewById(R.id.tv_result);
    //Button to trigger web service invocation
    b = (Button) findViewById(R.id.button1);
    //Display progress bar until web service invocation completes
    webservicePG = (ProgressBar) findViewById(R.id.progressBar1);
    //Button Click Listener
    b.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            //Check if text controls are not empty
            if (userNameET.getText().length() != 0 && userNameET.getText().toString() != "") {
                if(passWordET.getText().length() != 0 && passWordET.getText().toString() != ""){
                    editTextUsername = userNameET.getText().toString();
                    editTextPassword = passWordET.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");
                }
            //If Username text control is empty
            } else {
                statusTV.setText("Please enter Username");
            }
        }
    });
}
    private class AsyncCallWS extends AsyncTask<string,void,void> {
    @Override
    protected Void doInBackground(String... params) {
        //Call Web Method
        loginStatus =         WebService.invokeLoginWS(editTextUsername,editTextPassword,"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(CheckDNLoginActivity.this,HomeActivity.class);
        //Error status is false
        if(!errored){
            //Based on Boolean value returned from WebService
            if(loginStatus){
                //Navigate to Home Screen
                startActivity(intObj);
            }else{
                //Set Error message
                statusTV.setText("Login Failed, try again");
            }
        //Error status is true   
        }else{
            statusTV.setText("Error occured in invoking webservice");
        }
        //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) {
    }
}   













这是我的HomeActivity.java代码:









Here is my HomeActivity.java code:

import android.app.Activity;
import android.os.Bundle;

public class HomeActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.home);
}
}









WebService .java









WebService.java


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;


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://namastii.co.in/Service.asmx";
//SOAP Action URI again Namespace + Web method name
private static String SOAP_ACTION = "http://tempuri.org/GetUserDetails";

public static boolean invokeLoginWS(String username,String passWD, String webMethName) {
    boolean loginStatus = false;
    // Create request
    SoapObject request = new SoapObject(NAMESPACE, webMethName);
    // Property which holds input parameters
    PropertyInfo userName = new PropertyInfo();
    PropertyInfo password = new PropertyInfo();
    // Set Username
    userName.setName("username");
    // Set Value
    userName.setValue(username);
    // Set dataType
    userName.setType(String.class);
    // Add the property to request object
    request.addProperty(userName);
    //Set Password
    password.setName("password");
    //Set dataType
    password.setValue(passWD);
    //Set dataType
    password.setType(String.class);
    //Add the property to request object
    request.addProperty(password);
    // Create envelope
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
            SoapEnvelope.VER11);
    // 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'
        CheckDNLoginActivity.errored = true;
        e.printStackTrace();
    }
    //Return booleam to calling object
    return loginStatus;
}
}

推荐答案

你错过了这一行。

you are missing this line.
envelope.dotNet = true;





您的网络服务有问题..按下调用按钮时会显示此信息错误信息



此页面包含以下错误:

错误第37行第1行:文档末尾的额外内容

下面是第一个错误之前的页面呈现。


这篇关于我无法从Android应用程序登录到.asmx webservice的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆