package com.huncm.c202201020314;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import cn.bmob.v3.ai.ChatMessageListener;
public class ChatGPT_Page extends AppCompatActivity {
List<Chat> list=new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_chat_gpt_page);
TextView sendBt = findViewById(R.id.sendBt);
TextView questionchat = findViewById(R.id.questionchat);
TextView answerchat = findViewById(R.id.answerchat);
TextView chattime = findViewById(R.id.chattime);
EditText input = findViewById(R.id.GPTInput);
ImageView img1 = findViewById(R.id.img12);
ImageView img2= findViewById(R.id.img13);
ImageView lunbotu4= findViewById(R.id.lunbotu4);
Uri uri3= Uri.parse("https://roxying.oss-cn-shanghai.aliyuncs.com/lunbotu4.png");
Glide.with(ChatGPT_Page.this)
.load(uri3)
.into(lunbotu4);
什么是人工智能
sendBt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//连接AI服务器(这个代码为了防止AI连接中断,因为可能会存在某些情况下,比如网络切换、中断等,导致心跳连接失败)
BmobApp.bmobAI.Connect();
String question = input.getText().toString();
//发送对话信息
BmobApp.bmobAI.Chat(question, "session_id", new ChatMessageListener() {
@Override
public void onMessage(String message) {
//消息流的形式返回AI的结果
Log.d("Bmob", message);
}
@Override
public void onFinish(String message) {
// 定义时间格式
DateTimeFormatter formatter = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
LocalDateTime currentTime = LocalDateTime.now();
formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String formattedTime = currentTime.format(formatter);
chattime.setText(formattedTime);
}
questionchat.setText(question);
input.setText("");
answerchat.setText(message);
//一次性返回全部结果,这个方法需要等待一段时间,友好性较差
Log.d("Bmob", message);
}
@Override
public void onError(String error) {
//OpenAI的密钥错误或者超过OpenAI并发时,会返回这个错误
Log.d("Bmob", "连接发生异常了"+error);
}
@Override
public void onClose() {
Log.d("Bmob", "连接被关闭了");
}
});
Uri uri3= Uri.parse("https://roxying.oss-cn-shanghai.aliyuncs.com/touxiang2.jpg");
Glide.with(ChatGPT_Page.this)
.load(uri3)
.into(img1);
Uri uri2= Uri.parse("https://roxying.oss-cn-shanghai.aliyuncs.com/touxiang4.jpg");
Glide.with(ChatGPT_Page.this)
.load(uri2)
.into(img2);
}
});
}}
Android引入chatGpt
最新推荐文章于 2024-09-10 14:28:07 发布