本人是大四在校生,写这篇文章的目的就是主要记录下我在使用鹅场的文智API以及阿里云开放的智能机器人api所作的一个小demophp
(主界面_one)html
(主界面_two)java
在这个主界面中,主要是RecyclerView,两个头像用的ImageView,一个EditText,一个由TextView设计的发送按钮,模拟微信的消息发送(这里没有对消息种类进行判断,因此只是简单的发送一条数据,得到一条数据,尚未行聊天工具那样能够发送多条数据,由于每个信息请求都会有一条信息返回就没有作信息的左右判断 )android
<?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" />
<!--蒙层,用于edittext的聚焦以及失焦-->
<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="从这里写入您的话"
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="发送"
android:textColor="#fff"
android:textSize="13sp" />
</LinearLayout>
</LinearLayout>复制代码
这里有一个framlayout里面放了两个组件,一个是recyclerview,另外一个是一个透明的蒙层@android:color/transparent 这个是用来给edittext失去焦点用来隐藏键盘用的,这个能够用于经过控制组件的可见性来设置它是否存在,给edittext设置一个监听器用来判断聚焦状态的改变git
@Override
public void onFocusChange(View view, boolean b) {
if (b) {
Log.d(TAG, "onFocusChange: 已聚焦");
mMask.setVisibility(View.VISIBLE);
} else {
Log.d(TAG, "onFocusChange: 取消聚焦");
//键盘的强制隐藏
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(mContent.getWindowToken(), IMM_HIDE_FLAGS);
//将mask取消存在
mMask.setVisibility(View.GONE);
}
}复制代码
聚焦的时候,能够给输入框写字,而且给蒙层设置可见(蒙层的做用就是控制输入框聚焦),github
键盘的强制隐藏在代码中已经贴出。在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>复制代码
android:windowSoftInputMode="stateUnspecified|adjustResize"这句话是有必要的 具体详见复制代码
这里是网址:blog.csdn.net/xww810319/a…api
定义一个接口bash
public interface GetChatRequest {
@GET("/iqa/query?")
Call<chatBean> getChat(@Query("question") String question, @Header("Authorization") String appcode);
}
复制代码
定义一个JavaBean(用GsonFormat插件)服务器
public class chatBean {
/**
* status : 0
* msg : ok
* result : {"type":"标准回复","content":"杭州今天10℃~21℃ 晴 北风3-4 级转东北风微风\r\n建议着薄外套、开衫牛仔衫裤等服装。年老体弱者应适当添加衣物,宜着夹克衫、薄毛衣等。","relquestion":"查询天气[那么?][天气地区名|省名|城市别称][天气地区名?]"}
*/
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 {
/**
* type : 标准回复
* content : 杭州今天10℃~21℃ 晴 北风3-4 级转东北风微风
建议着薄外套、开衫牛仔衫裤等服装。年老体弱者应适当添加衣物,宜着夹克衫、薄毛衣等。
* relquestion : 查询天气[那么?][天气地区名|省名|城市别称][天气地区名?]
*/
private String type;
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;
}
}
}复制代码
这里主要是利用getContent()方法获取的信息接下来就是请求
很简单的Retrofit的使用 ,这里使用的GsonConverterFactory
是用来返回Gson数据的, String returnStr = response.body().getResult().getContent();就是返回的结果,可是是须要参数的
地址:market.aliyun.com/products/57…这里是免费的人工机器人啊!!!
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();
//与Adapter交换数据
swapDatas(returnStr);
}
@Override
public void onFailure(Call<chatBean> call, Throwable t) {
Toast.makeText(MainActivity.this, "机器人崩溃", Toast.LENGTH_SHORT).show();
}
});复制代码
请求的数据就在adapter中简单的经过对2取余数来判断是显示在左侧仍是右侧
固然为了数据的正常显示不移位(由于recycleview的复用)在onBindView中:必须对组件设置可见性 对每个须要的组件(父组件就行)设置 固然还有设置Tag的方法!!!!!敲黑板!!!!
public void onBindViewHolder(MyViewHolder holder, int position) {
Log.d(TAG, "onBindViewHolder: " + position);
if (position % 2 == 1) {
//左边消息
holder.mLeftContainer.setVisibility(View.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));
}
}复制代码
到此 咱们的机器人对话就完成了!!!
先上图
没错就是item的点击事件!!!!经过点击来弹出提示框 点击肯定后经过腾讯云的文智API来获取数据并显示在一个新的Activity中那么文智的连接在这里:nlp.qq.com/
域名:wenzhi.api.qcloud.com
接口名: TextSentiment
在舆情监控、话题监督、口碑分析等商业分析领域有很是重要的应用价值。(引用腾讯云)
下面是实例:
https://wenzhi.api.qcloud.com/v2/index.php?
Action=TextSentiment
&Nonce=345122
&Region=sz
&SecretId=AKIDz8krbsJ5yKBZQpn74WFkmLPx3gnPhESA
&Timestamp=1408704141
&Signature=HgIYOPcx5lN6gz8JsCFBNAWp2oQ
&content=双万兆服务器就是好,只是内存小点复制代码
这里有一个很大的坑就是HmacSHA256的加密算法如何去作请看官方文档
这里腾讯给广大开发者一个通用的jar包具体请见SDK文档用这里的jar包彻底能够轻松一件介入
到此微小Demo作完了给大佬们放上Github传送门!!!忘大佬们轻虐!!!!