软工第五周博客
本周做了使用手机端对疫情数据进行显示,通过在as端对服务器进行访问,从而将服务器端的数据在as端显示出来,同样利用爬虫,爬取最新数据,代码如下
布局文件,activity_main
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <EditText android:id="@+id/editText" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:inputType="textPersonName" android:text="" /> <Button android:id="@+id/send_request" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="点击查询" /> <!--带滚动条的视图--> <ScrollView android:layout_width="match_parent" android:layout_height="match_parent"> <!--响应数据--> <TextView android:id="@+id/response_data" android:layout_width="match_parent" android:layout_height="wrap_content" /> </ScrollView> </LinearLayout>
xml文件
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.a14769.yiqingchaxun"> <uses-permission android:name="android.permission.INTERNET"/> <application android:usesCleartextTraffic="true" 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"> <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>
MainActivity
package com.example.a14769.yiqingchaxun; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.EditText; import android.widget.TextView; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.ProtocolException; import java.net.URL; public class MainActivity extends AppCompatActivity { private TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViewById(R.id.send_request).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { send(); } }); textView = (TextView) findViewById(R.id.response_data); } private void send() { //开启线程,发送请求 new Thread(new Runnable() { @Override public void run() { HttpURLConnection connection = null; BufferedReader reader = null; try { EditText editText =(EditText)findViewById(R.id.editText); String timeend = editText.getText().toString(); URL url = new URL("http://10.0.2.2:8043/yiqingshuju1.0/androidServlet?date=2020-03-14"); //URL url = new URL("https://www.baidu.com/"); connection = (HttpURLConnection) url.openConnection(); //设置请求方法 connection.setRequestMethod("GET"); //设置连接超时时间(毫秒) connection.setConnectTimeout(5000); //设置读取超时时间(毫秒) connection.setReadTimeout(5000); //返回输入流 InputStream in = connection.getInputStream(); //读取输入流 reader = new BufferedReader(new InputStreamReader(in)); StringBuilder result = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { result.append(line); } show(result.toString()); } catch (MalformedURLException e) { e.printStackTrace(); } catch (ProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (reader != null) { try { reader.close(); } catch (IOException e) { e.printStackTrace(); } } if (connection != null) {//关闭连接 connection.disconnect(); } } } }).start(); } private void show(final String result) { runOnUiThread(new Runnable() { @Override public void run() { textView.setText(result); } }); } }