Android Studio - Implementation of Sensitive Word Retrieval

The effect achieved:
it is to search for sensitive words before storing data in the database. If there are sensitive words, it will prompt that there are sensitive words, and then the submission fails and cannot be stored in the database.

API used:
Chinese and English sensitive word filtering

Idea: first encapsulate the method of accessing the API, then call this method where needed, post the content to the server for filtering, and then return a data in json format, analyze the data in json format, and obtain the data data inside ( That is, the filtered content, the website will replace the sensitive word with *), at this time, you can directly store the data data in the database, so that the read out is the content with the sensitive word changed to *, or you can compare the data with the original content Compare, if there is no change, it means that there is no sensitive word, if there is a change, it means that there is a sensitive word, and it will prompt that there is a sensitive word that has failed to publish.

Specific process:
1. Import the dependencies required for json parsing:

    implementation 'com.alibaba:fastjson:1.1.54.android'

2. Encapsulate the method: create a TextFilter class:

ps: The private key in the code can enter the above website. After clicking the test API, click Select Token to obtain your own private key.
The function parameter content is the obtained user input content.

package com.example.academymanageapp.adapter;

import com.alibaba.fastjson.JSONObject;

import java.io.IOException;

import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class TextFilter {

    private String data;//调用该函数,返回data数据
    public String textFilterTest(String content){
        OkHttpClient client = new OkHttpClient().newBuilder().build();
        MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
        String string = "text="+content;
        RequestBody body = RequestBody.create(mediaType, string);
        Request request = new Request.Builder()
                .url("https://eolink.o.apispace.com/text-filters/api/v1/forward/text_filter/")
                .method("POST",body)
                .addHeader("X-APISpace-Token","7iv06pckr9yrbl3nqyrbcau9umy05vts")
                .addHeader("Authorization-Type","apikey")
                .addHeader("Content-Type","application/x-www-form-urlencoded")
                .build();

        Response response;
        try {
            response = client.newCall(request).execute();
            //获取data数据
            String result = response.body().string();
            JSONObject jsonObject = JSONObject.parseObject(result);
            data = jsonObject.getString("data");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return data;
    }
}

ps: There are two points to note in this step:

  • RequestBody body = RequestBody.create(mediaType, string) must add "text=" before the content, otherwise the format is wrong.
  • response.body() can only be used once.

3. Just call TextFilter's textFilterTest where needed.

Guess you like

Origin blog.csdn.net/zzzzzwbetter/article/details/130049230