实例介绍
【实例简介】小程序外网抓取内网数据,安卓开发,从手机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; } }; }; }
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论