I am a senior student in college. The purpose of writing this article is to record a small demo I made by using the Wenzhi API of Goose Field and the intelligent robot API of Ali Cloud
The main things used are:
- Retrofit2.0
- Tencent Cloud Wenzhi emotion value analysis API
- Ali Cloud intelligent robot API
- Gson parsing
- recyclerview
First, sample images:
(Main interface _ONE)
(Main interface _two)
Analysis main interface:
In this main interface, it is mainly RecyclerView, ImageView for two head pictures, one EditText and one send button designed by TextView, which simulate wechat message sending (there is no judgment on message type here, so it is simply to send a data and get a data. There is no line chat that can send multiple messages, because one message is returned for each message request.)
<? xml version="1.0" encoding="utf-8"? > <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.luobotie.kingshun.mychat.MainActivity">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<android.support.v7.widget.RecyclerView
android:id="@+id/rv_chat"
android:layout_width="match_parent"
android:layout_height="match_parent"/ > <! --> <View Android :id="@+id/maskForETFocus"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent"
android:onClick="clickToClearFocus"/> </FrameLayout> <! <LinearLayout Android :elevation="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:orientation="horizontal"
android:padding="5dp">
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:id="@+id/iv_send"
android:layout_weight="0.5"
android:src="@android:drawable/ic_menu_send" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="5"
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="vertical"
android:padding="3dp">
<EditText
android:id="@+id/et_message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@null"
android:hint="Write your words from here."
android:maxLines="3"
android:padding="5dp"
android:textAppearance="? android:attr/textAppearanceLargeInverse"
android:textCursorDrawable="@drawable/color_cursor"
android:textSize="16sp" />
<View
android:layout_marginTop="3dp"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/colorGray" />
</LinearLayout>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:background="@drawable/btn_selector"
android:gravity="center_horizontal|clip_horizontal"
android:onClick="sendMsg"
android:padding="8dp"
android:text="Send"
android:textColor="#fff"
android:textSize="13sp" />
</LinearLayout>
</LinearLayout>Copy the code
Here’s a framlayout inside put two components, one is recyclerview, another is a transparent cover layer @ android: this is used to color/transparent edittext loses focus used to hide the keyboard to use, This can be used to set the presence or absence of a component by controlling its visibility, and to set a listener to the EditText to determine changes in focus state
@Override
public void onFocusChange(View view, boolean b) {
if (b) {
Log.d(TAG, "OnFocusChange: Focused");
mMask.setVisibility(View.VISIBLE);
} else {
Log.d(TAG, "OnFocusChange: Unfocus"); InputMethodManager imm = (InputMethodManager) getSystemService(context.input_method_service); InputMethodManager imm = (InputMethodManager) getSystemService(context.input_method_service); imm.hideSoftInputFromWindow(mContent.getWindowToken(), IMM_HIDE_FLAGS); // Cancel mmask.setvisibility (view.gone); }}Copy the code
When focusing, you can write to the input box and make the mask visible (the function of the mask is to control the input box focus).
The enforced hiding of the keyboard has been posted in the code. Controls for the soft keyboard are also made in manifest
<activity
android:name=".MainActivity"
android:windowSoftInputMode="stateUnspecified|adjustResize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>Copy the code
android:windowSoftInputMode="stateUnspecified|adjustResize"This sentence is necessary for specific detailsCopy the code
Here is the url: blog.csdn.net/xww810319/a…
Here’s how to request API data using Retrofit:
Define an interface
public interface GetChatRequest {
@GET("/iqa/query?")
Call<chatBean> getChat(@Query("question") String question, @Header("Authorization") String appcode);
}
Copy the code
Define a JavaBean (using the GsonFormat plug-in)
public class chatBean {
/**
* status : 0
* msg : ok
* result : {"type":"Standard response"."content":Hangzhou today 10℃~21℃ fine north wind level 3-4 to the northeast wind breeze r\n suggested a thin coat, carsweater jeans pants clothing. The elderly and infirm should add clothes appropriately, such as jackets and thin sweaters."."relquestion":"Checking the weather [so?] [weather region name | | province famous city nickname] / weather region name? "}
*/
private String status;
private String msg;
private ResultBean result;
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public ResultBean getResult() {
return result;
}
public void setResult(ResultBean result) {
this.result = result;
}
public static class ResultBean {
/**
* typeHangzhou today 10℃~21℃ fine north wind level 3-4 to the northeast breeze suggested a thin coat, carden-jeans pants and other clothing. The elderly and infirm should add clothes appropriately, such as jackets and thin sweaters. * relquestion: query the weather [so?] [weather region name | | province famous city nickname] / weather region name? */ private Stringtype;
private String content;
private String relquestion;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getRelquestion() {
return relquestion;
}
public void setRelquestion(String relquestion) { this.relquestion = relquestion; }}}Copy the code
This is mainly the information obtained using the getContent() method followed by the request
GsonConverterFactory is used to return Gson data, String returnStr = response.body().getresult ().getContent(); Is the result returned, but takes arguments
- Question: The question to submit the request
- APPCODE, the APPCODE created in Ali Cloud
Address: market.aliyun.com/products/57… Here is free artificial robot!!
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
GetChatRequest request = retrofit.create(GetChatRequest.class);
Call<chatBean> call = request.getChat(question, APPCODE);
call.enqueue(new Callback<chatBean>() {
@Override
public void onResponse(Call<chatBean> call, Response<chatBean> response) {
String returnStr = response.body().getResult().getContent(); // Exchange data with Adapter swapDatas(returnStr);
}
@Override
public void onFailure(Call<chatBean> call, Throwable t) {
Toast.makeText(MainActivity.this, "Robot Crash.", Toast.LENGTH_SHORT).show(); }});Copy the code
The requested data is displayed on the left or right side of the adapter simply by taking the remainder of 2
Of course for the normal display of data does not shift (because of recycleView reuse) in onBindView: must set the visibility of the component for each required component (parent component on the line) Settings of course there are set Tag method !!!!! Knock on the blackboard !!!!
public void onBindViewHolder(MyViewHolder holder, int position) {
Log.d(TAG, "onBindViewHolder: " + position);
if(the position % 2 = = 1) {/ / the left message holder. MLeftContainer. SetVisibility (the VISIBLE). holder.mRightContainer.setVisibility(View.GONE); holder.mLeftMsg.setText(mDatas.get(position)); }else{ holder.mRightContainer.setVisibility(View.VISIBLE); holder.mLeftContainer.setVisibility(View.GONE); holder.mRightMsg.setText(mDatas.get(position)); }}Copy the code
This is the end of our robot conversation!!
Emotional value analysis:
The above first
Yes, the click event for item !!!! Click to pop up the prompt box. Click OK to get the data through the Wenzhi API of Tencent Cloud and display it in a new Activity. Then wenzhi link is here: nlp.qq.com/
Description of the wenzhi interface
Domain name: WENzhi.api.qcloud.com Interface name: TextSentiment in public opinion monitoring, topic supervision, word-of-mouth analysis and other business analysis has very important application value. (Reference Tencent Cloud)
Here’s an example:
https://wenzhi.api.qcloud.com/v2/index.php? Action=TextSentiment &Nonce=345122 &Region=sz &SecretId=AKIDz8krbsJ5yKBZQpn74WFkmLPx3gnPhESA &Timestamp=1408704141 &Signature=HgIYOPcx5lN6gz8JsCFBNAWp2oQ & Content = Dual gigabit server is good, just a little bit of memoryCopy the code
There is a big hole here is HmacSHA256 encryption algorithm how to do please see the official documentation
Tencent here to the majority of developers a general JAR package, please see the SDK documentation with the JAR package here can be easily involved
Github portal for the big guys!! Forget big guys light abuse !!!!