Question

I'm trying to make a chat bot using Program AB and AIML. I am trying to get the AIML files off the internet so the bot can read them and use them. I wrote some code saying to get a text file of a URL, but that gives me an unhandled exception, and yes, I added the internet permission in the Manifest. btw I am new to Java.

Here is the 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);
}

}

Heres my 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>

I dont want to have to make the user download all the AIML files because it takes up too much. Any help will be greatly appreciated.

Was it helpful?

Solution

The dropbox link you are using is dropbox's Online file viewer, which to java looks like a bunch of HTML code, and it has no clue what to do with it. Goto the dropbox link you specified, and hit Download (upright) > Download as Zip, and use that URL instead. But you will have to modify your bot to first extract all the files from .zip format, and then you should be good to go.

BTW the URL is https://dl.dropboxusercontent.com/shz/9cyfz0b45mj6szr/7pBuupNz3N/xecta?token_hash=AAEs9cDFswt98D1IhLnab4dHwhwh5z2Lmhq_N6H-2M0LWg&top_level_offset=6

"I dont want to have to make the user download all the AIML files because it takes up too much. Any help will be greatly appreciated." -- If you have to download each file when you want to use them, it would slow down your App extremely. It would be much easier to download all of it first and save it into a local cache so they don't have to download it again later. FYI The download is less than half a megabyte in zip form, so it shouldn't be bad to download it all.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top