基本信息
源码名称:android app实现外网抓取内网数据
源码大小:12.23M
文件格式:.zip
开发语言:Java
更新时间:2020-05-11
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍
小程序外网抓取内网数据,安卓开发,从手机APP上抓取内网数据的小程序

package com.example.myapplication;

import android.app.Activity;
import android.os.Bundle;


import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.EditText;


import android.widget.TextView;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class MainActivity extends Activity {

    private EditText input_id;
    private View btnTest;
    private View btnClean;
    private TextView tvTestResult;

    private EditText input_cardno;
    private TextView show_area, show_sex, show_bitrhday, show_reason;


    public static final String INFO_URL = "http://apis.juhe.cn/idcard/index?cardno=";
    //API中的KEY
    public static final String INPUT_URL_M = "&dtype=json&key=8fe2923a80a44ce4d48715cb226123d2";
    String area;        //地区
    String sex;         //性别
    String birthday;    //生日
    int resultcode;
    String reason;


    @Override
    protected void onCreate(Bundle savedInstanceState)
    {


        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        btnTest=findViewById(R.id.btnTestSql);
        btnClean=findViewById(R.id.btnClean);
        tvTestResult = (TextView)findViewById(R.id.tvTestResult);
        input_id = (EditText) findViewById(R.id.input_id);
        input_cardno = (EditText) findViewById(R.id.input_cardno);


        show_area = (TextView) findViewById(R.id.show_area);
        show_sex = (TextView) findViewById(R.id.show_sex);
        show_bitrhday = (TextView) findViewById(R.id.show_birthday);
        show_reason = (TextView) findViewById(R.id.show_reason);



        btnTest.setOnClickListener(getClickEvent());
        btnClean.setOnClickListener(getClickEvent());
    }






    public void search(View view) {

        show_area.setText("");
        show_sex.setText("");
        show_reason.setText("");
        show_bitrhday.setText("");
        makeHttpRequest();
    }

    private void makeHttpRequest() {
        //开启线程来发起网络请求
        new Thread(new Runnable() {
            @Override
            public void run() {
                //初始化创建HttpURLConnection
                HttpURLConnection connection = null;
                //初始化创建BufferedReader
                BufferedReader reader = null;
                //获取Text文本框中的值
                String id = input_cardno.getText().toString();
                try {
                    //将我们的URL传进来
                    URL url = new URL(INFO_URL   id   INPUT_URL_M);
                    //使用URL打开链接
                    connection = (HttpURLConnection) url.openConnection();
                    //设置网络请求方式
                    connection.setRequestMethod("GET");
                    //设置读取时间
                    connection.setReadTimeout(8000);
                    //设置链接时间
                    connection.setConnectTimeout(8000);
                    //初始化创建InputStream
                    //urlConn设置请求头信息
                    //设置请求中的媒体类型信息。
                    connection.setRequestProperty("Content-Type", "application/json");
                    //设置客户端与服务连接类型
                    connection.addRequestProperty("Connection", "Keep-Alive");
                    // 开始连接
                    connection.connect();
                    String result = streamToString(connection.getInputStream());

                    showResponse(result);
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    if (reader != null) {
                        try {
                            reader.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    if (connection != null) {
                        connection.disconnect();
                    }
                }
            }
        }).start();
    }

    public String streamToString(InputStream is) {
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int len = 0;
            while ((len = is.read(buffer)) != -1) {
                baos.write(buffer, 0, len);
            }
            baos.close();
            is.close();
            byte[] byteArray = baos.toByteArray();
            return new String(byteArray);
        } catch (Exception e) {

            return null;
        }
    }

    private void showResponse(final String s) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                FunctionJson(s);
            }
        });
    }


    private void FunctionJson(String data) {
        try {
            JSONObject jsonObject = new JSONObject(data);
            resultcode = jsonObject.optInt("resultcode");
            JSONObject jsonObject2 = jsonObject.getJSONObject("result");
            area = jsonObject2.optString("area").toString();
            sex = jsonObject2.optString("sex").toString();
            birthday = jsonObject2.optString("birthday").toString();
            reason = jsonObject.optString("reason").toString();

        } catch (JSONException e) {
            e.printStackTrace();
        }

        if (resultcode == 200) {

            show_area.setText("户籍:"   area);
            show_sex.setText("性别:"   sex);
            show_bitrhday.setText("出生年月:"   birthday);
            show_reason.setText("查询成功");

        } else {
            show_reason.setText("查询失败,请输入正确的身份证号");
        }


    }



    private View.OnClickListener getClickEvent(){
        return new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                tvTestResult.setText("...");
                if(v==btnTest){
                    test();

                }
            }
        };
    }
    private void test()
    {
        Runnable run = new Runnable()
        {
            @Override
            public void run()
            {
                String key = input_id.getText().toString();
                String ret = DBUtil.QuerySQL(key);
                Message msg = new Message();
                msg.what=1001;
                Bundle data = new Bundle();
                data.putString("result", ret);
                msg.setData(data);
                mHandler.sendMessage(msg);
            }
        };
        new Thread(run).start();

    }

    Handler mHandler = new Handler(){
        public void handleMessage(android.os.Message msg) {
            switch (msg.what)
            {
                case 1001:
                    String str = msg.getData().getString("result");
                    tvTestResult.setText(str);
                    break;

                default:
                    break;
            }
        };
    };

}