我正在尝试从链接到SQLserver的android studio登录到web服务 这是我的Webservice.java:
package comt.example.user.sql_login_test_2;
import android.content.Context;
import android.widget.Toast;
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 {
public static String SOAP_ACTION = "http://tempuri.org/Logins";
private static String NAMESPACE = "http://tempuri.org/";
private static String URL = "http://localhost:3284/Service1.asmx";
public static boolean invokeLoginWS(String userName,String passWord, 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("Login");
// Set Value
unamePI.setValue(userName);
// Set dataType
unamePI.setType(String.class);
// Add the property to request object
request.addProperty(unamePI);
//Set Password
passPI.setName("UserPWD");
//Set dataType
passPI.setValue(passWord);
//Set dataType
passPI.setType(String.class);
//Add the property to request object
request.addProperty(passPI);
// Create envelope
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
// Set output SOAP object
envelope.setOutputSoapObject(request);
// Create HTTP call object
try {
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
// Invoke web service
androidHttpTransport.call(SOAP_ACTION+webMethName, envelope);
// Get the response
Object response = (Object) envelope.getResponse();
// Assign it to boolean variable variable
loginStatus = Boolean.parseBoolean(response.toString());/* HOW boolean from response to string ? */
} catch (Exception e) {
//Assign Error Status true in static variable 'errored'
CheckLoginActivity.errored = true;
e.printStackTrace();
}
//Return booleam to calling object
return loginStatus;
}
}
那是我的Main.java:
package comt.example.user.sql_login_test_2;
import android.content.Intent;
import android.os.AsyncTask;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
public class Main extends Activity { /*input here function */
public static String OPERATION_NAME = "Logins"; /*hayde el Function Name*/
static boolean errored = false;
Button b;
TextView statusTV;
EditText userNameET , passWordET;
ProgressBar webservicePG;
String editTextUsername;
String editTextPassword;
boolean loginStatus;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
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 View.OnClickListener() {
public void onClick(View v) {
//Check if text controls are not empty
Toast.makeText(getApplicationContext(),
"Waiting Please...", Toast.LENGTH_LONG).show();
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(editTextUsername,editTextPassword);
}
//If Password text control is empty
else {
Toast.makeText(getApplicationContext(),
"please enter your Passwrod??", Toast.LENGTH_LONG).show();
}
//If Username text control is empty
} else {
Toast.makeText(getApplicationContext(),
"Please enter Username??", Toast.LENGTH_LONG).show();
}
}
});
}
private class AsyncCallWS extends AsyncTask<String, Void, Boolean> {
@Override
protected Boolean doInBackground(String... params) {
//Call Web Method
loginStatus = WebService.invokeLoginWS(editTextUsername,editTextPassword,OPERATION_NAME);
//authenticateUser
return loginStatus;
}
@Override
//Once WebService returns response
public void onPostExecute(Boolean result) {
//Make Progress Bar invisible
webservicePG.setVisibility(View.INVISIBLE);
Intent intObj = new Intent(Main.this,Home.class);
//Error status is false
if(!errored){
//Based on Boolean value returned from WebService
Toast.makeText(getApplicationContext(),"still waiting", Toast.LENGTH_LONG).show();
if(loginStatus){
//Navigate to Home Screen
startActivity(intObj);
Toast.makeText(getApplicationContext(),
"LOGIN SUCCESSFUL", Toast.LENGTH_LONG).show();
}else{
//Set Error message
Toast.makeText(getApplicationContext(),
"Login Failed, try again", Toast.LENGTH_LONG).show();
}
//Error status is true
}else{
Toast.makeText(getApplicationContext(),
"Error occured in invoking webservice", Toast.LENGTH_LONG).show();
}
//Re-initialize Error Status to False
}
@Override
//Make Progress Bar visible
protected void onPreExecute() {
webservicePG.setVisibility(View.VISIBLE);
}
@Override
protected void onProgressUpdate(Void... values) {
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
应用程序打开但总是给我LoginFailed请再试一次.... 问题可能是loginStatus布尔...它没有改变......当我将初始设置为true时,应用程序说LOGINSUCCESFULL但是当初始值为false时它不会...我找不到错误:/