使用者可以从客户端登陆服务器,在改密码的功能上,客户端的用户需要向服务器传username和newpassword,但是客户不必再重新输入username了,因为已经登陆了,怎样才能像java中当用户登陆的时候把username放入一个session中,然后可以用于其他功能?我的changepassword activity:public class ChangePasswordAction extends Activity {
private EditText newpassword1;
private EditText newpassword2;
private Button change;
private Button reset; private String new1;
private String new2; private String serverIp="192.168.1.100";
private String serverPort="8080";
private String httpStr;
private String postStr;
private String questStr;
//String requestUrl = "http://192.168.1.100:8080/serv/Login";
private Intent intent; /** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.changepassword);
newpassword1 = (EditText) findViewById(R.id.NewPassword1);
newpassword2 = (EditText) findViewById(R.id.NewPassword2);
change = (Button) findViewById(R.id.change);
reset = (Button) findViewById(R.id.reset); intent = new Intent(ChangePasswordAction.this, MainAction.class);
change.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (changeIsSuccsee()) {
change();
}

}

});


reset.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {
newpassword1 = (EditText) findViewById(R.id.NewPassword1);
newpassword2 = (EditText) findViewById(R.id.NewPassword2);
newpassword1.setText("");
newpassword2.setText("");
}
});
}

/*
 * 判断用户更改密码输入是否规范 录入信息验证 验证是否通过
 */ private boolean changeIsSuccsee(){     EditText pwd1=(EditText)findViewById(R.id.NewPassword1);
     EditText pwd2=(EditText)findViewById(R.id.NewPassword2);     //获取用户输入的信息     String password1=pwd1.getText().toString();
     String password2=pwd2.getText().toString();        if("".equals(password1)){
     //密码不能为空
     Toast.makeText(ChangePasswordAction.this, "Password must be filled!", Toast.LENGTH_SHORT).show();
     return false;
     }else if(!password1.equals(password2)){
     Toast.makeText(ChangePasswordAction.this, "Passwords must be same!", Toast.LENGTH_SHORT).show();
     return false;
     }
     return true;
    }

/**
 * 更改
 */
private void change() {
newpassword1 = (EditText) findViewById(R.id.NewPassword1);
newpassword2 = (EditText) findViewById(R.id.NewPassword2); new1 = newpassword1.getText().toString();
new2 = newpassword2.getText().toString();
httpStr = "http://";
postStr = httpStr + serverIp + ":" + serverPort
+ "/serv/changepassword";
questStr = "{CHANGEPASSWORD:{username:'"+"Kelly"+"',change1:'"
+ new1 + "',change2:'" + new2 + "'}}";
System.out.println("====questStr====" + questStr);
System.out.println("====postStr====" + postStr);
try {
HttpParams httpParams = new BasicHttpParams();
// 设置连接超时
int timeoutConnection = 3000;
HttpConnectionParams.setConnectionTimeout(httpParams,
timeoutConnection);
DefaultHttpClient httpClient = new DefaultHttpClient(httpParams);
HttpPost httpPost = new HttpPost(postStr);
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("change1", new1));
nvps.add(new BasicNameValuePair("change2", new1)); httpPost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
HttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
InputStream is=entity.getContent();
String isChange = ConvertStreamToString(is);

System.out.println(isChange);
if("success".equals(isChange)){
//表示用户注册成功
AlertDialog.Builder builder=new Builder(ChangePasswordAction.this);
builder.setMessage("Congratulations! You have new password now!");
builder.setTitle("Warning");
 builder.setPositiveButton("OK", new OnClickListener() {

public void onClick(DialogInterface dialog, int which) {
Intent i=new Intent(ChangePasswordAction.this,MainAction.class);
dialog.dismiss();
startActivity(i);
}
}).show();
}else {
//用户已经被注册
AlertDialog.Builder builder=new Builder(ChangePasswordAction.this);
builder.setMessage("Sorry!");
builder.setTitle("Warning");
builder.setPositiveButton("OK", new OnClickListener() {

public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}).show();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();

}
}
// 读取字符流
public String ConvertStreamToString(InputStream is) {
StringBuffer sb = new StringBuffer();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String returnStr = "";
try {
while ((returnStr = br.readLine()) != null) {
sb.append(returnStr);
} } catch (IOException e) {
e.printStackTrace();
}
final String result = sb.toString(); System.out.println(result);
return result;
}
}

解决方案 »

  1.   

    登录成功后,把用户名和其他基本信息存放到SharedPreference中
      

  2.   

    SharedPreference 也行 或者自己命名一个xml
    sqllite也可以
      

  3.   

    应该放在App全局类里,程序退出后自动销毁,下一次运行程序还是需要登录。
      

  4.   

    如果我的Server端在这个功能上需要接收的parameter包含这个用户的username,怎么用App全局类啊?
      

  5.   

    你都知道java中有session这个作用域了,App也就相当于session.
    session中可以直接保存一个User类对像,也可以保存String uid 和 String upass,
    App也是相同的:
    User user=登录成功;
    app.writToPreferences(key, user); //写入保存
    app.getContextToPreferences(key);  //读取获得至于要怎么发送给server端,User user=app.getContextToPreferences(key);
    user.username就是你想要知道的了。