diff --git a/build.gradle.kts b/build.gradle.kts index b42421c..fbd1fa9 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -50,6 +50,12 @@ dependencies { implementation ("io.github.lucksiege:ucrop:v3.11.2") implementation ("io.github.lucksiege:camerax:v3.11.2") implementation ("com.github.bumptech.glide:glide:4.15.1") + + implementation ("com.squareup.okhttp3:okhttp:4.11.0") + implementation ("com.google.code.gson:gson:2.10.1") + implementation ("com.squareup.okhttp3:logging-interceptor:4.9.3") + implementation ("com.squareup.retrofit2:retrofit:2.9.0") + implementation ("com.squareup.retrofit2:converter-gson:2.9.0") //权限 diff --git a/src/main/java/com/hnucm/c202201020328/ApiService.java b/src/main/java/com/hnucm/c202201020328/ApiService.java new file mode 100644 index 0000000..aed4e84 --- /dev/null +++ b/src/main/java/com/hnucm/c202201020328/ApiService.java @@ -0,0 +1,8 @@ +package com.hnucm.c202201020328; +import retrofit2.Call; +import retrofit2.http.GET; +import java.util.List; +public interface ApiService { + @GET("test") // 替换为你的 API 路径 + Call> getSystemMessages(); +} diff --git a/src/main/java/com/hnucm/c202201020328/System_messages.java b/src/main/java/com/hnucm/c202201020328/System_messages.java index 0e1d0e2..f440278 100644 --- a/src/main/java/com/hnucm/c202201020328/System_messages.java +++ b/src/main/java/com/hnucm/c202201020328/System_messages.java @@ -1,5 +1,13 @@ package com.hnucm.c202201020328; public class System_messages { - public String title; + String title; + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } } diff --git a/src/main/java/com/hnucm/c202201020328/System_messagesActivity.java b/src/main/java/com/hnucm/c202201020328/System_messagesActivity.java index b1ee773..7ecd592 100644 --- a/src/main/java/com/hnucm/c202201020328/System_messagesActivity.java +++ b/src/main/java/com/hnucm/c202201020328/System_messagesActivity.java @@ -1,6 +1,6 @@ package com.hnucm.c202201020328; - import android.os.Bundle; +import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -16,34 +16,65 @@ import com.gyf.immersionbar.ImmersionBar; import java.util.ArrayList; import java.util.List; +import retrofit2.Call; +import retrofit2.Callback; +import retrofit2.Response; +import retrofit2.Retrofit; +import retrofit2.converter.gson.GsonConverterFactory; + public class System_messagesActivity extends AppCompatActivity { RecyclerView recyclerView1; MyAdapter myAdapter; - List system_messages=new ArrayList<>(); + List system_messages = new ArrayList<>(); + ApiService apiService; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_system_messages); ImmersionBar.with(this).init(); - recyclerView1=findViewById(R.id.recyclerview1); - for (int i=1;i<=25;i++){ - System_messages systemMessages=new System_messages(); - String c=String.valueOf(i); - systemMessages.title=c; - system_messages.add(systemMessages); - } - myAdapter=new MyAdapter(); - recyclerView1.setAdapter(myAdapter); + recyclerView1 = findViewById(R.id.recyclerview1); recyclerView1.setLayoutManager(new LinearLayoutManager(this)); - } - public class MyAdapter extends RecyclerView.Adapter{ + // 初始化 Retrofit 和 ApiService + Retrofit retrofit = new Retrofit.Builder() + .baseUrl("https://dev.usemock.com/673ff29f94b857f277d90f07/") // 替换为你的 API 基础 URL + .addConverterFactory(GsonConverterFactory.create()) + .build(); + apiService = retrofit.create(ApiService.class); + + // 发起网络请求 + fetchSystemMessages(); + } + private void fetchSystemMessages() { + Call> call = apiService.getSystemMessages(); + call.enqueue(new Callback>() { + @Override + public void onResponse(Call> call, Response> response) { + if (response.isSuccessful() && response.body() != null) { + Log.i("test", "Response: " + response.body().toString()); + system_messages.clear(); + system_messages.addAll(response.body()); + myAdapter = new MyAdapter(); + recyclerView1.setAdapter(myAdapter); + } else { + Log.e("test", "Response failed: " + response.message()); + } + } + + + @Override + public void onFailure(Call> call, Throwable t) { + Log.e("test", "Request failed: " + t.getMessage(), t); + } + }); + } + public class MyAdapter extends RecyclerView.Adapter { @NonNull @Override public MyViewHoler onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { - View view= LayoutInflater.from(System_messagesActivity.this).inflate(R.layout.item_system_messages,parent,false); - MyViewHoler myViewHoler=new MyViewHoler(view); - return myViewHoler; + View view = LayoutInflater.from(System_messagesActivity.this).inflate(R.layout.item_system_messages, parent, false); + return new MyViewHoler(view); } @Override @@ -56,11 +87,13 @@ public class System_messagesActivity extends AppCompatActivity { return system_messages.size(); } } + // ViewHolder public class MyViewHoler extends RecyclerView.ViewHolder { TextView text; + public MyViewHoler(@NonNull View itemView) { super(itemView); - text=itemView.findViewById(R.id.textView115); + text = itemView.findViewById(R.id.textView115); } } } \ No newline at end of file diff --git a/src/main/res/layout/activity_tool.xml b/src/main/res/layout/activity_tool.xml index 691daec..34c8188 100644 --- a/src/main/res/layout/activity_tool.xml +++ b/src/main/res/layout/activity_tool.xml @@ -672,7 +672,7 @@ android:id="@+id/txt_content" android:layout_width="match_parent" android:layout_height="140dp" - android:layout_marginTop="50dp" + android:layout_marginTop="84dp" android:ellipsize="end" android:paddingLeft="16dp" android:paddingRight="16dp" @@ -681,6 +681,7 @@ android:textColor="#BBBBBB" android:textSize="18dp" app:layout_constraintEnd_toEndOf="parent" + app:layout_constraintHorizontal_bias="0.0" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" />