首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在android中连接到pandorbot服务器时出现问题

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

Stack Overflow用户
提问于 2014-03-23 22:31:40
回答 3查看 113关注 0票数 1

我正在尝试用android做一个聊天机器人。因此,我正在尝试将pandorabot与我的android应用程序连接起来。

代码语言:javascript
复制
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();
    }
}

这是我用于连接的代码。但机器人总是以"Response Failed“响应。

代码语言:javascript
复制
String responseContent = pandorabotsRequest(input, host, botid);

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

这里,responseContent的值变为null,因此显示"Response Failed“。我已经看了几次引号,但我似乎找不到为什么responseContent中的值是null。有没有比我更有见识的人,请看一下代码,指出我在哪里犯了错?

EN

回答 3

Stack Overflow用户

发布于 2014-08-15 13:37:54

我也有同样的问题,解决方法如下:

Java在HttpGet()中抛出了一个异常android.os.NetworkOnMainThreadException

这是因为您正尝试在主线程上执行网络操作

将这两行添加到manifest.xml中

将此import语句添加到主活动中

导入android.os.StrictMode;

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

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

你应该可以走了!

票数 0
EN

Stack Overflow用户

发布于 2014-11-17 21:03:05

从新的HTTP线程发出请求。

它应该可以解决NetworkOnMainThreadException.

现在,要更新UI线程处理程序中的任何内容,请使用。

票数 0
EN

Stack Overflow用户

发布于 2014-08-15 13:42:57

Java在HttpGet()中抛出了一个异常android.os.NetworkOnMainThreadException

这是因为您正尝试在主线程上执行网络操作

将这两行添加到manifest.xml

代码语言:javascript
复制
uses-permission android:name="android.permission.INTERNET" 
uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" 

将此import语句添加到主活动中

代码语言:javascript
复制
import android.os.StrictMode;     

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

代码语言:javascript
复制
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22592212

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档