我正在尝试使用 Program AB 和 AIML 制作聊天机器人。我正在尝试从互联网上获取 AIML 文件,以便机器人可以读取并使用它们。我写了一些代码来获取 URL 的文本文件,但这给了我一个未处理的异常,是的,我在 Manifest 中添加了 internet 权限。顺便说一句,我是 Java 新手。
这是 MainActivity.java
package com.NautGames.xecta.app;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import org.alicebot.ab.Chat;
import org.alicebot.ab.Bot;
import android.view.View;
import android.widget.Button;
import java.io.BufferedReader;
import java.net.MalformedURLException;
import java.io.InputStreamReader;
import java.net.URL;
import java.io.IOException;
public class MainActivity extends ActionBarActivity {
//private EditText botIn = (EditText) findViewById(R.id.botInput);;
//private Button botSubmit = (Button) findViewById(R.id.tellBot);;
//String botString = botIn.getText().toString();
EditText mEdit;
public String botIn;
private String readData()
{
try
{
// Create a URL for the desired page
URL url = new URL("https://www.dropbox.com/sh/9cyfz0b45mj6szr/7pBuupNz3N");
// Read all the text returned by the server
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String str;
while ((str = in.readLine()) != null)
{
// str is one line of text; readLine() strips the newline character(s)
}
in.close();
}
catch (MalformedURLException e)
{
//do nothing
}
catch (IOException e)
{
//do nothing
}
return "URL";
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void buttonOnClick(View v)
{
Button button=(Button) v;
mEdit = (EditText)findViewById(R.id.editText1);
//Creating bot
String botname="xecta";
String path= readData();
Bot xecta = new Bot(botname, path);
Chat chatSession = new Chat(xecta);
String request = mEdit.getText().toString();
String response = chatSession.multisentenceRespond(request);
((Button) v).setText(response);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
这是我的activity_main.xml
<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.NautGames.chatbot.app.MainActivity"
android:background="@drawable/oneiric640x960">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText1"
android:hint="Talk to Xecta"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send"
android:id="@+id/button1"
android:layout_centerHorizontal="true"
android:layout_below="@+id/editText1"
android:onClick="buttonOnClick" />
</RelativeLayout>
我不想让用户下载所有的 AIML 文件,因为它占用太多。任何帮助将不胜感激。