1 package com.ch.day4_httpurlconn; 2 3 import java.io.BufferedReader; 4 import java.io.DataOutputStream; 5 import java.io.IOException; 6 import java.io.InputStreamReader; 7 import java.net.HttpURLConnection; 8 import java.net.MalformedURLException; 9 import java.net.URL; 10 import java.util.ArrayList; 11 import java.util.Iterator; 12 import java.util.Vector; 13 14 import android.os.Bundle; 15 import android.os.Handler; 16 import android.app.Activity; 17 import android.view.Menu; 18 import android.view.View; 19 import android.view.View.OnClickListener; 20 import android.widget.Button; 21 import android.widget.TextView; 22 import android.widget.Toast; 23 24 public class MainActivity extends Activity { 25 private TextView jsonData; 26 private Button login; 27 //服务器地址,路径 28 public static final String JSON_URL = "http://169.254.109.73:8080/tqyb/newsList.json"; 29 public static final String LOGIN_URL = "http://169.254.109.73:8080/serverof1407a/loginServlet"; 30 31 //定义一个handler的匿名内部类的对象 32 Handler handler = new Handler(){ 33 public void handleMessage(android.os.Message msg) { 34 if(msg.what == 1){ 35 jsonData.setText((String)msg.obj); 36 }else if(msg.what == 2){ 37 login.setText((String)msg.obj); 38 } 39 }; 40 }; 41 42 @Override 43 protected void onCreate(Bundle savedInstanceState) { 44 super.onCreate(savedInstanceState); 45 setContentView(R.layout.activity_main); 46 47 init(); 48 } 49 50 public void init(){ 51 jsonData = (TextView) findViewById(R.id.jsonData); 52 login = (Button) findViewById(R.id.login); 53 //点击login到服务器登陆验证 54 login.setOnClickListener(new OnClickListener() { 55 @Override 56 public void onClick(View v) { 57 //在次线程中,请求web服务器 58 new Thread(){ 59 public void run() { 60 //调用httpurlconnection的get请求,到服务器请求json数据 61 // String rs = loginByServerByGet("admin", "123456"); 62 63 String rs = loginByServerByPost("admin1", "123456"); 64 //在此次线程中,通过handler把数据传递给主线程,显示在主线程的组件中 65 handler.sendMessage(handler.obtainMessage(2, rs)); 66 if("success".equals(rs)){ 67 //跳转到功能页面, 68 }else{ 69 //提示登录失败,请修改登录信息等等 70 } 71 72 }; 73 }.start(); 74 } 75 }); 76 77 jsonData.setOnClickListener(new OnClickListener() { 78 @Override 79 public void onClick(View v) { 80 //在次线程中,请求web服务器 81 new Thread(){ 82 public void run() { 83 //调用httpurlconnection的get请求,到服务器请求json数据 84 String rs = getJsonFromWeb();//此方法请求web服务器 85 //在此次线程中,通过handler把数据传递给主线程,显示在主线程的组件中 86 handler.sendMessage(handler.obtainMessage(1, rs)); 87 }; 88 }.start(); 89 } 90 }); 91 } 92 //到服务器验证登陆信息,,通过post请求 93 public String loginByServerByPost(String name,String pass){ 94 String rs = ""; 95 URL url = null; 96 HttpURLConnection urlConn = null; 97 BufferedReader br = null; 98 DataOutputStream output = null; 99 // StringBuffer sb = new StringBuffer();//定义缓存字符串,拼装字符串 100 try { 101 102 url = new URL(LOGIN_URL);//创建和服务器的连接对象URL 103 urlConn = (HttpURLConnection) url.openConnection();//打开连接 104 urlConn.setConnectTimeout(5*1000);//设置连接超时容忍时间 105 urlConn.setReadTimeout(5*1000);//设置读取时间 106 107 //设置post请求 108 urlConn.setRequestMethod("POST"); 109 urlConn.setDoInput(true); 110 urlConn.setDoOutput(true); 111 //通过post请求,向服务器传值 112 output = new DataOutputStream(urlConn.getOutputStream());//创建服务器的输出流 113 String data = "username="+name+"&userpass="+pass; 114 output.write(data.getBytes()); 115 output.flush(); 116 output.close();//传值成功,完毕 117 118 if(urlConn.getResponseCode() == 200){//如果响应码为200表示响应成功,并且同时成功的相应了数据 119 //获得服务器相应的数据,字节输入流(数据流),转换为缓存字符流便于读取 120 br = new BufferedReader(new InputStreamReader(urlConn.getInputStream(),"utf-8")); 121 rs = br.readLine();//获得服务器返回的简单数据 122 System.out.println(rs); 123 } 124 125 } catch (IOException e) { 126 // TODO Auto-generated catch block 127 e.printStackTrace(); 128 } 129 130 return rs;//转换为字符串,返回 131 } 132 //到服务器验证登陆信息,,通过get请求 133 public String loginByServerByGet(String name,String pass){ 134 String rs = ""; 135 URL url = null; 136 HttpURLConnection urlConn = null; 137 BufferedReader br = null; 138 StringBuffer sb = new StringBuffer(LOGIN_URL);//定义缓存字符串,拼装字符串 139 try { 140 sb.append("?username="+name); 141 sb.append("&userpass="+pass); 142 System.out.println(sb.toString()); 143 url = new URL(sb.toString());//创建和服务器的连接对象URL 144 urlConn = (HttpURLConnection) url.openConnection();//打开连接 145 urlConn.setConnectTimeout(5*1000);//设置连接超时容忍时间 146 urlConn.setReadTimeout(5*1000);//设置读取时间 147 if(urlConn.getResponseCode() == 200){//如果响应码为200表示响应成功,并且同时成功的相应了数据 148 //获得服务器相应的数据,字节输入流(数据流),转换为缓存字符流便于读取 149 br = new BufferedReader(new InputStreamReader(urlConn.getInputStream(),"utf-8")); 150 rs = br.readLine();//获得服务器返回的简单数据 151 System.out.println(rs); 152 } 153 154 } catch (IOException e) { 155 // TODO Auto-generated catch block 156 e.printStackTrace(); 157 } 158 159 return rs;//转换为字符串,返回 160 } 161 162 163 public String getJsonFromWeb(){ 164 URL url = null; 165 HttpURLConnection urlConn = null; 166 BufferedReader br = null; 167 StringBuffer sb = new StringBuffer();//定义缓存字符串,拼装字符串 168 try { 169 url = new URL(JSON_URL);//创建和服务器的连接对象URL 170 urlConn = (HttpURLConnection) url.openConnection();//打开连接 171 urlConn.setConnectTimeout(5*1000);//设置连接超时容忍时间 172 urlConn.setReadTimeout(5*1000);//设置读取时间 173 if(urlConn.getResponseCode() == 200){//如果相应码为200表示响应成功,并且同时成功的相应了数据 174 //获得服务器相应的数据,字节输入流(数据流),转换为缓存字符流便于读取 175 br = new BufferedReader(new InputStreamReader(urlConn.getInputStream(),"utf-8")); 176 String str = null; 177 while((str = br.readLine()) != null){//一行行的读取,一行行的收集 178 sb.append(str); 179 } 180 } 181 182 } catch (IOException e) { 183 // TODO Auto-generated catch block 184 e.printStackTrace(); 185 } 186 187 return sb.toString();//转换为字符串,返回 188 } 189 197 }