**AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mobile1920605114zyh0513homeword">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:usesCleartextTraffic="true"
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>
**
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"
tools:context=".MainActivity">
<TextView
android:id="@+id/tvMainTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="网站简介"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.03" />
<ImageView
android:id="@+id/ivMainBanner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:background="#00857676"
app:layout_constraintEnd_toEndOf="@+id/tvMainTitle"
app:layout_constraintStart_toStartOf="@+id/tvMainTitle"
app:layout_constraintTop_toBottomOf="@+id/tvMainTitle"
app:srcCompat="@drawable/ic_launcher_foreground" />
<TextView
android:id="@+id/tvMainContent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/ivMainBanner" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity
package com.example.mobile1920605114zyh0513homeword;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
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;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private TextView tvMainTitle;
private ImageView ivMainBanner;
private TextView tvMainContent;
// 根据网站实际情况定义图片和文本路径
final String IMAGEPATH = "http://192.168.0.106:8080/web/timg.jpg";
final String TEXTPATH = "http://192.168.0.106:8080/web/text1.txt";//UTD-8
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
new Thread(){
@Override
public void run() {
super.run();
try {
URL imageUrl = new URL(IMAGEPATH);
//定义连接对象
HttpURLConnection imageConnection = (HttpURLConnection) imageUrl.openConnection();
imageConnection.setRequestMethod("GET");//访问方式
imageConnection.setConnectTimeout(5000);//访问超时
//访问服务器,并通过字节输入流方式保存返回结果
InputStream imageIS = imageConnection.getInputStream();
final Bitmap bitmap = BitmapFactory.decodeStream(imageIS);
runOnUiThread(new Runnable() {
@Override
public void run() {
ivMainBanner.setImageBitmap(bitmap);
Log.i("TAG", "run: "+bitmap);
}
});
imageIS.close();//关闭输入流
imageConnection.disconnect();//关闭链接
} catch (Exception e) {
e.printStackTrace();
}
}
}.start();
//访问网络文字
new Thread(){
@Override
public void run(){
super.run();
try {
//定义URL访问对象
URL textURL = new URL(TEXTPATH);
//定义连接对象
HttpURLConnection textConnection = (HttpURLConnection) textURL.openConnection();
//访问服务器,并通过字节输入流方式保存返回结果
InputStream textIs = textConnection.getInputStream();
//字节流转换为字符流
InputStreamReader isr = new InputStreamReader(textIs);
//字符流转换为字符缓冲流
final BufferedReader br = new BufferedReader(isr);
//通过readLine方法读取字符缓冲流的内容
String line = null;
String temp = "";
while ((line = br.readLine()) != null) {
temp+=line;//读取1行文本
temp += "\n";//换行
}
//在runOnUiThread()方法中,将读取内容赋值给控件。
final String msg = temp;
runOnUiThread(new Runnable() {
@Override
public void run() {
tvMainContent.setText(msg);
Log.i("TAG", "run: "+msg);
}
});
isr.close();
br.close();
textIs.close();
textConnection.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
}
}.start();
}
//初始化控件
private void initView() {
tvMainTitle = findViewById(R.id.tvMainTitle);
ivMainBanner = findViewById(R.id.ivMainBanner);
tvMainContent = findViewById(R.id.tvMainContent);
}
}