登录验证失败

时间:2014-07-24 07:04:28

标签: android ksoap2

我使用kso​​ap网络服务进行用户登录..但是当我添加用户名和密码然后获取消息登录失败...我该如何解决它...用户名= adc和密码= adc已经在网络服务中大量使用

MainActivity.class

public class DotNetWSActivity extends Activity {
static boolean errored = false;
Button b;
TextView tv;
EditText et ,pw;
boolean loginStatus;
ProgressBar pg;
String UserName ,Password;
String LoginAuthentication;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    //Name Text control
    et = (EditText) findViewById(R.id.editText1);
    pw = (EditText) findViewById(R.id.editText2);
    //Display Text control
    tv = (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
    pg = (ProgressBar) findViewById(R.id.progressBar1);
    //Button Click Listener
    b.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            //Check if Name text control is not empty
            if (et.getText().length() != 0 && et.getText().toString() != "") {
                 if (pw.getText().length() != 0 && pw.getText().toString() != "") {
                //Get the text control value
                UserName = et.getText().toString();
                Password =pw.getText().toString();
                //Create instance for AsyncCallWS
                AsyncCallWS task = new AsyncCallWS();
                //Call execute 
                task.execute();
            //If text control is empty
            } else {
                tv.setText("Please enter name");
            }
            }else {
                  tv.setText("Please enter password");
            }
        }
    });
}

private class AsyncCallWS extends AsyncTask<String, Void, Void> {
    @Override
    protected Void doInBackground(String... params) {
        LoginAuthentication = WebService.invokeHelloWorldWS(UserName, Password,"LoginAuthentication");
        return null;
    }

    protected void onPostExecute(Void result) {
//      webservicePG.setVisibility(View.INVISIBLE);
        Intent intObj = new Intent(DotNetWSActivity.this,dfdf.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
                tv.setText("Login Failed, try again");
            }
        //Error status is true   
        }else{
            tv.setText("Error occured in invoking webservice");
        }
        //Re-initialize Error Status to False
        errored = false;
    }

    protected void onPreExecute() {
        pg.setVisibility(View.VISIBLE);
    }


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

}

}

这是我的Webservice类

public class WebService {

private static String NAMESPACE = "http://tempuri.org/";
private static String URL ="http://192.168.0.100/xandroid.asmx";
private static String SOAP_ACTION = "http://tempuri.org/";


public static String invokeHelloWorldWS(String UserName,String Password, String webMethName) {
    String resTxt = null;
     boolean loginStatus = false;
    // Create request
    SoapObject request = new SoapObject(NAMESPACE, webMethName);
    // Property which holds input parameters
    PropertyInfo celsiusPI = new PropertyInfo();
    // Set Name
    celsiusPI.setName("UserName");
    // Set Value
    celsiusPI.setValue(UserName);
    celsiusPI.setName("Password");
    // Set Value
    celsiusPI.setValue(Password);
    // Set dataType
    celsiusPI.setType(String.class);
    // Add the property to request object
    request.addProperty(celsiusPI);
    // 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 {
        // Invole web service
        androidHttpTransport.call(SOAP_ACTION+webMethName, envelope);
        // Get the response
        SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
        // Assign it to fahren static variable
          loginStatus = Boolean.parseBoolean(response.toString());
//      resTxt = response.toString();

    } catch (Exception e) {
        e.printStackTrace();
        resTxt = "Error occured";
    } 

    return resTxt;
}
}

logcat的:

07-24 12:13:47.121: W/System.err(1593): java.lang.NullPointerException
07-24 12:13:47.121: W/System.err(1593):     at   com.prgguru.android.WebService.invokeHelloWorldWS(WebService.java:50)
07-24 12:13:47.141: W/System.err(1593):     at com.prgguru.android.DotNetWSActivity$AsyncCallWS.doInBackground(DotNetWSActivity.java:63)
07-24 12:13:47.151: W/System.err(1593):     at com.prgguru.android.DotNetWSActivity$AsyncCallWS.doInBackground(DotNetWSActivity.java:1)
07-24 12:13:47.151: W/System.err(1593):     at android.os.AsyncTask$2.call(AsyncTask.java:264)
07-24 12:13:47.151: W/System.err(1593):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
07-24 12:13:47.151: W/System.err(1593):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
07-24 12:13:47.151: W/System.err(1593):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
07-24 12:13:47.151: W/System.err(1593):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
07-24 12:13:47.162: W/System.err(1593):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
07-24 12:13:47.162: W/System.err(1593):     at java.lang.Thread.run(Thread.java:856)

1 个答案:

答案 0 :(得分:0)

一些事情

在你的doInBackground中

LoginAuthentication = WebService.invokeHelloWorldWS(UserName, Password,"LoginAuthentication");

您正在从尚未实例化的类中调用方法。我建议在WebService中创建一个构造函数并获取这3个参数,然后在类中使用getter返回resTxt。然后你对班级的调用看起来像

WebService webservice = new WebService(UserName, Password,"LoginAuthentication");
String  LoginAuthentication = webservice.getResTxt(); 

或者,如果您坚持保留现在的格式,您仍然必须首先调用new WebService来实例化该类。

您现在没有收到编译错误的唯一原因是因为您的invokeHelloWorldWS方法是静态的......我可以说,没有理由认为它应该是静态的。

另外,你从doInBackground传递NULL到onPostExecute ......为什么? onPostExecute上的一点是从doInBackground获取值并对其执行某些操作。

相关问题