0

我正在尝试使用 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 文件,因为它占用太多。任何帮助将不胜感激。

4

1 回答 1

0

您正在使用的 Dropbox 链接是 Dropbox 的在线文件查看器,它对 java 来说就像一堆 HTML 代码,它不知道如何处理它。转到您指定的保管箱链接,然后点击下载(直立)> 下载为 Zip,然后使用该 URL。但是您必须修改您的机器人以首先从 .zip 格式中提取所有文件,然后您就可以开始了。

顺便说一句,网址是https://dl.dropboxusercontent.com/shz/9cyfz0b45mj6szr/7pBuupNz3N/xecta?token_hash=AAEs9cDFswt98D1IhLnab4dHwhwh5z2Lmhq_N6H-2M0LWg&top_level_offset=6

“我不想让用户下载所有的 AIML 文件,因为它占用太多。任何帮助将不胜感激。” -- 如果你想使用它们时必须下载每个文件,这会极大地降低你的应用程序的速度。首先下载所有内容并将其保存到本地缓存中会容易得多,这样他们以后就不必再次下载了。仅供参考,zip格式的下载不到半兆字节,所以下载它应该不错。

于 2014-04-13T00:28:57.597 回答