在Android中制作考勤应用

时间:2017-03-19 08:15:25

标签: java android json http

我必须为大学制作考勤应用程序。该应用程序将从大学网站获取数据,并根据用户登录名和密码在应用程序上显示。

当我们登录大学的网站时,我们必须将package ftclient; public class Ftclientmini { private Socket echoSocket; private ServerSocket servSocket; private Socket dataSocket; PrintWriter out; BufferedReader in; PrintWriter dataOut; BufferedReader dataIn; /** * @param args the command line arguments */ public static void main(String[] args) throws IOException { String hostName = args[0]; int portNumber = Integer.parseInt(args[1]); String filename = ""; int datanum = 0; String command = args[2]; if (command.equals("-l")){ if (args.length != 4) System.err.println("Usage: java EchoClient <host name> " + "<port number> <command> <dataport>"); datanum = Integer.parseInt(args[3]); } Ftclientmini one = new Ftclientmini(); one.startControl(hostName, portNumber); System.out.println("have set up the control connection\n"); one.sendCommand(command, filename, hostName, datanum); String servResponse = one.in.readLine(); if (servResponse != null){ System.out.println(servResponse); if (servResponse.equals("invalid command")) System.exit(0); else{ //byte[] myarray = new byte[1024]; String bytesRead; String[] retVal; one.startData(datanum); if (command.equals("-l")){ while ((bytesRead = one.dataIn.readLine()) != null){ //bytesRead = one.dataIn.readLine(); retVal = bytesRead.split(","); for (String temp: retVal){ System.out.println(temp); } } } } } one.servSocket.close(); one.dataSocket.close(); one.echoSocket.close(); System.exit(0); } void startControl(String name, int number){ try { echoSocket = new Socket(name, number); out = new PrintWriter( echoSocket.getOutputStream(), true); in = new BufferedReader( new InputStreamReader(echoSocket.getInputStream())); } catch (IOException e) { System.err.println("Couldn't get IO for connection to" + name); e.printStackTrace(); } } void sendCommand(String command, String filename, String host, int datanum){ String stringData = Integer.toString(datanum); if (command.equals("-l")){ command = command.concat("#" + host + "#" + stringData +"#"); System.out.println("sending: " + command); out.println(command); } else { command = command.concat("#" + host + "#" + stringData + "#" + filename + "#"); System.out.println("sending: " + command); out.println(command); } System.out.println("finished sending command"); } void startData(int datanum){ try { servSocket = new ServerSocket(datanum); //servSocket.setSoTimeout(10000); System.out.println("wait for a client"); dataSocket = servSocket.accept(); System.out.println("ftclient.java has been accepted"); dataOut = new PrintWriter( dataSocket.getOutputStream(), true); dataIn = new BufferedReader( new InputStreamReader(dataSocket.getInputStream())); } catch (IOException e) { System.err.println("Couldn't get IO for connection"); e.printStackTrace(); } } } id放在我的应用上,这样我就可以在应用中看到它。

我搜索了passwordhttpurlconnectionhttpgethttppost

到目前为止,我已经明白我需要jsoup加载学院的考勤网站,然后httprequest发布用户名和密码,然后httppost抓取数据来自HTML页面。

但我看过教程只是为了请求JSON页面,但是如何请求HTML页面?并登录到它?

以下是我尝试从JSON

收集数据的内容
jsoup

1 个答案:

答案 0 :(得分:0)

&#13;
&#13;
 String mLoadURL="http://www.google.com";
 
 public class LoadHtml extends AsyncTask<String, String, String> {

        @Override
        protected String doInBackground(String... params) {
            URL url = null;
            try {
                url = new URL(mLoadURL);
                StringBuilder stringBuilder = new StringBuilder();
                URLConnection conn = url.openConnection();
                // Get the response
                BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                String line = "";
                while ((line = rd.readLine()) != null) {
                    stringBuilder.append(line);
                }
                return stringBuilder.toString();
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return "";
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
             if (!s.trim().isEmpty()) {
               //load html to webview
             }
        }
    }
&#13;
&#13;
&#13;

使用以下方法调用AsyncTask:

LoadHtml loadHtml= new LoadHtml();
load.execute();