1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > <Android>集成图灵机器人的小demo

<Android>集成图灵机器人的小demo

时间:2023-07-27 23:22:28

相关推荐

<Android>集成图灵机器人的小demo

需要自己先注册账号注册机机器人,并且得到自己的apiKey

接下来就可以使用了,上代码

package com.sdp.panda.panpanapp.utils;import com.google.gson.Gson;import com.sdp.panda.panpanapp.bean.ChatMessage;import com.sdp.panda.panpanapp.bean.ResultBean;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.UnsupportedEncodingException;import .HttpURLConnection;import .MalformedURLException;import .URL;import .URLEncoder;import java.util.Date;/*** Created by 80926 on /12/1.*/public class HttpUtils {private static final String URL = "/openapi/api";private static final String API_KEY = "dbc824a0f5164b6a97ef7c9c828e350c";///openapi/api?key=dbc824a0f5164b6a97ef7c9c828e350c&info=你好啊//发送消息得到消息public static ChatMessage sendMessage(String msg) {ChatMessage chatMessage = new ChatMessage();String jsonRes = doGet(msg);Gson gson = new Gson();ResultBean resultBean = null;try {resultBean = gson.fromJson(jsonRes, ResultBean.class);chatMessage.setMsg(resultBean.getText());}catch (Exception e){chatMessage.setMsg("服务器繁忙");}chatMessage.setDate(new Date());chatMessage.setType(ChatMessage.Type.INCOMING);return chatMessage;}public static String doGet(String msg) {String result = "";String url = setParams(msg);InputStream is = null;ByteArrayOutputStream baos = null;try {.URL urlNet = new URL(url);HttpURLConnection conn = (HttpURLConnection) urlNet.openConnection();conn.setConnectTimeout(5000);conn.setReadTimeout(5000);conn.setRequestMethod("GET");is = conn.getInputStream();int len = -1;byte[] buffer = new byte[1024];baos = new ByteArrayOutputStream();while ((len = is.read(buffer)) != -1) {baos.write(buffer, 0, len);}baos.flush();result = new String(baos.toByteArray());} catch (MalformedURLException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {if (baos != null) {try {baos.close();} catch (IOException e) {e.printStackTrace();}}if (is != null) {try {is.close();} catch (IOException e) {e.printStackTrace();}}}return result;}private static String setParams(String msg) {String url = "";try {url = URL + "?key=" + API_KEY + "&info=" + URLEncoder.encode(msg, "UTF-8");} catch (UnsupportedEncodingException e) {e.printStackTrace();}return url;}}

1

package com.sdp.panda.panpanapp;import android.os.AsyncTask;import android.os.Handler;import android.os.Message;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.text.TextUtils;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.ListView;import android.widget.TextView;import android.widget.Toast;import com.sdp.panda.panpanapp.adapter.ChatMessageAdapter;import com.sdp.panda.panpanapp.bean.ChatMessage;import com.sdp.panda.panpanapp.utils.HttpUtils;import java.util.ArrayList;import java.util.Date;import java.util.List;public class MainActivity extends AppCompatActivity implements View.OnClickListener {private TextView textView;private ListView lvSend;private EditText etSend;private Button btnSend;private List<ChatMessage> data = new ArrayList<>();private ChatMessageAdapter adapter;private Handler handler = new Handler(){@Overridepublic void handleMessage(Message msg) {super.handleMessage(msg);ChatMessage fromChatMessage = (ChatMessage) msg.obj;data.add(fromChatMessage);adapter.notifyDataSetChanged();lvSend.setSelection(data.size()-1);}};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initView();initData();btnSend.setOnClickListener(this);}private void initData() {ChatMessage chatMessage = new ChatMessage("你好盼盼为你服务", ChatMessage.Type.INCOMING,new Date());data.add(chatMessage);adapter = new ChatMessageAdapter(this,data);lvSend.setAdapter(adapter);}private void initView() {lvSend = (ListView) findViewById(R.id.lv_send);etSend = (EditText) findViewById(R.id.et_send_msg);btnSend = (Button) findViewById(R.id.btn_send_msg);}@Overridepublic void onClick(View v) {final String string = etSend.getText().toString().trim();if (TextUtils.isEmpty(string)){Toast.makeText(this, "不能为空", Toast.LENGTH_SHORT).show();return;}ChatMessage toChatMessage = new ChatMessage();toChatMessage.setType(ChatMessage.Type.OUTCOMING);toChatMessage.setMsg(string);toChatMessage.setDate(new Date());data.add(toChatMessage);adapter.notifyDataSetChanged();lvSend.setSelection(data.size()-1);etSend.setText("");new Thread(new Runnable() {@Overridepublic void run() {ChatMessage fromMessage = HttpUtils.sendMessage(string);fromMessage.setDate(new Date());fromMessage.setType(ChatMessage.Type.INCOMING);fromMessage.setMsg(fromMessage.getMsg());// runOnUiThread(new Runnable() {// @Override// public void run() {//// }// });Message message = new Message();message.obj = fromMessage;handler.sendMessage(message);}}).start();}}

1

package com.sdp.panda.panpanapp.bean;import java.util.Date;/*** Created by 80926 on /12/1.*/public class ChatMessage {private String name;private String msg;private Type type;private Date date;public ChatMessage() {}public ChatMessage(String msg, Type type, Date date) {this.msg = msg;this.type = type;this.date = date;}public enum Type {INCOMING, OUTCOMING}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getMsg() {return msg;}public void setMsg(String msg) {this.msg = msg;}public Type getType() {return type;}public void setType(Type type) {this.type = type;}public Date getDate() {return date;}public void setDate(Date date) {this.date = date;}}

1

package com.sdp.panda.panpanapp.bean;/*** Created by 80926 on /12/1.*/public class ResultBean {private int code;private String text;public int getCode() {return code;}public void setCode(int code) {this.code = code;}public String getText() {return text;}public void setText(String text) {this.text = text;}@Overridepublic String toString() {return "ResultBean{" +"code=" + code +", text='" + text + '\'' +'}';}}

1

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><TextViewandroid:id="@+id/from_msg_date"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="15sp"android:textColor="@android:color/black"android:gravity="center"android:layout_gravity="center"android:background="@android:color/holo_blue_light"android:text="_3_3"/><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="horizontal"><LinearLayoutandroid:layout_gravity="center"android:layout_marginLeft="5dp"android:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="vertical"><ImageViewandroid:layout_width="40dp"android:layout_height="40dp"android:scaleType="centerCrop"android:layout_gravity="center"android:src="@mipmap/ic_launcher"/><TextViewandroid:layout_marginTop="5dp"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="10sp"android:textColor="@android:color/darker_gray"android:text="盼盼机器人"/></LinearLayout><TextViewandroid:id="@+id/from_msg"android:layout_marginLeft="5dp"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"android:gravity="center"android:text="你也好啊"android:background="@drawable/chatfrom_bg_normal"android:textSize="16sp"/></LinearLayout></LinearLayout>

1

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:gravity="right"android:orientation="vertical"><TextViewandroid:id="@+id/send_msg_date"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="15sp"android:textColor="@android:color/black"android:gravity="center"android:layout_gravity="center"android:background="@android:color/darker_gray"android:text="_3_3"/><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="horizontal"><TextViewandroid:id="@+id/send_msg"android:layout_marginLeft="5dp"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"android:text="你好"android:gravity="center"android:background="@drawable/chatto_bg_normal"android:textSize="16sp"/><LinearLayoutandroid:layout_marginRight="5dp"android:layout_marginLeft="10dp"android:layout_gravity="center"android:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="vertical"><ImageViewandroid:layout_width="40dp"android:layout_height="40dp"android:scaleType="centerCrop"android:layout_gravity="center"android:src="@mipmap/ic_launcher"/><TextViewandroid:layout_marginTop="5dp"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="10sp"android:layout_gravity="center"android:textColor="@android:color/darker_gray"android:text="盼盼"/></LinearLayout></LinearLayout></LinearLayout>

1

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="/apk/res/android"android:id="@+id/activity_main"android:layout_width="match_parent"android:layout_height="match_parent"><RelativeLayoutandroid:id="@+id/rl_title"android:layout_width="match_parent"android:layout_height="45dp"android:background="@android:color/black"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="20sp"android:layout_centerInParent="true"android:textColor="@android:color/white"android:text="与盼盼机器人对话中..."/></RelativeLayout><RelativeLayoutandroid:id="@+id/rl_send"android:layout_width="match_parent"android:background="@color/colorAccent"android:layout_alignParentBottom="true"android:layout_height="40dp"><EditTextandroid:id="@+id/et_send_msg"android:layout_margin="3dp"android:layout_centerInParent="true"android:background="@android:color/white"android:layout_width="match_parent"android:layout_height="30dp" /><Buttonandroid:id="@+id/btn_send_msg"android:textColor="@android:color/white"android:background="@android:color/holo_red_dark"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentRight="true"android:text="发送"/></RelativeLayout><ListViewandroid:layout_below="@id/rl_title"android:layout_above="@id/rl_send"android:id="@+id/lv_send"android:divider="@null"android:dividerHeight="3dp"android:layout_width="match_parent"android:layout_height="match_parent"></ListView></RelativeLayout>

1

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。