嗨,在我的应用程序中,我试图从webservice检查数据库中的用户名和密码,如果它的真实将显示成功消息或失败的消息,但无法显示状态消息
public class AndroidLoginExampleActivity extends Activity {
private final String NAMESPACE = "http://ws.userlogin.com";
private final String URL = "http://localhost:8080/Androidlogin/services/Login?wsdl";
private final String SOAP_ACTION = "http://ws.userlogin.com/authentication";
private final String METHOD_NAME = "authentication";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button login = (Button) findViewById(R.id.btn_login);
login.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
loginAction();
}
});
}
@SuppressLint("NewApi") private void loginAction(){
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
EditText userName = (EditText) findViewById(R.id.tf_userName);
String user_Name = userName.getText().toString();
EditText userPassword = (EditText) findViewById(R.id.tf_password);
String user_Password = userPassword.getText().toString();
//Pass value for userName variable of the web service
PropertyInfo unameProp =new PropertyInfo();
unameProp.setName("userName");//Define the variable name in the web service method
unameProp.setValue(user_Name);//set value for userName variable
unameProp.setType(String.class);//Define the type of the variable
request.addProperty(unameProp);//Pass properties to the variable
//Pass value for Password variable of the web service
PropertyInfo passwordProp =new PropertyInfo();
passwordProp.setName("password");
passwordProp.setValue(user_Password);
passwordProp.setType(String.class);
request.addProperty(passwordProp);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
try{
androidHttpTransport.call(SOAP_ACTION, envelope);
SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
TextView result = (TextView) findViewById(R.id.tv_status);
result.setText(response.toString());
Log.d("resp:",response.toString() );
}
catch(Exception e){
}
}
下面是我的网络服务电话
public class Login {
public String authentication(String userName,String password){
String retrievedUserName = "";
String retrievedPassword = "";
String status = "";
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb","root","root");
PreparedStatement statement = con.prepareStatement("SELECT * FROM user WHERE username = '"+userName+"'");
ResultSet result = statement.executeQuery();
while(result.next()){
retrievedUserName = result.getString("username");
retrievedPassword = result.getString("password");
}
if(retrievedUserName.equals(userName)&&retrievedPassword.equals(password)){
status = "Success!";
}
else{
status = "Login fail!!!";
}
}
catch(Exception e){
e.printStackTrace();
}
return status;
}
}
不确定我做错了。感谢任何帮助。
答案 0 :(得分:1)
您应该在线程上进行网络实际操作。您可以使用thread
或AsyncTask
。
将loginAction()
移到thread
内或doInbackground
AsyncTask
内。
请记住不要从后台线程更新ui。
new TheTask().execute();
的AsyncTask
public class TheTask extends AsyncTask <Void,Void,Void>
{
@Override
protected void onPreExecute() {
super.onPreExecute();
// display a dialog
}
@Override
protected Void doInBackground(Void... params) {
// your login authentcation
// remove updation of textview.
// do not update ui here
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// dismiss the dialog
// update textview
}
}
AsyncTask docs
http://developer.android.com/reference/android/os/AsyncTask.html
编辑:
public class MainActivity extends Activity {
private final String NAMESPACE = "http://ws.userlogin.com";
private final String URL = "http://localhost:8080/Androidlogin/services/Login?wsdl";
private final String SOAP_ACTION = "http://ws.userlogin.com/authentication";
private final String METHOD_NAME = "authentication";
/** Called when the activity is first created. */
EditText ed1,ed2;
TextView tv;
String user_Name,user_Password;
SoapPrimitive response ;
ProgressDialog pd;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ed1 = (EditText) findViewById(R.id.editText1);
ed2 = (EditText) findViewById(R.id.editText2);
tv = (TextView) findViewById(R.id.textView1);
user_Name = ed1.getText().toString();
user_Password = ed2.getText().toString();
pd = new ProgressDialog(this);
Button login = (Button) findViewById(R.id.button1);
login.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
new TheTask().execute();
}
});
}
class TheTask extends AsyncTask<Void,Void,SoapPrimitive>
{
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
pd.show();
}
@Override
protected SoapPrimitive doInBackground(Void... params) {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
PropertyInfo unameProp =new PropertyInfo();
unameProp.setName("userName");//Define the variable name in the web service method
unameProp.setValue(user_Name);//set value for userName variable
unameProp.setType(String.class);//Define the type of the variable
request.addProperty(unameProp);//Pass properties to the variable
PropertyInfo passwordProp =new PropertyInfo();
passwordProp.setName("password");
passwordProp.setValue(user_Password);
passwordProp.setType(String.class);
request.addProperty(passwordProp);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
try{
androidHttpTransport.call(SOAP_ACTION, envelope);
response = (SoapPrimitive) envelope.bodyIn;
Log.i("Response",""+response);
// response = (SoapPrimitive)envelope.getResponse();
}
catch(Exception e){
}
return response;
}
@Override
protected void onPostExecute(SoapPrimitive result) {
super.onPostExecute(result);
pd.dismiss();
if(result!=null)
tv.setText(result.toString());
}
}
}
答案 1 :(得分:0)
public static String ValidateSalesOfficerLogin(Context c, String userName,
String passWord) throws IOException, XmlPullParserException {
String METHOD_NAME = "ValidateSalesOfficerLogin";
String SOAP_ACTION = "http://tempuri.org/authentication/";
SOAP_ACTION = SOAP_ACTION + METHOD_NAME;
SoapObject request = new SoapObject(CommonVariable.NAMESPACE,
METHOD_NAME);
// Use this to add parameters
request.addProperty("Username", userName);
request.addProperty("Password", passWord);
// Declare the version of the SOAP request
return WebCalls.call(c, request, CommonVariable.NAMESPACE, METHOD_NAME,
SOAP_ACTION);
}
//////////////////////////////////////////////////////////////////////////////
public static String call(Context c,SoapObject request ,String NAMESPACE,String METHOD_NAME,String SOAP_ACTION) throws IOException, XmlPullParserException{
Log.i(WebCalls,"URL: "+ CommonVariable.URL);
Log.i(WebCalls,"Method Name: "+ METHOD_NAME);
Log.i(WebCalls,"Parameters: "+request.toString());
String SoapResult = null;
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
envelope.dotNet = true;
HttpTransportSE androidHttpTransport = new HttpTransportSE(CommonVariable.URL);
// this is the actual part that will call the webservice
androidHttpTransport.call(SOAP_ACTION, envelope);
// Get the SoapResult from the envelope body.
if (envelope.bodyIn instanceof SoapFault) {
SoapResult = ((SoapFault) envelope.bodyIn).faultstring;
} else {
SoapObject resultsRequestSOAP = (SoapObject) envelope.bodyIn;
SoapResult = resultsRequestSOAP.getProperty(0).toString();
}
Log.i(WebCalls,"Response: "+ SoapResult);
return SoapResult;
}
call above method....
public static void Setusernamepassword(Context context, String user ,string pass)
throws JSONException, IOException, XmlPullParserException {
String Response = SoaplCalls.ValidateSalesOfficerLogin(context, user,pass);
Log.i("SetTokenId", Response);
}
/////////////////////////////////////////////////////////////////////////////////////
new Thread(new Runnable() {
@Override
public void run() {
try {
Setusernamepassword(viewCompetitor,user,pass);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
mHandler.post(new Runnable() {
@Override
public void run() {
}
});
}
}
}).start();
}