Demo entry 6686801

zhuce

   

Submitted by Tonghow on Dec 25, 2017 at 07:17
Language: Java. Code size: 4.0 kB.

package com.example.kcb;
import java.io.IOException;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class Login extends Activity {
	String id;
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		Button exit_butt = (Button)findViewById(R.id.exit);
		SharedPreferences sharedPreferences = getSharedPreferences("id", Context.MODE_PRIVATE);
		id = sharedPreferences.getString("id", "").replace("\n", "").replace(" ", "");
	    EditText a = (EditText)findViewById(R.id.name);
	    EditText b = (EditText)findViewById(R.id.pwd);
	    Button   c = (Button)findViewById(R.id.login_butt);
	    Button   d = (Button)findViewById(R.id.zhuce_butt);
		if(id=="0"||id=="")
		{

			exit_butt.setVisibility(View.GONE);
		}
		else
		{
			a.setVisibility(View.GONE);
			b.setVisibility(View.GONE);
			c.setVisibility(View.GONE);
			d.setVisibility(View.GONE);
		}
		
	}
	public void login(View view) throws InterruptedException {
	    EditText a = (EditText)findViewById(R.id.name);
	    EditText b = (EditText)findViewById(R.id.pwd);
	    String name = (String)a.getText().toString();
		String pwd = (String)b.getText().toString();
		String target = "http://120.78.63.232/cgi-bin/login.py?name="+name+"&pwd="+pwd;
		String results ="";
		HttpClient http = new DefaultHttpClient();
		HttpGet httpRequest = new HttpGet(target);
		HttpResponse httpResponse;
		try {
			httpResponse = http.execute(httpRequest);
			if(httpResponse.getStatusLine().getStatusCode()==HttpStatus.SC_OK) {
				results=EntityUtils.toString(httpResponse.getEntity());
				if(results.replace("\n", "").split(" ")[1]=="0")
				{
					Toast.makeText(getApplicationContext(),results.replace("\n", "").split(" ")[0], 1).show();
				}
				else
				{
					SharedPreferences sharedPreferences = getSharedPreferences("id", Context.MODE_PRIVATE);
					Editor editor = sharedPreferences.edit();
					editor.putString("id", results.replace("\n", "").split(" ")[1]);
					editor.commit();
					Toast.makeText(getApplicationContext(),results.replace("\n", "").split(" ")[0], 1).show();
					Thread.sleep(1000);
					Intent intent =new Intent(Login.this,MainActivity.class);
					startActivity(intent);
					finish();
				}
			}
			else {
				results="服务器或数据库错误!";
			}
			}
		
		catch (ClientProtocolException e) {
			e.printStackTrace();
		}
		catch (IOException e) {
			e.printStackTrace();
		}
	}
	public void exit_on(View view) {
		try {
			SharedPreferences sharedPreferences = getSharedPreferences("id", Context.MODE_PRIVATE);
			Editor editor = sharedPreferences.edit();
			editor.putString("id", "0");
			editor.commit();
			Toast.makeText(getApplicationContext(),"退出成功", 1).show();
			Thread.sleep(1000);
			Intent intent =new Intent(Login.this,MainActivity.class);
			startActivity(intent);
			finish();
		}
		catch (Exception e) {
			Toast.makeText(getApplicationContext(),e.toString(), 1).show();
		}
	}
	public void shouye_on(View view) throws InterruptedException {
		Intent intent =new Intent(Login.this,MainActivity.class);
		startActivity(intent);
		finish();
	}
	public void zhuce_on(View view) throws InterruptedException {
		Intent intent_2 =new Intent(Login.this,Zhuce.class);
		startActivity(intent_2);
		finish();
	}
}

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).