我正在尝试用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();
}
}这是我用于连接的代码。但机器人总是以"Response Failed“响应。
String responseContent = pandorabotsRequest(input, host, botid);
if (responseContent == null)
return responseFailed;
else
return pandorabotsResponse(responseContent, host, botid);这里,responseContent的值变为null,因此显示"Response Failed“。我已经看了几次引号,但我似乎找不到为什么responseContent中的值是null。有没有比我更有见识的人,请看一下代码,指出我在哪里犯了错?
发布于 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 );
你应该可以走了!
发布于 2014-11-17 21:03:05
从新的HTTP线程发出请求。
它应该可以解决NetworkOnMainThreadException.
现在,要更新UI线程处理程序中的任何内容,请使用。
发布于 2014-08-15 13:42:57
Java在HttpGet()中抛出了一个异常android.os.NetworkOnMainThreadException
这是因为您正尝试在主线程上执行网络操作
将这两行添加到manifest.xml中
uses-permission android:name="android.permission.INTERNET"
uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" 将此import语句添加到主活动中
import android.os.StrictMode; 将这两行代码添加到onCreate()方法的主活动中
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);https://stackoverflow.com/questions/22592212
复制相似问题