【问题标题】:Problems while connecting to pandorbot server in android在android中连接到pandorbot服务器时出现问题
【发布时间】:2014-04-30 19:04:49
【问题描述】:

我正在尝试在 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 中的值为 null 的原因。谁能比我更有知识,请查看代码并指出我在哪里犯了错误?

【问题讨论】:

  • 您的 logcat 是否显示任何异常?应该是,因为pandorabotsRequest(String, String, String)在catch中只能返回null,specresponseContent(string)不能返回null,只能抛出异常。尽量避免笼统地说(catch (Exception e)),但要具体

标签: java android pandorabots


【解决方案1】:

从新的线程发出 HTTP 请求。

它应该解决 NetworkOnMainThreadException

现在要更新 UI 线程 中的任何内容,请使用 Handler

【讨论】:

    【解决方案2】:

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

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

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

    uses-permission android:name="android.permission.INTERNET" 
    uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" 
    

    将此导入语句添加到您的主要活动中

    import android.os.StrictMode;     
    

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

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

    【讨论】:

    • 您已经发布了两次几乎相同的答案。我建议你删除其中一个。
    • 这是一个非常糟糕的解决方案。
    【解决方案3】:

    我遇到了同样的问题,这里是解决方法:

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

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

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

    将此导入语句添加到您的主要活动中

    导入 android.os.StrictMode;

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

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

    你应该很高兴!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-16
      • 1970-01-01
      • 2010-10-11
      • 1970-01-01
      • 2016-10-05
      • 1970-01-01
      • 2016-04-23
      • 1970-01-01
      相关资源
      最近更新 更多