Android开发疫情查询app(实例代码)
程序员文章站
2022-07-06 16:41:28
一丶工作原理:app 通过请求本地tomcat发布的servlet (调用了 httpurlconnection 方法)获取mysql数据库当中的数据,获取数据并返回到app 当中,显示给用户。(其中...
一丶工作原理:
app 通过请求本地tomcat发布的servlet (调用了 httpurlconnection 方法)获取mysql数据库当中的数据,获取数据并返回到app 当中,显示给用户。(其中传递的格式为 json)
使用的工具:android studio 开发app eclipse 发布servlet,数据传递
二丶运行代码:
tomcat 发布的servlet 类:
package com.servlet; import java.io.ioexception; import javax.servlet.servletexception; import javax.servlet.annotation.webservlet; import javax.servlet.http.httpservlet; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; import com.bean.worldbean; import com.dao.dao; import com.google.gson.gson; /** * servlet implementation class worldservlet */ @webservlet("/worldservlet") public class worldservlet extends httpservlet { private static final long serialversionuid = 1l; /** * @see httpservlet#httpservlet() */ public worldservlet() { super(); // todo auto-generated constructor stub } /** * @see httpservlet#doget(httpservletrequest request, httpservletresponse response) */ protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { // todo auto-generated method stub response.setcontenttype("text/html;charset=utf-8"); request.setcharacterencoding("utf-8"); string s=null; //获取传递过来的参数 string date = request.getparameter("date"); string name =request.getparameter("name"); // gson 谷歌推出的用于生成和解析json 数据格式的工具 使用时需要 导入jar 包 我的是 gson-2.6.2.jar gson gson=new gson(); try { worldbean info= dao.getinfo(date,name); //将数据 转换为 json 格式 s=gson.tojson(info); } catch (exception e) { // todo auto-generated catch block e.printstacktrace(); } //system.out.println(s); //方法作用 只能打印输出文本格式的(包括html标签) 不可打印对象 response.getwriter().write(s); } /** * @see httpservlet#dopost(httpservletrequest request, httpservletresponse response) */ protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { // todo auto-generated method stub doget(request, response); } }
as 当中的mainactivity:
package com.example.yiqingdemo; import androidx.appcompat.app.appcompatactivity; import android.os.bundle; import android.util.log; import android.view.view; import android.widget.button; import android.widget.edittext; import android.widget.textview; import org.json.jsonobject; import java.io.bufferedinputstream; 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.malformedurlexception; import java.net.url; public class mainactivity extends appcompatactivity { edittext edittextcountry, edittextdate; textview textview; button button; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); edittextcountry = findviewbyid(r.id.edittext4); edittextdate = findviewbyid(r.id.edittext3); textview = findviewbyid(r.id.textview2); button = findviewbyid(r.id.button); button.setonclicklistener( new view.onclicklistener() { @override public void onclick(view v) { //本机tomcat 发布的网站 其实是一个servlet 类 必须先让本机发布(启动tomcat 运行) 然后才能访问改网站 string url = "http://192.168.0.106:8080/yiqingsearch/worldservlet?date=" + edittextdate.gettext().tostring() + "&name=" + edittextcountry.gettext().tostring(); get(url); } } ); } public void get(final string url) { new thread(new runnable() { @override public void run() { httpurlconnection connection = null; inputstream is = null; try { //获取url 对象 url url = new url(url); //获取httpurlconnection 对象 connection = (httpurlconnection) url.openconnection(); //默认为get方法 or post connection.setrequestmethod("get"); //默认不使用缓存 connection.setusecaches(false); //设置连接超时时间 单位毫秒 connection.setconnecttimeout(10000); //设置读取超时时间 connection.setreadtimeout(10000); //设置是否从httpurlconnection 读入,默认为true connection.setdoinput(true); //相应的码数为 200 if (connection.getresponsecode() == httpurlconnection.http_ok) { //获取输入流 is = connection.getinputstream(); //将输入流内的数据变为sting类型数据 string info = getstringfrominputstream(is); //转换为json 类型便于读取 jsonobject jsonobject = new jsonobject(info); textview.settext( "更新时间:" + jsonobject.getstring("updatetime") + "\n确诊人数:" + jsonobject.getstring("confirm") + "\n死亡人数:" + jsonobject.getstring("dead") + "\n治愈人数:" + jsonobject.getstring("heal") ); /* //获取url 网页的源代码 bufferedreader reader= new bufferedreader(new inputstreamreader(is)); //包装字节流为字符流 stringbuilder response = new stringbuilder(); string line; while((line = reader.readline())!=null){ response.append(line); } string s = response.tostring(); */ } } catch (exception e) { e.printstacktrace(); } finally { if (connection != null) { connection.disconnect(); } } } }).start(); } private static string getstringfrominputstream(inputstream is) throws exception { //定义字节数组缓存区 bytearrayoutputstream by = new bytearrayoutputstream(); byte[] buff = new byte[1024]; int len = -1; while ((len = is.read(buff)) != -1) { by.write(buff, 0, len); } is.close(); //将缓冲区的数据转换为 string 类型 string html = by.tostring(); by.close(); return html; } }
除此之外还需要给app赋予权限 :
as 的 androidmainfest 如下:
添加注释的为自主添加的权限
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.yiqingdemo"> <uses-permission android:name="android.permission.internet" /> <!--联网所需要的权限--> <uses-permission android:name="android.permission.access_wifi_state" /> <!-- 主要用于管理 wifi 连接的各方面--> <uses-permission android:name="android.permission.access_network_state" /> <!--主要用于监视一般网路连接 --> <application android:allowbackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundicon="@mipmap/ic_launcher_round" android:supportsrtl="true" android:theme="@style/apptheme" android:usescleartexttraffic="true"> <!-- 指示应用程序是否打算使用明文网络流量 --> <activity android:name=".mainactivity"> <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> </application> </manifest>
三丶 运行结果:
以上就是android开发实例(疫情查询app)的详细内容,更多关于android开发app的资料请关注其它相关文章!
推荐阅读