android:id="@+id/ll1"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical" >

android:layout_width="match_parent"

android:layout_height="wrap_content"/>

android:layout_width="match_parent"

android:layout_height="wrap_content"/>

android:layout_width="match_parent"

android:layout_height="wrap_content"/>

android:layout_width="match_parent"

android:layout_height="wrap_content"/>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="登录"/>

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_weight="1">

android:layout_width="match_parent"

android:layout_height="match_parent">

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_weight="1"/>

public class MainActivity extends Activity{

private EditText username;

private EditText password;

private Button button;

private Handler handler;

private String result="";

private TextView resultTV;

public  static  final String Intent_key="token";

public  static  final String Intent_url="URL";

private SharedPreferences preferences;

private SharedPreferences.Editor editor;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

requestWindowFeature(Window.FEATURE_NO_TITLE);

setContentView(R.layout.activity_main);

//获取preferences和editor对象

preferences = getSharedPreferences("loginState",MODE_PRIVATE);

editor = preferences.edit();

String token =preferences.getString("token","fail");

Intent intent = new Intent(this,SuccessActivity.class);

Bundle bundle = new Bundle();

if(token.equals("user3_login_success")){

bundle.putString(Intent_key, token);

bundle.putString(Intent_url, urlInfo + token);

intent.putExtra("bundle", bundle);

startActivityForResult(intent,0);

}else if(token.equals("user4_login_success")){

bundle.putString(Intent_key, token);

bundle.putString(Intent_url, urlInfo + token);

intent.putExtra("bundle", bundle);

startActivityForResult(intent,0);

}

username=(EditText)findViewById(R.id.username);

password=(EditText)findViewById(R.id.password);

resultTV=(TextView)findViewById(R.id.result);

button=(Button)findViewById(R.id.button);

button.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View arg0) {

if("".equals(username.getText().toString())){

Toast.makeText(MainActivity.this, "请登录",

Toast.LENGTH_SHORT).show();

return;

}

new Thread(new Runnable() {

@Override

public void run() {

login();

Messagem=handler.obtainMessage();

handler.sendMessage(m);

}

}).start();

}

});

handler=new Handler(){

@Override

public void handleMessage(Message msg) {

if(result!=null){

resultTV.setText(result);

username.setText("");

password.setText("");

}

super.handleMessage(msg);

}

};

}

//当从secondActivity中返回时调用此函数,清空token

@Override

protected void onActivityResult(int requestCode, int resultCode, Intentdata) {

super.onActivityResult(requestCode, resultCode, data);

if(requestCode==0 && resultCode==RESULT_OK){

Bundle bundle = data.getExtras();

String text =null;

if(bundle!=null)

text=bundle.getString("return");

Log.d("text",text);

editor.remove("token");

editor.commit();

}

}

public void login() {

URL url;

try {

url=new URL(target);

HttpURLConnection urlConn=(HttpURLConnection)url.openConnection();

urlConn.setRequestMethod("POST");

urlConn.setDoInput(true);

urlConn.setDoOutput(true);

urlConn.setUseCaches(false);

urlConn.setInstanceFollowRedirects(true);

urlConn.setRequestProperty("Content-Type",

"application/x-www-form-urlencoded");

DataOutputStream out=new DataOutputStream(urlConn.getOutputStream());

String param="username="+URLEncoder.encode(username.getText().toString(),"utf-8")

+"&password="+URLEncoder.encode(password.getText().toString(),"utf-8");

System.out.println(username);

out.writeBytes(param);

out.flush();

out.close();

if(urlConn.getResponseCode()==HttpURLConnection.HTTP_OK){

InputStreamReader in=newInputStreamReader(urlConn.getInputStream());

BufferedReader buffer=newBufferedReader(in);

String inputLine=null;

String token = "";

result = "";

while((inputLine=buffer.readLine())!=null){

result+=inputLine;

}

in.close();

Intent intent = newIntent(this,SuccessActivity.class);

Bundle bundle = new Bundle();

if(result.indexOf("user3_login_success")!=-1){

token ="user3_login_success";

}elseif(result.indexOf("user4_login_success")!=-1){

token ="user4_login_success";

}else{

return;

}

editor.putString("token",token);

bundle.putString(Intent_key,token);

bundle.putString(Intent_url,urlInfo + token);

editor.commit();

intent.putExtra("bundle", bundle);

startActivityForResult(intent,0);

}

urlConn.disconnect();

} catch (MalformedURLException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

}

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐