06移动端疫情展示
MainActivity:
package com.example.esinquirysystem;
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 com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Type;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private EditText editText1;
private EditText editText2;
private Button button;
private String result;
private TextView textView;
private static final String TAG="MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText1=findViewById(R.id.editText);
editText2=findViewById(R.id.editText2);
button=findViewById(R.id.button);
textView=findViewById(R.id.textView);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Thread(new Runnable() {
@Override
public void run() {
requestDataByGet();
}
}).start();
}
});
}
public void requestDataByGet()
{ if (editText1.getText().toString() != null && editText2.getText().toString() != null)
{ try {
URL url = new URL("http://117.50.96.227/EpidemicInfo/city/getInfoByCountry/" +editText2.getText().toString()+"/"+editText1.getText().toString());
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setConnectTimeout(30 * 1000);
httpURLConnection.setRequestMethod("GET");
httpURLConnection.setRequestProperty("Content-Type", "application/json");
httpURLConnection.setRequestProperty("Charset", "UTF-8");
httpURLConnection.setRequestProperty("Accept-Charset", "UTF-8");
httpURLConnection.connect();//发起连接
int responseCode = httpURLConnection.getResponseCode();
String responseMessage = httpURLConnection.getResponseMessage();
if (responseCode == HttpURLConnection.HTTP_OK) {
InputStream inputStream = httpURLConnection.getInputStream();
result = streamToString(inputStream);
runOnUiThread(new Runnable() {
@Override
public void run() {
List<yiqingclass> list=new ArrayList<yiqingclass>();
Gson gson=new Gson();
Type cityType = new TypeToken<ArrayList<yiqingclass>>() {}.getType();
list=gson.fromJson(result,cityType);
String ziz="";
for(yiqingclass yiqi:list) {
ziz=yiqi.getName()+"\n确诊:"+yiqi.getConfirm()+"\n治愈:"+yiqi.getHeal()+
"\n死亡:"+yiqi.getDead()+"\n";
break;
}
textView.setText(ziz);
}
});
}
} catch(MalformedURLException e){
e.printStackTrace();
} catch(IOException e){
e.printStackTrace();
}}
}
public String streamToString(InputStream is) {
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len;
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) {
Log.e(TAG, e.toString());
return null;
}
}
}
yiqingclass:
package com.example.esinquirysystem;
public class yiqingclass {
private int id;
private String name;
private String confirm;
private String suspect;
private String dead;
public String getDead() {
return dead;
}
public void setDead(String dead) {
this.dead = dead;
}
private String heal;
private String severe;
private String idcode;
private String lastupdatetime;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getConfirm() {
return confirm;
}
public yiqingclass(int id, String name, String confirm, String suspect, String heal, String severe, String idcode, String lastupdatetime) {
this.id = id;
this.name = name;
this.confirm = confirm;
this.suspect = suspect;
this.heal = heal;
this.severe = severe;
this.idcode = idcode;
this.lastupdatetime = lastupdatetime;
}
public void setConfirm(String confirm) {
this.confirm = confirm;
}
@Override
public String toString() {
return "yiqingclass{" +
"id=" + id +
", name='" + name + '\'' +
", confirm='" + confirm + '\'' +
", suspect='" + suspect + '\'' +
", heal='" + heal + '\'' +
", severe='" + severe + '\'' +
", idcode='" + idcode + '\'' +
", lastupdatetime='" + lastupdatetime + '\'' +
'}';
}
public String getSuspect() {
return suspect;
}
public void setSuspect(String suspect) {
this.suspect = suspect;
}
public String getHeal() {
return heal;
}
public void setHeal(String heal) {
this.heal = heal;
}
public String getSevere() {
return severe;
}
public void setSevere(String severe) {
this.severe = severe;
}
public String getIdcode() {
return idcode;
}
public void setIdcode(String idcode) {
this.idcode = idcode;
}
public String getLastupdatetime() {
return lastupdatetime;
}
public void setLastupdatetime(String lastupdatetime) {
this.lastupdatetime = lastupdatetime;
}
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/textview3"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/editText"
app:layout_constraintHorizontal_bias="0.566"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.686" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/textview1"
android:textSize="36sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.123" />
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:text="@string/name"
android:hint="#2020-03-20"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.797"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.523"
android:autofillHints="" />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:text="@string/name1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.797"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText"
app:layout_constraintVertical_bias="0.223"
android:autofillHints="" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/textview"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/editText"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.524" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.445"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.833" />
</androidx.constraintlayout.widget.ConstraintLayout>