首先,添加网络权限。
<uses-permission android:name="android.permission.INTERNET"/>
在properties配置文件中,进行配置。
logout=http://ipgw.neu.edu.cn:804/include/auth_action.php login=http://ipgw.neu.edu.cn:804/srun_portal_pc.php?ac_id=1& logoutData=username=20124849&password=XXX&action=logout&ajax=1 loginData=username=20124849&password=XXX&action=login&ac_id=1
布局文件,neu.xml配置文件:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:id="@+id/btn_login" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="一键登录"/> <TextView android:id="@+id/net_info" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="20sp"/> </LinearLayout>
properties工具类:
package com.tarena.viewer.util; import java.io.IOException; import java.io.InputStream; import java.util.Properties; public class PropertiesUtil { public static Properties getProperties(){ Properties prop = new Properties(); InputStream is = PropertiesUtil.class.getClassLoader().getResourceAsStream("user.properties"); try { prop.load(is); return prop; } catch (IOException e) { e.printStackTrace(); } return null; } }
最后是全部源码,经过测试,可以一键连接,想要改用户名和密码,就在properties配置文件中进行更改。
package com.tarena.viewer; import java.io.BufferedReader; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.UnsupportedEncodingException; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.nio.ByteBuffer; import java.util.Arrays; import java.util.Properties; import com.tarena.viewer.util.PropertiesUtil; import android.R.integer; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; /** * 一键登录东大校园网,需要添加Internet权限 * @author chenchi * */ public class MainActivity2 extends Activity implements OnClickListener { private String castQuantity(String quantity){ Long quantityLong = Long.parseLong(quantity); if(quantityLong > 1024 * 1024 *1024){ return quantityLong / (1024 * 1024 *1024) + "G"; }else if(quantityLong > 1024 * 1024){ return quantityLong / (1024 * 1024) + "M"; }else if(quantityLong > 1024){ return quantityLong / (1024) + "K"; }else{ return quantityLong + "B"; } } private String castTime(String time){ Long timeLong = Long.parseLong(time); return String.format("%02d:%02d:%02d", timeLong / 3600, (timeLong / 60) % 60, timeLong % 60); } private Handler handler = new Handler(){ public void handleMessage(Message msg) { switch (msg.what) { case 0: Toast.makeText(MainActivity2.this, "连接错误", Toast.LENGTH_SHORT).show(); break; case 1: TextView tx = (TextView) findViewById(R.id.net_info); String[] reback = ((String) msg.obj).split(","); StringBuilder builder = new StringBuilder(); builder.append("已用流量:"+castQuantity(reback[0])+" "); builder.append("登录时间:"+castTime(reback[1])+" "); builder.append("账户余额:"+reback[2]+" "); builder.append("IP地址:"+reback[5]); tx.setText(builder.toString()); break; } } }; private InputStream post(String path, String data){ try { //2.把网址封装成一个URL对象 URL url = new URL(path); //3.获取服务器和客户端的链接对象,此时还没有建立连接 HttpURLConnection conn = (HttpURLConnection) url.openConnection(); //4.对连接对象进行初始化 //设置请求方法,注意大写 conn.setRequestMethod("POST"); //设置连接超时 conn.setConnectTimeout(5000); //设置读取超时 conn.setReadTimeout(5000); conn.setDoOutput(true); OutputStream os = conn.getOutputStream(); os.write(data.getBytes()); os.close(); //5.发送请求,与服务器建立连接 conn.connect(); System.out.println(conn.getResponseCode()); if(conn.getResponseCode() == 200){ InputStream is = conn.getInputStream(); return is; }else{ Message msg = handler.obtainMessage(); msg.what = 0;//失败 handler.sendMessage(msg); return null; } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.neu); Button btn_download = (Button) findViewById(R.id.btn_login); btn_download.setOnClickListener(this); } @Override public void onClick(View v) { Thread thread = new Thread(new Runnable() { @Override public void run() { //1.确定网址 Properties prop = PropertiesUtil.getProperties(); String logout = prop.getProperty("logout"); String login = prop.getProperty("login"); String logoutData = prop.getProperty("logoutData"); String loginData = prop.getProperty("loginData"); post(logout, logoutData); post(login, loginData); InputStream is = post(logout, "action=get_online_info"); if(is== null){ return; } InputStreamReader isr; try { isr = new InputStreamReader(is, "utf-8"); BufferedReader br = new BufferedReader(isr); String reback = ""; String str = null; while((str = br.readLine()) != null){ reback += str; } System.out.println(reback); //把消息返送回主线程的消息队列 Message msg = new Message(); msg.obj = reback; msg.what = 1; handler.sendMessage(msg); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); System.out.println("抛异常啦"); } } }); thread.start(); } }