中国矿业大学校园网自动登录APP程序

执行完程序app会自动关闭,不是闪退

package com.example.autologin;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;

import org.jetbrains.annotations.NotNull;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;

public class MainActivity extends AppCompatActivity {

    private int GET_DATA_SUCCESS = 101;
    private HttpURLConnection connection;
    private InputStream inputStream;
    private BufferedReader bufferedReader;
    private String number="学号";//学号
    private String password="密码";//密码
    private String operator="%40cmcc";//运营商
    private String geturl="http://10.2.5.251:801/eportal/?c=Portal&a=login&login_method=1&user_account="+number+operator+"&user_password="+password;

    Handler mHandler = new Handler();
    Runnable r = new Runnable() {

        @Override
        public void run() {
            System.exit(0);
        }
    };

    Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message msg) {
            if (msg.what==GET_DATA_SUCCESS){
                String data=msg.getData().getString("data");
                System.out.println("data:"+data);

                String  jsonData=data.substring(1,data.length()-1);
                JSONObject jsonObject = null;
                try {
                    jsonObject = new JSONObject(jsonData);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                String getmsg = null;
                String ret_code = null;
                try {
                    getmsg = jsonObject.getString("msg");
                    ret_code= jsonObject.getString("ret_code");
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                if (getmsg.equals("认证成功")) {
                    Toast.makeText(MainActivity.this,"登录成功",Toast.LENGTH_LONG).show();
                }
                else if (ret_code.equals("2")){
                    Toast.makeText(MainActivity.this,"已登录",Toast.LENGTH_LONG).show();
                }
                else{
                    Toast.makeText(MainActivity.this,"登录失败",Toast.LENGTH_LONG).show();
                }
                mHandler.postDelayed(r, 1000);//延时100毫秒

            }
            return false;
        }
    });


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        connect();

    }

    private void connect() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                String data = getDataFromServer();
                //创建信息对象
                Message message=Message.obtain();
                Bundle bundle=new Bundle();
                bundle.putString("data",data);
                message.setData(bundle);
                message.what=GET_DATA_SUCCESS;
                //向主线程发信息
                handler.sendMessage(message);
            }
        }).start();
    }


    private String getDataFromServer() {
        try {
            URL url=new URL(geturl);
            System.out.println("url"+url);
            //打开链接
            connection = (HttpURLConnection) url.openConnection();
            //判断并处理结果
            if (connection.getResponseCode()==200) {
                //获取数据流
                inputStream = connection.getInputStream();
                bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
                StringBuilder stringBuilder = new StringBuilder();
                for (String line = ""; (line = bufferedReader.readLine()) != null; ) {
                    stringBuilder.append(line);
//                }
                    return stringBuilder.toString();
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                if (bufferedReader!=null)bufferedReader.close();
                if (inputStream!=null)inputStream.close();
                if (connection!=null)connection.disconnect();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return "";
    }
}

你可能感兴趣的:(安卓,android,android,studio)