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

Java 在android中连接pandorbot服务器时出现问题,java,android,pandorabots,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 = "d

我正在尝试用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();
    }
}
这是我用于连接的代码。但是bot总是以“Response Failed”进行响应


这里,responseContent的值变为null,因此显示“Response Failed”。我已经查看了几次引用,但似乎找不到responseContent中的值为null的原因。有谁比我更了解这段代码并指出我犯的错误吗?

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


Java正在HttpGet()的深处抛出一个异常android.os.NetworkOnMainThreadException

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

将这两行添加到manifest.xml

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

import android.os.StrictMode;     
导入android.os.StrictMode

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

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


你应该可以走了

Java正在
android.os.NetworkOnMainThreadException
HttpGet()

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

将这两行添加到您的
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);

从新的线程发出HTTP请求

它应该解决NetworkOnMainThreadException


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

,您已经发布了两次几乎相同的答案。我建议您删除其中一个。这是一个非常糟糕的解决方案。您的日志是否显示任何异常?应该是,对于
pandorabotrequest(String,String,String)
只能在catch中返回null,
spec
responseContent(String)
不能返回null,只能抛出异常。尽量避免使用catchall(
catch(异常e)
),但要具体