日报2025424

今日实现个人作业第二阶段

package com.example.dailyreport.ui.blog;

import android.app.ProgressDialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.example.dailyreport.R;
import com.example.dailyreport.adapter.BlogAdapter;
import com.example.dailyreport.model.Blog;
import com.example.dailyreport.network.ApiResponse;
import com.example.dailyreport.network.RetrofitClient;

import java.util.ArrayList;
import java.util.List;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ViewBlogsActivity extends AppCompatActivity {
    private RecyclerView recyclerView;
    private BlogAdapter adapter;
    private String userId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_blogs);
        
        // 获取当前用户ID
        userId = getIntent().getStringExtra("userId");
        if(userId == null) {
            Toast.makeText(this, "用户ID获取失败", Toast.LENGTH_SHORT).show();
            finish();
            return;
        }

        // 在onCreate方法中添加以下代码,显示学生姓名
        String studentName = getIntent().getStringExtra("studentName");
        if (studentName != null && !studentName.isEmpty()) {
            setTitle(studentName + "的博客列表");
        }
        recyclerView = findViewById(R.id.rvBlogs);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        
        loadBlogs();
        // 在onCreate方法中添加以下代码,初始化适配器
        // 在onCreate方法中初始化适配器时添加点击监听
        adapter = new BlogAdapter(new ArrayList<>(), blog -> {
            // 处理博客点击事件
            if (blog != null && blog.getBlogAddress() != null) {
                String url = blog.getBlogAddress();
                // 添加URL格式验证
                if (!url.startsWith("http://") && !url.startsWith("https://")) {
                    url = "http://" + url;
                }
                
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setData(Uri.parse(url));
                startActivity(intent);
            }
        });
        recyclerView.setAdapter(adapter);
    }

    private void loadBlogs() {
        ProgressDialog progressDialog = new ProgressDialog(this);
        progressDialog.setMessage("正在加载博客列表...");
        progressDialog.show();
        
        RetrofitClient.getInstance().getApiService()
            .selectByUserId(userId)
            .enqueue(new Callback<ApiResponse<List<Blog>>>() {
                @Override
                public void onResponse(Call<ApiResponse<List<Blog>>> call, Response<ApiResponse<List<Blog>>> response) {
                    progressDialog.dismiss();
                    try {
                        if (response.isSuccessful() && response.body() != null) {
                            ApiResponse<List<Blog>> apiResponse = response.body();
                            List<Blog> blogs = apiResponse.getData();
                            
                            if (blogs == null || blogs.isEmpty()) {
                                Toast.makeText(ViewBlogsActivity.this, "没有找到博客", Toast.LENGTH_SHORT).show();
                            } else {
                                // 使用新添加的方法更新适配器
                                adapter.setBlogList(blogs);
                            }
                        } else {
                            Toast.makeText(ViewBlogsActivity.this, "获取博客失败", Toast.LENGTH_SHORT).show();
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                        Toast.makeText(ViewBlogsActivity.this, "处理响应异常: " + e.getMessage(), Toast.LENGTH_SHORT).show();
                    }
                }
                
                @Override
                public void onFailure(Call<ApiResponse<List<Blog>>> call, Throwable t) {
                    progressDialog.dismiss();
                    Toast.makeText(ViewBlogsActivity.this, 
                        "网络错误: " + t.getMessage(), Toast.LENGTH_SHORT).show();
                }
            });
    }
}
package com.example.dailyreport.ui.blog;

import android.os.Bundle;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.example.dailyreport.R;

public class WebViewActivity extends AppCompatActivity {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_webview);
        
        String url = getIntent().getStringExtra("url");
        if (url == null || url.isEmpty()) {
            Toast.makeText(this, "无效的URL", Toast.LENGTH_SHORT).show();
            finish();
            return;
        }
        
        // 确保URL格式正确
        if (!url.startsWith("http://") && !url.startsWith("https://")) {
            url = "https://" + url;
        }
        
        WebView webView = findViewById(R.id.webView);
        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setDomStorageEnabled(true);
        webSettings.setLoadsImagesAutomatically(true);
        webSettings.setCacheMode(WebSettings.LOAD_DEFAULT);
        webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        
        // 允许加载任何来源的内容
        webSettings.setAllowContentAccess(true);
        webSettings.setAllowFileAccess(true);
        webSettings.setAllowFileAccessFromFileURLs(true);
        webSettings.setAllowUniversalAccessFromFileURLs(true);
        
        final String finalUrl = url;
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
                Toast.makeText(WebViewActivity.this, 
                    "加载失败,请检查网络连接或URL是否正确", Toast.LENGTH_LONG).show();
            }
            
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                return false; // 允许WebView处理URL加载
            }
            
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                // 页面加载完成
                Toast.makeText(WebViewActivity.this, "页面加载完成", Toast.LENGTH_SHORT).show();
            }
        });
        
        try {
            // 打印URL,便于调试
            System.out.println("正在加载URL: " + finalUrl);
            webView.loadUrl(finalUrl);
        } catch (Exception e) {
            Toast.makeText(this, "加载URL时出错: " + e.getMessage(), Toast.LENGTH_LONG).show();
            e.printStackTrace();
        }
    }
    
    @Override
    public void onBackPressed() {
        WebView webView = findViewById(R.id.webView);
        if (webView.canGoBack()) {
            webView.goBack();
        } else {
            super.onBackPressed();
        }
    }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#F5F5F5">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="学生列表"
        android:textSize="20sp"
        android:textStyle="bold"
        android:gravity="center"
        android:padding="16dp"
        android:background="#FFFFFF"
        android:elevation="4dp"/>

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rvStudents"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="8dp"
        android:clipToPadding="false"/>

</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rvBlogs"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <WebView
        android:id="@+id/webView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>
posted @ 2025-04-24 22:30  花落水无痕  阅读(13)  评论(0)    收藏  举报