我开发了登录表单android应用程序。在这里,我必须实现忘记密码字段。如果我点击忘记密码textview
,那么它将转到下一个活动。
下一个活动将收集用户的电子邮件,并从调用soap webservices
的mysql数据库检查其验证。
我已完成上述部分。
现在我必须实现以下部分:
电子邮件有效意味着从mysql数据库获取密码并将这些密码发送到有效的电子邮件。
如何在我的java webservice代码中实现这一部分。请帮助我。给我一些解决方案。
这是我的java网络服务代码,用于检查电子邮件是有效还是无效:
public class Checkemail {
public String authentication(String Email){
String retrievedUserName = "";
String status = "";
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/xcart-432pro","root","");
PreparedStatement statement = con.prepareStatement("SELECT * FROM xcart_customers WHERE email = '"+Email+"'");
ResultSet result = statement.executeQuery();
while(result.next()){
retrievedUserName = result.getString("email");
}
if(retrievedUserName.equals(Email)){
status = "Valid Email";
}
else{
status = "Invalid Email!!!";
}
}
catch(Exception e){
e.printStackTrace();
}
return status;
}
}
这是我的android代码:
public class Login extends Activity {
private final String NAMESPACE = "http://xcart.com";
private final String URL = "http://10.0.0.75:8080/XcartLogin/services/Checkemail?wsdl";
private final String SOAP_ACTION = "http://xcart.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.login);
Button login = (Button) findViewById(R.id.btn_login);
login.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
loginAction();
}
});
}
private void loginAction(){
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
EditText Email = (EditText) findViewById(R.id.tf_userName);
String email = Email.getText().toString();
//Pass value for userName variable of the web service
PropertyInfo unameProp =new PropertyInfo();
unameProp.setName("Email");//Define the variable name in the web service method
unameProp.setValue(email);//set value for userName variable
unameProp.setType(String.class);//Define the type of the variable
request.addProperty(unameProp);//Pass properties to the variable
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();
String status = response.toString();
TextView result = (TextView) findViewById(R.id.tv_status);
result.setText(response.toString());
}
catch(Exception e){
}
}
}
如何实现获取密码,将其发送到java webservice代码中的电子邮件?
答案 0 :(得分:1)
使用此代码,对您有所帮助......
String email_id = etxt_user.getText().toString();
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("Email", email_id);
Pattern EMAIL_ADDRESS_PATTERN =Pattern.compile(
"[a-zA-Z0-9\\+\\.\\_\\%\\-\\+]{1,256}" +
"\\@" +
"[a-zA-Z0-9][a-zA-Z0-9\\-]{0,64}" +
"(" +
"\\." +
"[a-zA-Z0-9][a-zA-Z0-9\\-]{0,25}" +
")+");
Matcher matcher = EMAIL_ADDRESS_PATTERN.matcher(email_id);
if(matcher.matches()){
Log.v(TAG, "Your email id is valid ="+email_id);
// System.out.println("Your email id is valid ="+email);
}
else{
// System.out.println("enter valid email id");
Log.v(TAG, "enter valid email id" );
}
SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
soapEnvelope.dotNet = true;
soapEnvelope.setOutputSoapObject(request);
HttpTransportSE aht = new HttpTransportSE(URL);
try {
aht.setXmlVersionTag("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
aht.call(SOAP_ACTION, soapEnvelope);
SoapObject resultsRequestSOAP = (SoapObject) soapEnvelope.bodyIn;
Log.v("TAG", String.valueOf(resultsRequestSOAP));
} catch (Exception e) {
e.printStackTrace();
}
}
答案 1 :(得分:0)
使用此代码..对您非常有帮助.....
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.Authenticator;
import java.net.PasswordAuthentication;
import java.net.URL;
import java.net.URLConnection;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class Connect extends Activity {
static StringBuilder sb;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String strURL = "http://hostserver/";
final String username = "username";
final String password = "password";
Authenticator.setDefault(new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
PasswordAuthentication pa = new PasswordAuthentication
(username, password.toCharArray());
System.out.println(pa.getUserName() + ":" + new
String(pa.getPassword()));
return pa;
}
});
BufferedReader in = null;
StringBuffer sb = new StringBuffer();
try {
URL url = new URL(strURL);
URLConnection connection = url.openConnection();
in = new BufferedReader(new InputStreamReader(connection
.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
sb.append(line);
}
} catch (java.net.ProtocolException e) {
sb.append("User Or Password is wrong!");
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (in != null) {
in.close();
}
} catch (Exception e) {
System.out.println("Exception");
}
}
Log.d("DATA", sb.toString());
}
}