Last week, the outbreak data did show the use of the mobile phone side, by the following code to access the server as at the end, so as to display the data on the server side as in the end come out, use the same reptile, crawling to the latest data,
Layout files, 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 = "click the query" /> <- view with scroll bar ->! <ScrollView Android: layout_width = "match_parent" Android: layout_height = "match_parent"> ! <- response data -> <TextView Android: ID = "@ + ID / RESPONSE_DATA" Android: layout_width = "match_parent" Android: layout_height = "the wrap_content" /> </ the ScrollView> </ the LinearLayout>
xml file
<?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 = newThe URL of ( "http://10.0.2.2:8043/yiqingshuju1.0/androidServlet?date=2020-03-14" ); // the URL of the URL of new new url = ( " https://www.baidu.com/ ") ; connection = (the HttpURLConnection) url.openConnection (); // set request method connection.setRequestMethod ( "the GET" ); // set the connection time (ms) connection.setConnectTimeout (5000 ); // set the read timeout ( ms) connection.setReadTimeout (5000 ); // returns an input stream 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); } }); } }