码迷,mamicode.com
首页 > 移动开发 > 详细

Android开发-图灵聊天机器人接口引用

时间:2014-11-28 18:12:56      阅读:270      评论:0      收藏:0      [点我收藏+]

标签:des   android   style   blog   http   io   ar   color   os   

  转载注明出处: http://www.cnblogs.com/frank-zouxu/p/4121601.html  

  在前几日,偶然看到新闻,图灵机器人向开发者提供了API,API地址为:http://www.tuling123.com/openapi/,因为这个API可以定制自己的聊天机器人,这在我看来,是个很有意思的事情,于是我就试着在Android应用中进行了API测试,效果图如下(图1):

bubuko.com,布布扣

    图1

 

主要代码如下:

public class MainActivity extends Activity implements OnClickListener {

    private String requesturl;
    private ListView lv;
    private EditText et_input;
    private ChatListAdapter chat_adp;
    private Context ctx;

    private InputMethodManager imm;
    private Button et_sendMsg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ctx = this;
        imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        setContentView(R.layout.activity_main);
        initView();
    }
    private void initView() {
        lv = (ListView) findViewById(R.id.lv_conmunicate);
        et_input = (EditText) findViewById(R.id.et_input);
        et_input.setOnClickListener(this);
        et_sendMsg = (Button) findViewById(R.id.bt_sendMsg);
        et_sendMsg.setOnClickListener(this);
        chat_adp = new ChatListAdapter(ctx);
        lv.setAdapter(chat_adp);
    }
@Override
public void onClick(View v) { switch (v.getId()) { case R.id.bt_sendMsg: new AsyReq().execute(et_input.getText().toString().trim()); break; } } private class AsyReq extends AsyncTask<String, Integer, String> { private String result; private DialogShowStyle dialog; @Override protected void onPreExecute() { // TODO Auto-generated method stub // 隐藏软键盘 if (imm.isActive()) { ((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE)) .hideSoftInputFromWindow(et_input.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); } int netType = new NetWorkUtils(MainActivity.this).getNetType(); switch (netType) { case Configs.NONET: // handler.sendEmptyMessage(Configs.NONET); Toast.makeText(MainActivity.this, "请检查网络连接", Toast.LENGTH_LONG) .show(); // 取消任务的执行 this.cancel(true); break; case Configs.SLOWNET: // handler.sendEmptyMessage(Configs.SLOWNET); dialog = new DialogShowStyle(MainActivity.this, "小图灵正在深思..."); dialog.dialogShow(); case Configs.WIFI: chat_adp.add("我:" + et_input.getText().toString()); break; } } @Override protected String doInBackground(String... params) { try { // 防止get请求中出现中文乱码 String INFO = URLEncoder.encode(params[0], "utf-8"); requesturl = Configs.API_URL + Configs.APIKEY + "&info=" + INFO; HttpGet request = new HttpGet(requesturl); HttpResponse response; response = new DefaultHttpClient().execute(request); if (response.getStatusLine().getStatusCode() == 200) { result = EntityUtils.toString(response.getEntity()); result = JsonUtils.analysisInfo(result); } } catch (Exception e) { e.printStackTrace(); } return result; } @Override protected void onPostExecute(String result) { if (dialog != null) { dialog.dialogDismiss(); dialog = null; } chat_adp.add("图灵:" + result); et_input.setText(""); } } }

 

Android开发-图灵聊天机器人接口引用

标签:des   android   style   blog   http   io   ar   color   os   

原文地址:http://www.cnblogs.com/frank-zouxu/p/4121601.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!