您好,登錄后才能下訂單哦!
這篇文章主要為大家展示了Android如何開(kāi)發(fā)疫情查詢(xún)app,內(nèi)容簡(jiǎn)而易懂,希望大家可以學(xué)習(xí)一下,學(xué)習(xí)完之后肯定會(huì)有收獲的,下面讓小編帶大家一起來(lái)看看吧。
一丶工作原理:
App 通過(guò)請(qǐng)求本地tomcat發(fā)布的servlet (調(diào)用了 HttpURLConnection 方法)獲取MySQL數(shù)據(jù)庫(kù)當(dāng)中的數(shù)據(jù),獲取數(shù)據(jù)并返回到App 當(dāng)中,顯示給用戶(hù)。(其中傳遞的格式為 json)
使用的工具:Android Studio 開(kāi)發(fā)APP Eclipse 發(fā)布Servlet,數(shù)據(jù)傳遞
二丶運(yùn)行代碼:
Tomcat 發(fā)布的Servlet 類(lèi):
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; //獲取傳遞過(guò)來(lái)的參數(shù) String date = request.getParameter("date"); String name =request.getParameter("name"); // Gson 谷歌推出的用于生成和解析JSON 數(shù)據(jù)格式的工具 使用時(shí)需要 導(dǎo)入jar 包 我的是 gson-2.6.2.jar Gson gson=new Gson(); try { worldbean info= Dao.getinfo(date,name); //將數(shù)據(jù) 轉(zhuǎn)換為 Json 格式 s=gson.toJson(info); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } //System.out.println(s); //方法作用 只能打印輸出文本格式的(包括html標(biāo)簽) 不可打印對(duì)象 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 當(dāng)中的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) { //本機(jī)tomcat 發(fā)布的網(wǎng)站 其實(shí)是一個(gè)servlet 類(lèi) 必須先讓本機(jī)發(fā)布(啟動(dòng)tomcat 運(yùn)行) 然后才能訪問(wèn)改網(wǎng)站 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 對(duì)象 URL Url = new URL(url); //獲取httpURlConnection 對(duì)象 connection = (HttpURLConnection) Url.openConnection(); //默認(rèn)為get方法 or post connection.setRequestMethod("GET"); //默認(rèn)不使用緩存 connection.setUseCaches(false); //設(shè)置連接超時(shí)時(shí)間 單位毫秒 connection.setConnectTimeout(10000); //設(shè)置讀取超時(shí)時(shí)間 connection.setReadTimeout(10000); //設(shè)置是否從httpUrlConnection 讀入,默認(rèn)為true connection.setDoInput(true); //相應(yīng)的碼數(shù)為 200 if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) { //獲取輸入流 is = connection.getInputStream(); //將輸入流內(nèi)的數(shù)據(jù)變?yōu)镾ting類(lèi)型數(shù)據(jù) String info = getStringFromInputStream(is); //轉(zhuǎn)換為JSON 類(lèi)型便于讀取 JSONObject jsonObject = new JSONObject(info); textView.setText( "更新時(shí)間:" + jsonObject.getString("updatetime") + "\n確診人數(shù):" + jsonObject.getString("confirm") + "\n死亡人數(shù):" + jsonObject.getString("dead") + "\n治愈人數(shù):" + jsonObject.getString("heal") ); /* //獲取url 網(wǎng)頁(yè)的源代碼 BufferedReader reader= new BufferedReader(new InputStreamReader(is)); //包裝字節(jié)流為字符流 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 { //定義字節(jié)數(shù)組緩存區(qū) 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(); //將緩沖區(qū)的數(shù)據(jù)轉(zhuǎn)換為 String 類(lèi)型 String html = by.toString(); by.close(); return html; } }
除此之外還需要給APP賦予權(quán)限 :
As 的 AndroidMainfest 如下:
添加注釋的為自主添加的權(quán)限
<?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" /> <!--聯(lián)網(wǎng)所需要的權(quán)限--> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <!-- 主要用于管理 WIFI 連接的各方面--> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <!--主要用于監(jiān)視一般網(wǎng)路連接 --> <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"> <!-- 指示應(yīng)用程序是否打算使用明文網(wǎng)絡(luò)流量 --> <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>
三丶 運(yùn)行結(jié)果:
以上就是關(guān)于Android如何開(kāi)發(fā)疫情查詢(xún)app的內(nèi)容,如果你們有學(xué)習(xí)到知識(shí)或者技能,可以把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。