小i聊天机器人自定义在android 上的使用:
OnAnswerListener listener = new OnAnswerListener(){
@Override
public void result(String result) {
// TODO Auto-generated method stub
}
@Override
public void error(String error) {
// TODO Auto-generated method stub
}
};
xiaoiRobot xiaoi = new xiaoiRobot(listener);
xiaoi.ask(question);
以下为xiaoiRobot的源码:
需要用到org.apache.commons.codec, org.apache.commons.lang3, org.apache.commons.httpclient 的包, 下面org.apache.common.codec是我自己打包的,为了防止和android自带同名包冲突, 修改了包名, 请注意。
import java.util.Random;
import org.apache.common.codec.binary.Hex;
import org.apache.common.codec.digest.DigestUtils;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.commons.lang3.StringUtils;
import android.os.AsyncTask;
public class xiaoiRobot {
private static final String url = "http://nlp.xiaoi.com/ask.do";
private static final String XIAOI_APPKEY = "GNO97fnOa***";// xiaoi的appkey
private static final String XIAOI_SECRETKEY = "k0Q5QiXl1nT8dt5k****";//xiaoi的SECRETKEY"
private static String nonce;
private static String sign;
private static String heads;
private String question;
private OnAnswerListener mOnAnswerListener;
public xiaoiRobot(OnAnswerListener l)
{
heads = getSignKey();
mOnAnswerListener = l;
}
/*
* 1. 用SHA1将AppKey、Realm(固定值:xiaoi.com)以及Secret值按照“AppKey:Realm:Secret”的格式进行加密。
* 2. 用SHA1将Method(值大写,如:POST)和URL按照“Method:URL”的格式进行加密。
* 3. 用SHA1将第一二步获取的值和40位随机数按照“HA1:nonce:HA2”的格式进行加密,得到签名值。
* 4. 将AppKey、第三步中的40位随机数以及签名值按照app_key="xxx",nonce="xxx",signature="xxx"组装成字符串。
* 5. 在请求头中添加Key为X-Auth的键,键值为步骤4中获得的字符串。
*/
private static String getSignKey()
{
String realm = "xiaoi.com";
String method = "POST";
String uri = "/ask.do";
byte[] b = new byte[20];
new Random().nextBytes(b);
nonce = new String(Hex.encodeHex(b));
String HA1 = DigestUtils.shaHex(StringUtils.join(new String[] { XIAOI_APPKEY, realm, Config.XIAOI_SECRETKEY }, ":"));
String HA2 = DigestUtils.shaHex(StringUtils.join(new String[] { method, uri }, ":"));
sign = DigestUtils.shaHex(StringUtils.join(new String[] { HA1, nonce, HA2 }, ":"));
String heads = "app_key=\""+ XIAOI_APPKEY +"\", nonce=\"" + nonce + "\", signature=\"" + sign + "\"";
return heads;
}
public interface OnAnswerListener {
void result(String result);
void error(String error);
}
public void SetOnAnswerListener(OnAnswerListener l)
{
mOnAnswerListener = l;
}
public void ask(String question)
{
this.question = question;
new AskQuestionTask().execute(0);
}
private class AskQuestionTask extends AsyncTask<Integer, String, String> {
int re_code;
@Override
protected String doInBackground(Integer... params) {
try {
HttpClient hc = new HttpClient();
PostMethod pm = new PostMethod(url);
pm.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,
"utf-8");
pm.addRequestHeader("X-Auth", heads);
pm.setParameter("platform", "custom");
pm.setParameter("type", "0");//type 响应类型(0:普通、1:高级) 例如:"1"
pm.setParameter("userId", "user0001");//userId 用户id,用户和会话判断依据 例如:"user0001"
pm.setParameter("question", question);//question 问题内容 例如:"您好!"
re_code = hc.executeMethod(pm);
if (re_code == 200)
{
String str = pm.getResponseBodyAsString();
return str;
}
else
{
if(mOnAnswerListener != null)
mOnAnswerListener.error(re_code+"");
}
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
}
return null;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
if (result != null) {
if(mOnAnswerListener != null)
mOnAnswerListener.result(result);
}
}
}
}