今天把登錄注冊寫了一下,百度解決了兩個問題:
1.模擬器輸入中文,就長按然後選擇谷歌輸入法就好
2.得到自定義的Dialog上的組件信息,主要要通過view獲得,不能直接就用findviewbyid
之前的saveUser 和 findUserByName也簡單的封裝成了函數
界面如下,一切從簡單處理
後台打印處理結果
關於這個對話框,處理邏輯比較麻煩。使用SharedPreferences保存一個變量來控制對話框的出現。第一次登陸的時候要出現,登錄成功後,下次一次就不用彈出了,如果出現錯誤,這個對話框還要出現。
代碼
package seu.xin.webservice;
import java.io.IOException;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;
import seu.xin.entity.UserInfo;
import com.google.gson.Gson;
import Android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class WebserviceActivity extends Activity {
/** Called when the activity is first created. */
private static final String NAMESPACE = "http://10.6.11.20/";
private static String URL = "http://10.6.11.20:8080/webservicedemo/services/webservice";
private static final String SAVE_METHOD_NAME = "save";
private static final String FIND_METHOD_NAME = "findUserByName";
private static String SOAP_ACTION = "http://10.6.11.20:8080/webservicedemo/services/";
AlertDialog loginRegDialog ;
SharedPreferences sp ;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
sp = this.getSharedPreferences("data",MODE_WORLD_READABLE);
boolean isFirst = sp.getBoolean("first", true);
if(isFirst){
showloginRegDialog();
return;
}else{
System.out.println("isFirst-------->"+isFirst);
}
}
public void showloginRegDialog() {
// TODO Auto-generated method stub
LayoutInflater factory = LayoutInflater.from(WebserviceActivity.this);
final View loginRegView = factory.inflate(R.layout.login, null);
AlertDialog.Builder builder = new AlertDialog.Builder(WebserviceActivity.this);
builder.setIcon(R.drawable.dialog_login)
.setTitle("Welcome to our app")
.setView(loginRegView)
.setPositiveButton("登陸", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
EditText etName = (EditText)loginRegView.findViewById(R.id.login_name);
EditText etPassword= (EditText) loginRegView.findViewById(R.id.login_password);
String name = etName.getText().toString();
String password = etPassword.getText().toString();
System.out.println("login name"+name+"password"+password);
UserInfo loginUser = new UserInfo();
loginUser = findUserByName(name);
if(name.equals("") || password.equals("")){
Toast.makeText(WebserviceActivity.this, "請輸入用戶名和密碼", Toast.LENGTH_LONG).show();
System.out.println("請輸入用戶名和密碼");
showloginRegDialog();
}else if(loginUser == null){
Toast.makeText(WebserviceActivity.this, "不存在此用戶", Toast.LENGTH_LONG).show();
System.out.println("不存在此用戶");
showloginRegDialog();
}else if(password.equals(loginUser.getPassword())){
Toast.makeText(WebserviceActivity.this, "登陸成功", Toast.LENGTH_LONG).show();
System.out.println("登陸成功");
Editor editor = sp.edit();
editor.putString("name", name);
editor.putString("password", password);
editor.putBoolean("first", false);
editor.commit();
}else{
Toast.makeText(WebserviceActivity.this, "密碼錯誤", Toast.LENGTH_LONG).show();
System.out.println("密碼錯誤");
showloginRegDialog();
}
}
})
.setNeutralButton("注冊",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
EditText etName = (EditText)loginRegView.findViewById(R.id.login_name);
EditText etPassword= (EditText) loginRegView.findViewById(R.id.login_password);
String name = etName.getText().toString();
String password = etPassword.getText().toString();
System.out.println("register name"+name+"password"+password);
UserInfo existUser = new UserInfo();
existUser = findUserByName(name);
UserInfo registerUser = new UserInfo();
registerUser.setName(name);
registerUser.setPassword(password);
if(name.equals("") || password.equals("")){
Toast.makeText(WebserviceActivity.this, "請輸入用戶名和密碼", Toast.LENGTH_LONG).show();
System.out.println("請輸入用戶名和密碼");
showloginRegDialog();
}else if(existUser != null){
Toast.makeText(WebserviceActivity.this, "該用戶已注冊,請換個名字注冊", Toast.LENGTH_LONG).show();
System.out.println("該用戶已注冊,請換個名字注冊");
showloginRegDialog();
}else if((registerUser!=null) &&(existUser==null)) {
saveUser(name,password);
Toast.makeText(WebserviceActivity.this, "注冊成功,請登錄", Toast.LENGTH_LONG).show();
System.out.println("注冊成功,請登錄");
showloginRegDialog();
}
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
loginRegDialog = builder.create();
loginRegDialog.show();
}
public UserInfo findUserByName(String name) {
// TODO Auto-generated method stub
SoapObject soapObject = new SoapObject(NAMESPACE, FIND_METHOD_NAME);
soapObject.addProperty("name", name);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);// 版本
envelope.bodyOut = soapObject ;
envelope.dotNet = false ;
envelope.setOutputSoapObject(soapObject) ;
HttpTransportSE trans = new HttpTransportSE(URL) ;
trans.debug = true ; // 使用調試功能
try {
trans.call(SOAP_ACTION, envelope) ;
} catch (IOException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
}
SoapObject result = (SoapObject) envelope.bodyIn;
Gson gson = new Gson();
String json = result.getProperty(0).toString();
UserInfo userInfo = new UserInfo();
userInfo = gson.fromJson(json, UserInfo.class);
System.out.println("Web Service返回的數據是:"+result.getProperty(0));
System.out.println("Web Service返回的userInfo"+userInfo);
return userInfo;
}
public void saveUser(String name,String password) {
// TODO Auto-generated method stub
SoapObject soapObject = new SoapObject(NAMESPACE, SAVE_METHOD_NAME);
soapObject.addProperty("name", name);
soapObject.addProperty("password", password);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);// 版本
envelope.bodyOut = soapObject ;
envelope.dotNet = false ;
envelope.setOutputSoapObject(soapObject) ;
HttpTransportSE trans = new HttpTransportSE(URL) ;
trans.debug = true ; // 使用調試功能
try {
trans.call(SOAP_ACTION, envelope) ;
} catch (IOException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
}
}
}
相關閱讀:Android教程:在百度地圖上畫出軌跡 http://www.linuxidc.com/Linux/2013-04/82748.htm
更多Android相關信息見Android 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=11