如何下车Android Studio中使用Java的网络文件夹? [英] How to get a folder off the internet using Java in Android Studio?

查看:302
本文介绍了如何下车Android Studio中使用Java的网络文件夹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让使用程序AB和AIML一个聊天机器人。我试图让AIML文件从互联网使机器人可以读取并使用它们。我写了一些code说要得到一个URL的一个文本文件,但是这给了我一个未处理的异常,是的,我加在清单中的Internet权限。顺便说一句我是新来的Java。

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.

下面是MainActivity.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);
}

}

我的继承人activity_main.xml中

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>

我不希望有让用户下载的所有文件,AIML因为它占用了太多。
任何帮助将大大AP preciated。

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.

推荐答案

您正在使用的Dropbox的链接的Dropbox的在线文件浏览器,它为Java看起来像一堆HTML code,而且它没有什么线索来用它做。转到您指定,并创下下载Dropbox的链接(直立)>下载为Zip,并使用该URL来代替。但是,你必须修改你的机器人先提取.zip格式的所有文件,然后你要善于去。

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的URL是<一个href=\"https://dl.dropboxusercontent.com/shz/9cyfz0b45mj6szr/7pBuupNz3N/xecta?token_hash=AAEs9cDFswt98D1IhLnab4dHwhwh5z2Lmhq_N6H-2M0LWg&top_level_offset=6\" rel=\"nofollow\">https://dl.dropboxusercontent.com/shz/9cyfz0b45mj6szr/7pBuupNz3N/xecta?token_hash=AAEs9cDFswt98D1IhLnab4dHwhwh5z2Lmhq_N6H-2M0LWg&top_level_offset=6

我不希望有让用户下载的所有文件,AIML因为它占用了太多。任何帮助将大大AP preciated。 - 如果你要下载的每个文件,当你想使用它们,那就你的App非常慢下来。这将是更容易先下载这一切,并保存到本地缓存,使他们不必稍后重新下载。 FYI下载小于zip格式半兆字节,所以它应该不会差下载了这一切。

"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.

这篇关于如何下车Android Studio中使用Java的网络文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆