赞
踩
手机登录校园网极其费劲,时常打不开校园网的登录页面,打开了也存不住账号密码
为了不要每次都输一遍,打算做个手动登录校园网的APP
经过抓包发现校园网只需通过get请求发送ip,账号和密码即可登录成功,就在某篇文章里找到了这个GET获取天气的方法,改过许多遍,在我目前的JDK8环境下能跑起来
注意,Android中访问网络需要开启子线程来完成
- package com.example.myapplication;
-
- import android.app.Notification;
- import android.app.NotificationChannel;
- import android.app.NotificationManager;
- import android.app.PendingIntent;
- import android.content.Intent;
- import android.os.Build;
- import android.os.Bundle;
- import android.os.Handler;
- import android.os.Looper;
- import android.os.Message;
- import android.util.Log;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.Button;
- import android.widget.PopupWindow;
- import android.widget.TextView;
- import android.widget.Toast;
-
- import androidx.annotation.NonNull;
- import androidx.appcompat.app.AppCompatActivity;
- import androidx.core.app.NotificationCompat;
-
- public class MainActivity extends AppCompatActivity {
- private TextView tvContent;
- private Handler mHandler = new Handler(Looper.myLooper()){
-
- public void handleMessage(@NonNull Message msg){
- tvContent = findViewById(R.id.tv_content);
- super.handleMessage(msg);
-
- if(msg.what==0){
- String strData = (String)msg.obj;
- tvContent.setText(strData);
- Toast.makeText(MainActivity.this,"主线程收到网络消息啦!",Toast.LENGTH_SHORT).show();
- }
- }
- };
-
- private String getStringFormNet(){
- //从网络上获取字符串
- return NetUtil.getWeatherOfCity("赣州");
- }
-
- public void start(View view){
- //做一个耗时任务
- new Thread(new Runnable(){
- @Override
- public void run(){
- String stringFormNet = getStringFormNet();
-
- //使用handler来发送消息
- Message message = new Message();
- message.what = 0;//用于区分是谁发的消息
- message.obj = stringFormNet;
- mHandler.sendMessage(message);
-
- }
- }).start();
- Toast.makeText(MainActivity.this,"开启子线程请求网络!",Toast.LENGTH_SHORT).show();
- }
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
-
-
- start(findViewById(R.id.tv_content));//tvcontent是一个textview
- Log.d("success", "成功启动 ");
-
- setContentView(R.layout.activity_main);
-
-
-
-
-
-
- }
-
MainActivity.java
使用geteather of city函数完成对城市的传参,并且拼接get请求的url传入Netutil函数进行http请求
- package com.example.myapplication;
-
- import android.util.Log;
-
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.net.HttpURLConnection;
- import java.net.MalformedURLException;
- import java.net.ProtocolException;
- import java.net.URL;
-
- public class NetUtil{
- public static String BASE_URL="https://v0.yiketianqi.com/free/day";
- public static String APP_ID="14846972";
- public static String APP_SECRET="Guya4Gz2";
-
- public static String doGet(String url){
- BufferedReader reader = null;
- String bookHSONString = null;
- String bookJSONString;
- HttpURLConnection httpURLConnection = null;
- try{
- String uc = null;
- //1.HttpURLConnection建立连接
-
- URL requestUrl = new URL(url);
- httpURLConnection = (HttpURLConnection)requestUrl.openConnection();//打开连接
- httpURLConnection.setRequestMethod("GET");//两种方法GET/POST
- httpURLConnection.setConnectTimeout(5000);//设置超时连接时间
- httpURLConnection.connect();
-
- //2.InputStream获取二进制流
- InputStream inputstream = httpURLConnection.getInputStream();
-
- //3.InputStreamReader将二进制流进行包装成BufferedReader
- reader = new BufferedReader(new InputStreamReader(inputstream));
-
- //4.从BufferedReader中读取String字符串,用StringBulider接收
- StringBuilder builder = new StringBuilder();
-
-
- String line;
- while((line=reader.readLine())!=null){
- builder.append(line);
- builder.append("\n");
- }
-
- if(builder.length()==0)
- {
- return null;
- }
-
- //5.StringBulider将字符串进行拼接
- bookJSONString = builder.toString();
-
- }
-
- catch (ProtocolException e) {
- throw new RuntimeException(e);
- } catch (MalformedURLException e) {
- throw new RuntimeException(e);
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
-
-
- finally {
- // 关闭连接
- if (httpURLConnection != null) {
- httpURLConnection.disconnect();
- }
- if (reader != null) {
- try {
- reader.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- return bookJSONString;
- }
-
- public static String getWeatherOfCity(String city){
- //拼接处get请求的url
- String weatherUrl = BASE_URL+"?"+"appid="+APP_ID+"&"+"appsecret="+APP_SECRET+"&"+"city="+city;
- //打印上面的url
- Log.d("fan","-----weatherUrl----"+weatherUrl);
-
- //调用上文所写的doGet方法,传参
- String weatherResult = doGet(weatherUrl);
- return decodeUnicode(weatherResult);
- }
-
- //解码Unicode,将其转化为我们认识的汉字
- public static String decodeUnicode(String unicodeStr) {
- if (unicodeStr == null) {
- return null;
- }
- StringBuffer retBuf = new StringBuffer();
- int maxLoop = unicodeStr.length();
- for (int i = 0; i < maxLoop; i++) {
- if (unicodeStr.charAt(i) == '\\') {
- if ((i < maxLoop - 5) && ((unicodeStr.charAt(i + 1) == 'u') || (unicodeStr.charAt(i + 1) == 'U')))
- try {
- retBuf.append((char) Integer.parseInt(unicodeStr.substring(i + 2, i + 6), 16));
- i += 5;
- } catch (NumberFormatException localNumberFormatException) {
- retBuf.append(unicodeStr.charAt(i));
- }
- else
- retBuf.append(unicodeStr.charAt(i));
- } else {
- retBuf.append(unicodeStr.charAt(i));
- }
- }
- return retBuf.toString();
- }
-
-
- }
-
-
-
-
NetUtil.java 运行子线程
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。