科技政策查询系统app总结

MySQL导入policy表,利用android studio和idea开发。
idea代码
package com.example.one;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class OneApplication {

public static void main(String[] args) {
    SpringApplication.run(OneApplication.class, args);
}

}

package com.example.one;

import javax.persistence.*;
import java.util.Date;

@Entity
public class Policy {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String type;
private String category;
private String range;
private String document;
private String form;
private String organ;
private Date viadata;
private Date pubdata;
private Date perdata;
private String field;
private String theme;
private String keyword;
private String superior;
private String precursor;
private String succeed;
private String state;
@Lob
private String text;
private String pdf;
private String redundancy;
private String rank;
private String policykey;
private String newrank;
private String year;
private String newkey;
private String secondtheme;
private Integer allsum;

// Getters and Setters

public Long getId() {
    return id;
}

public void setId(Long id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getType() {
    return type;
}

public void setType(String type) {
    this.type = type;
}

public String getCategory() {
    return category;
}

public void setCategory(String category) {
    this.category = category;
}

public String getRange() {
    return range;
}

public void setRange(String range) {
    this.range = range;
}

public String getDocument() {
    return document;
}

public void setDocument(String document) {
    this.document = document;
}

public String getForm() {
    return form;
}

public void setForm(String form) {
    this.form = form;
}

public String getOrgan() {
    return organ;
}

public void setOrgan(String organ) {
    this.organ = organ;
}

public Date getViadata() {
    return viadata;
}

public void setViadata(Date viadata) {
    this.viadata = viadata;
}

public Date getPubdata() {
    return pubdata;
}

public void setPubdata(Date pubdata) {
    this.pubdata = pubdata;
}

public Date getPerdata() {
    return perdata;
}

public void setPerdata(Date perdata) {
    this.perdata = perdata;
}

public String getField() {
    return field;
}

public void setField(String field) {
    this.field = field;
}

public String getTheme() {
    return theme;
}

public void setTheme(String theme) {
    this.theme = theme;
}

public String getKeyword() {
    return keyword;
}

public void setKeyword(String keyword) {
    this.keyword = keyword;
}

public String getSuperior() {
    return superior;
}

public void setSuperior(String superior) {
    this.superior = superior;
}

public String getPrecursor() {
    return precursor;
}

public void setPrecursor(String precursor) {
    this.precursor = precursor;
}

public String getSucceed() {
    return succeed;
}

public void setSucceed(String succeed) {
    this.succeed = succeed;
}

public String getState() {
    return state;
}

public void setState(String state) {
    this.state = state;
}

public String getText() {
    return text;
}

public void setText(String text) {
    this.text = text;
}

public String getPdf() {
    return pdf;
}

public void setPdf(String pdf) {
    this.pdf = pdf;
}

public String getRedundancy() {
    return redundancy;
}

public void setRedundancy(String redundancy) {
    this.redundancy = redundancy;
}

public String getRank() {
    return rank;
}

public void setRank(String rank) {
    this.rank = rank;
}

public String getPolicykey() {
    return policykey;
}

public void setPolicykey(String policykey) {
    this.policykey = policykey;
}

public String getNewrank() {
    return newrank;
}

public void setNewrank(String newrank) {
    this.newrank = newrank;
}

public String getYear() {
    return year;
}

public void setYear(String year) {
    this.year = year;
}

public String getNewkey() {
    return newkey;
}

public void setNewkey(String newkey) {
    this.newkey = newkey;
}

public String getSecondtheme() {
    return secondtheme;
}

public void setSecondtheme(String secondtheme) {
    this.secondtheme = secondtheme;
}

public Integer getAllsum() {
    return allsum;
}

public void setAllsum(Integer allsum) {
    this.allsum = allsum;
}

}
package com.example.one;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/policies")
public class PolicyController {

@Autowired
private PolicyRepository policyRepository;

@GetMapping("/search")
public List<Policy> searchPolicies(@RequestParam String keyword) {
    return policyRepository.findByNameContaining(keyword);
}

@GetMapping("/{id}")
public Policy getPolicyById(@PathVariable Long id) {
    return policyRepository.findById(id).orElse(null);
}

}
package com.example.one;

import org.springframework.data.jpa.repository.JpaRepository;
import java.util.List;

public interface PolicyRepository extends JpaRepository<Policy, Long> {
List findByNameContaining(String keyword);
}
Android studio代码
androidmanifest.xml






            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".SearchActivity" /> <!-- 注册 SearchActivity -->
    <activity android:name=".PolicyDetailActivity" /> <!-- 注册详情页面 -->
</application>

package com.example.one;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
private Button buttonGoToSearch;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // 确保这里的 ID 和布局文件中的 ID 一致
    buttonGoToSearch = findViewById(R.id.buttonGoToSearch);
    buttonGoToSearch.setOnClickListener(v -> {
        // 跳转到查询页面
        Intent intent = new Intent(MainActivity.this, SearchActivity.class);
        startActivity(intent);
    });
}

}
package com.example.one;

import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Path;
import retrofit2.http.Query;
import java.util.List;

public interface ApiService {
@GET("api/policies/search")
Call<List> searchPolicies(@Query("keyword") String keyword);

@GET("api/policies/{id}")
Call<Policy> getPolicyById(@Path("id") Long id);

}
package com.example.one;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
private Button buttonGoToSearch;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // 确保这里的 ID 和布局文件中的 ID 一致
    buttonGoToSearch = findViewById(R.id.buttonGoToSearch);
    buttonGoToSearch.setOnClickListener(v -> {
        // 跳转到查询页面
        Intent intent = new Intent(MainActivity.this, SearchActivity.class);
        startActivity(intent);
    });
}

}
package com.example.one;

public class Policy {
private Long id;
private String name;
private String text;

// Getters and Setters
public Long getId() { return id; }
public void setId(Long id) { this.id = id; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public String getText() { return text; }
public void setText(String text) { this.text = text; }

}
package com.example.one;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;

public class PolicyAdapter extends RecyclerView.Adapter<PolicyAdapter.PolicyViewHolder> {
private List policyList;
private OnItemClickListener listener;

// 构造函数
public PolicyAdapter(List<Policy> policyList, OnItemClickListener listener) {
    this.policyList = policyList;
    this.listener = listener;
}

@NonNull
@Override
public PolicyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    // 加载列表项布局
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_policy, parent, false);
    return new PolicyViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull PolicyViewHolder holder, int position) {
    // 获取当前政策
    Policy policy = policyList.get(position);

    // 绑定数据到视图
    holder.textViewPolicyName.setText(policy.getName());
    // 设置点击事件
    holder.itemView.setOnClickListener(v -> {
        if (listener != null) {
            listener.onItemClick(policy);
        }
    });
}

@Override
public int getItemCount() {
    return policyList.size();
}

// ViewHolder 类
static class PolicyViewHolder extends RecyclerView.ViewHolder {
    TextView textViewPolicyName;
    TextView textViewPolicyDate;

    public PolicyViewHolder(@NonNull View itemView) {
        super(itemView);
        // 初始化视图
        textViewPolicyName = itemView.findViewById(R.id.textViewPolicyName);
    }
}

// 点击事件接口
public interface OnItemClickListener {
    void onItemClick(Policy policy);
}

}
package com.example.one;

import android.os.Bundle;
import android.webkit.WebView;
import androidx.appcompat.app.AppCompatActivity;

public class PolicyDetailActivity extends AppCompatActivity {
private WebView webViewPolicyDetail;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_policy_detail);

    webViewPolicyDetail = findViewById(R.id.webViewPolicyDetail);

    // 获取传递过来的政策内容
    String policyText = getIntent().getStringExtra("policy_text");
    if (policyText != null) {
        // 加载 HTML 内容
        webViewPolicyDetail.loadData(policyText, "text/html; charset=UTF-8", null);
    } else {
        webViewPolicyDetail.loadData("未找到政策详情", "text/html; charset=UTF-8", null);
    }
}

}
package com.example.one;

import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class RetrofitClient {
private static final String BASE_URL = "http://192.168.215.13:8080/";
private static Retrofit retrofit = null;

public static ApiService getApiService() {
    if (retrofit == null) {
        retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }
    return retrofit.create(ApiService.class);
}

}
package com.example.one;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import java.util.List;

public class SearchActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private PolicyAdapter adapter;
private EditText editTextKeyword;
private Button buttonSearch;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_search);

    editTextKeyword = findViewById(R.id.editTextKeyword);
    buttonSearch = findViewById(R.id.buttonSearch);
    recyclerView = findViewById(R.id.recyclerView);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));

    buttonSearch.setOnClickListener(v -> {
        String keyword = editTextKeyword.getText().toString().trim();
        if (!keyword.isEmpty()) {
            searchPolicies(keyword);
        } else {
            Toast.makeText(SearchActivity.this, "请输入关键字", Toast.LENGTH_SHORT).show();
        }
    });
}

private void searchPolicies(String keyword) {
    ApiService apiService = RetrofitClient.getApiService();
    Call<List<Policy>> call = apiService.searchPolicies(keyword);
    call.enqueue(new Callback<List<Policy>>() {
        @Override
        public void onResponse(Call<List<Policy>> call, Response<List<Policy>> response) {
            if (response.isSuccessful()) {
                List<Policy> policies = response.body();
                if (policies != null && !policies.isEmpty()) {
                    Log.d("SearchActivity", "Loaded policies: " + policies.size()); // 打印数据量
                    adapter = new PolicyAdapter(policies, policy -> {
                        Intent intent = new Intent(SearchActivity.this, PolicyDetailActivity.class);
                        intent.putExtra("policy_text", policy.getText());
                        startActivity(intent);
                    });
                    recyclerView.setAdapter(adapter);
                } else {
                    Toast.makeText(SearchActivity.this, "未找到相关数据", Toast.LENGTH_SHORT).show();
                }
            } else {
                Toast.makeText(SearchActivity.this, "请求失败: " + response.code(), Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onFailure(Call<List<Policy>> call, Throwable t) {
            Toast.makeText(SearchActivity.this, "网络错误: " + t.getMessage(), Toast.LENGTH_SHORT).show();
        }
    });
}

}
activity_main.xml

<Button
    android:id="@+id/buttonGoToSearch"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="跳转到查询页面" />

activity_policy_detail.xml

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

activity_search.xml

<!-- 标题栏 -->
<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="科技政策查询系统"
    android:textSize="24sp"
    android:textColor="@color/black"
    android:textStyle="bold"
    android:gravity="center"
    android:padding="16dp" />

<!-- 搜索框 -->
<EditText
    android:id="@+id/editTextKeyword"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="请输入关键字"
    android:padding="12dp"
    android:textSize="16sp" />
<!-- 查询按钮 -->
<Button
    android:id="@+id/buttonSearch"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="查询"
    android:backgroundTint="@color/purple_500"
    android:textColor="@color/white"
    android:textSize="18sp"
    android:layout_marginTop="16dp"
    android:layout_marginBottom="16dp" />

<!-- 查询结果列表 -->
<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/recyclerView"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:padding="8dp" />

item_policy.xml

<TextView
    android:id="@+id/textViewPolicyName"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="政策名称"
    android:textSize="18sp"
    android:textColor="@color/black"
    android:textStyle="bold" />

network_security_config.xml

192.168.215.13
posted @ 2025-04-07 19:11  霸王鸡  阅读(35)  评论(0)    收藏  举报