java - 在 android 中连接到 pandorbot 服务器时出现问题

标签 java android pandorabots

我正在尝试在 Android 中制作一个聊天机器人。所以,我正在尝试将 pandorabot 与我的 Android 应用程序连接。

public class Brain {
    public String defaultCustid = "0";
    public String custid = defaultCustid;
    public String responseFailed = "RESPONSE FAILED";
    public String defaultBotId = "da43c986ee34039e";
    public String defaultHost = "http://www.pandorabots.com";
    String botResponse;

    public String askPandorabots(String input) {
        return askPandorabots(input, defaultHost, defaultBotId);
    }
    public String askPandorabots(String input, String host, String botid) {
        String responseContent = pandorabotsRequest(input, host, botid);
        if (responseContent == null) 
            return responseFailed;
        else 
            return pandorabotsResponse(responseContent, host, botid);
    }

    public String pandorabotsRequest(String input, String host, String botid) {
        try {
            String spec = spec(host, botid, custid, input);
            return responseContent(spec);
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    public String spec(String host, String botid, String custid, String input) {
        String spec = "";
        try {
            if (custid.equals("0"))      // get custid on first transaction with Pandorabots
                spec =    String.format("%s?botid=%s&input=%s",
                        "http://" + host + "/pandora/talk-xml",
                        botid,
                        URLEncoder.encode(input, "UTF-8"));
            else spec =                 // re-use custid on each subsequent interaction
                    String.format("%s?botid=%s&custid=%s&input=%s",
                            "http://" + host + "/pandora/talk-xml",
                            botid,
                            custid,
                            URLEncoder.encode(input, "UTF-8"));
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return spec;
    }

    public String responseContent(String url) throws Exception {
        HttpClient client = new DefaultHttpClient();
        HttpGet request = new HttpGet();
        request.setURI(new URI(url));
        InputStream is = client.execute(request).getEntity().getContent();
        BufferedReader inb = new BufferedReader(new InputStreamReader(is));
        StringBuilder sb = new StringBuilder("");
        String line;
        String NL = System.getProperty("line.separator");
        while ((line = inb.readLine()) != null) {
            sb.append(line).append(NL);
        }
        inb.close();
        return sb.toString();
    }
}

这是我用于连接的代码。但机器人总是回复“响应失败”。

String responseContent = pandorabotsRequest(input, host, botid);

if (responseContent == null) 
    return responseFailed;
else 
    return pandorabotsResponse(responseContent, host, botid);

这里,responseContent的值变成了null,所以显示“Response Failed”。我已经多次查看了引用,但我似乎无法找到为什么responseContent 中的值为空。任何比我更有知识的人都可以检查一下代码并指出我在哪里犯了错误吗?

最佳答案

我也遇到了同样的问题,解决方法如下:

Java 在 HttpGet() 深处抛出异常 android.os.NetworkOnMainThreadException

这是因为你试图在主线程上进行网络操作

将这两行添加到您的manifest.xml

将此导入语句添加到您的主要 Activity

导入 android.os.StrictMode;

将这两行代码添加到主 Activity 的 onCreate() 方法

StrictMode.ThreadPolicy 策略 = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(策略);

你应该可以开始了!

关于java - 在 android 中连接到 pandorbot 服务器时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22592212/

相关文章:

android - 通过通知向 pendingIntent 发送不同的参数

javascript - 在一个 HTML 标签中调用多个函数

java - XML-RPC 错误,org.xmlpull.v1.XmlPullParserException

java - 如何使用 Java API 获取 Azure VM(非经典/资源托管)列表

javafx 2 媒体异常 : MEDIA_UNAVAILABLE won't load file

java - 从 javadocs 制作一个 bitbucket 源 wiki

java - 无法解析 java.lang.ClassCastException

JAVA If else 如果用户输入 1 = 0 则退出的进程不起作用

java - 计算器问题