生活随笔
收集整理的這篇文章主要介紹了
java阿里云文本反垃圾sdk调用详解
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
java阿里云文本反垃圾sdk調用
簡介:java阿里云文本反垃圾是結合行為、內容,采用多維度、多模型、多檢測手段,識別文本中的垃圾內容,規避色情、廣告、灌水、渉政、辱罵等內容風險。文本反垃圾只有同步檢測接口,您可以在一次請求中檢測一條或者多條文本。
sdk調用流程:注冊阿里云賬號→創建RAM子賬號→子賬號授權→復制子賬號的AccessKey ID&Access Key Secret→sdk&api調用
阿里云RAM子賬號創建、授權、api文檔詳情查看
阿里云文本反垃圾接口詳情及請求、返回參數詳情查看
注意:調用阿里云接口的時候最容易出錯的倆個地方1.AccessKey ID&Access Key Secret因為阿里云是主張接口調用都是給子賬號權限然后用子賬號的AccessKey ID&Access Key Secret來調用接口,所有我們復制這倆個參數的時候很容易復制成我們主賬號的這倆參數去了,切記一定要復制子賬號的這倆參數!!2.權限添加很多時候我們所有步驟都作對了就是連不通的原因,就是子賬號的權限沒給,如我現在要調文本反垃圾的接口但是我沒給子賬號這個權限是無論如何都調不通的切記!!
1.阿里云不僅提供了api還提供了sdk,有了sdk我們就方便多了,阿里云幫我們封裝了所有的方法我們只需要復制粘貼就能用這些接口了,但是前提得注意我上面說的一些注意事項切記嗷java阿里云sdk地址
2.java文本反垃圾(同步)代碼如下
@org.junit
.Test
public void aliyunTextCheck() throws Exception
{IClientProfile profile
= DefaultProfile
.getProfile("你的regionId", "你的accessKeyId", "你的accessKeySecret");IAcsClient client
= new DefaultAcsClient(profile
);TextScanRequest textScanRequest
= new TextScanRequest();textScanRequest
.setAcceptFormat(FormatType
.JSON
); textScanRequest
.setHttpContentType(FormatType
.JSON
);textScanRequest
.setMethod(com
.aliyuncs
.http
.MethodType
.POST
); textScanRequest
.setEncoding("UTF-8");textScanRequest
.setRegionId("cn-shanghai");List
<Map
<String, Object>> tasks
= new ArrayList<Map
<String, Object>>();Map
<String, Object> task1
= new LinkedHashMap<String, Object>();task1
.put("dataId", UUID
.randomUUID().toString());task1
.put("content", "test content");tasks
.add(task1
);JSONObject data
= new JSONObject();data
.put("scenes", Arrays
.asList("antispam"));data
.put("tasks", tasks
);System
.out
.println(JSON
.toJSONString(data
, true));textScanRequest
.setHttpContent(data
.toJSONString().getBytes("UTF-8"), "UTF-8", FormatType
.JSON
);textScanRequest
.setConnectTimeout(3000);textScanRequest
.setReadTimeout(6000);try {HttpResponse httpResponse
= client
.doAction(textScanRequest
);if (httpResponse
.isSuccess()) {JSONObject scrResponse
= JSON
.parseObject(new String(httpResponse
.getHttpContent(), "UTF-8"));System
.out
.println(JSON
.toJSONString(scrResponse
, true));if (200 == scrResponse
.getInteger("code")) {JSONArray taskResults
= scrResponse
.getJSONArray("data");for (Object taskResult
: taskResults
) {if (200 == ((JSONObject
) taskResult
).getInteger("code")) {JSONArray sceneResults
= ((JSONObject
) taskResult
).getJSONArray("results");for (Object sceneResult
: sceneResults
) {String scene
= ((JSONObject
) sceneResult
).getString("scene");String suggestion
= ((JSONObject
) sceneResult
).getString("suggestion");System
.out
.println("scene = [" + scene
+ "]");System
.out
.println("suggestion = [" + suggestion
+ "]");}} else {System
.out
.println("task process fail:" + ((JSONObject
) taskResult
).getInteger("code"));}}} else {System
.out
.println("detect not success. code:" + scrResponse
.getInteger("code"));}} else {System
.out
.println("response not success. status:" + httpResponse
.getStatus());}} catch (ServerException e
) {e
.printStackTrace();} catch (ClientException e
) {e
.printStackTrace();} catch (Exception e
) {e
.printStackTrace();}}
3.檢測結果如下
{"scenes":["antispam"],"tasks":[{"dataId":"1baf084f-b052-4cae-8db9-4cc374eb1969","content":"test content"}]
}
{"msg":"OK","code":200,"data":[{"msg":"OK","code":200,"dataId":"1baf084f-b052-4cae-8db9-4cc374eb1969","results":[{"rate":99.91,"suggestion":"pass","label":"normal","scene":"antispam"}],"content":"test content","taskId":"txt7ezmSKsAHUh6ZNPADjNAGj-1r9Am9"}],"requestId":"F7CEB479-F5F3-45A9-BA6A-E14BCF82A4BA"
}
scene
= [antispam
]
suggestion
= [pass
]
總結
以上是生活随笔為你收集整理的java阿里云文本反垃圾sdk调用详解的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。