package com.dream.apm;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
public class MyActivity extends Activity {
//请求地址
private static String url="http://10.0.2.2:8080/think/index.php/Home/Android";
public Button start;
public EditText username,password;
public URL http_url;
public String data;
public Handler handler;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//设置全屏
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
//去除应用程序标题
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
//设置竖屏
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setContentView(R.layout.main);
start=(Button)findViewById(R.id.start_one);
username=(EditText)findViewById(R.id.username);
password=(EditText)findViewById(R.id.password);
//消息处理器
handler=new Handler(Looper.getMainLooper())
{
@Override
public void handleMessage(Message msg)
{
super.handleMessage(msg);
switch(msg.what)
{
//登入成功
case 1:
Toast.makeText(MyActivity.this, msg.getData().getString("msg"),
Toast.LENGTH_SHORT).show();
break;
//登入失败
case 2:
Toast.makeText(MyActivity.this, msg.getData().getString("msg"),
Toast.LENGTH_SHORT).show();
break;
}
}
};
start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//是否输入账号密码
if(username.getText().toString().length()>0&&password.getText().toString().length()>0){
//子线程可以获取UI的值,不能更改
new Thread(new Runnable() {
@Override
public void run() {
try {
http_url=new URL(url);
if(http_url!=null)
{
//打开一个HttpURLConnection连接
HttpURLConnection conn = (HttpURLConnection) http_url.openConnection();
conn.setConnectTimeout(5* 1000);//设置连接超时
conn.setRequestMethod("POST");//以get方式发起请求
//允许输入输出流
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);//使用Post方式不能使用缓存
//获取账号密码
String params = "username=" + username.getText().toString()
+ "&password=" + password.getText().toString();
//设置请求体的类型是文本类型
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
//设置请求体的长度--字节长度
conn.setRequestProperty("Content-Length",String.valueOf(params.getBytes().length) );
//发送post参数
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(conn.getOutputStream()));
bw.write(params);
bw.close();
//接收服务器响应
if (conn.getResponseCode() == 200) {
InputStream is = conn.getInputStream();//得到网络返回的输入流
BufferedReader buf=new BufferedReader(new InputStreamReader(is));//转化为字符缓冲流
data=buf.readLine();
buf.close();is.close();
//判断登入结果
analyse(data);
}
}
} catch( Exception e) {
e.printStackTrace();
}
}
}).start();
}
else
{
Toast.makeText(MyActivity.this, "请完整输入账号密码",
Toast.LENGTH_SHORT).show();
}
}
});
}
public void analyse (String data)
{
System.out.println(data);
try {
JSONObject json_data=new JSONObject(data);
Boolean state=json_data.getBoolean("success");
String msg=json_data.getString("msg");
//登入成功
if(state)
{
//发送消息
Message message= new Message();
message.what=1;
Bundle temp = new Bundle();
temp.putString("msg",msg);
message.setData(temp);
handler.sendMessage(message);
}
//登入失败
else
{
Message message= new Message();
message.what=2;
Bundle temp = new Bundle();
temp.putString("msg",msg);
message.setData(temp);
handler.sendMessage(message);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
package com.dream.apm;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
public class MyActivity extends Activity {
public Button start;
public EditText username,password;
public URL http_url;
public String data;
public Handler handler;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//设置全屏
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
//去除应用程序标题
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
//设置竖屏
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setContentView(R.layout.main);
start=(Button)findViewById(R.id.start_one);
username=(EditText)findViewById(R.id.username);
password=(EditText)findViewById(R.id.password);
//消息处理器
handler=new Handler(Looper.getMainLooper())
{
@Override
public void handleMessage(Message msg)
{
super.handleMessage(msg);
switch(msg.what)
{
//登入成功
case 1:
Toast.makeText(MyActivity.this, msg.getData().getString("msg"),
Toast.LENGTH_SHORT).show();
break;
//登入失败
case 2:
Toast.makeText(MyActivity.this, msg.getData().getString("msg"),
Toast.LENGTH_SHORT).show();
break;
}
}
};
start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//是否输入账号密码
if(username.getText().toString().length()>0&&password.getText().toString().length()>0){
//子线程可以获取UI的值,不能更改
new Thread(new Runnable() {
@Override
public void run() {
try {
//请求地址--
String url="http://10.0.2.2:8080/think/index.php/Home/Android?"+ "username=" + URLEncoder.encode(username.getText().toString(), "UTF-8")
+ "&password=" + URLEncoder.encode(password.getText().toString(), "UTF-8");
http_url=new URL(url);
if(http_url!=null)
{
//打开一个HttpURLConnection连接
HttpURLConnection conn = (HttpURLConnection) http_url.openConnection();
conn.setConnectTimeout(5* 1000);//设置连接超时
conn.setRequestMethod("GET");//以get方式发起请求
//允许输入流
conn.setDoInput(true);
//接收服务器响应
if (conn.getResponseCode() == 200) {
InputStream is = conn.getInputStream();//得到网络返回的输入流
BufferedReader buf=new BufferedReader(new InputStreamReader(is));//转化为字符缓冲流
data=buf.readLine();
buf.close();is.close();
//判断登入结果
analyse(data);
}
}
} catch( Exception e) {
e.printStackTrace();
}
}
}).start();
}
else
{
Toast.makeText(MyActivity.this, "请完整输入账号密码",
Toast.LENGTH_SHORT).show();
}
}
});
}
public void analyse (String data)
{
System.out.println(data);
try {
JSONObject json_data=new JSONObject(data);
Boolean state=json_data.getBoolean("success");
String msg=json_data.getString("msg");
//登入成功
if(state)
{
//发送消息
Message message= new Message();
message.what=1;
Bundle temp = new Bundle();
temp.putString("msg",msg);
message.setData(temp);
handler.sendMessage(message);
}
//登入失败
else
{
Message message= new Message();
message.what=2;
Bundle temp = new Bundle();
temp.putString("msg",msg);
message.setData(temp);
handler.sendMessage(message);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}