gpt4 book ai didi

java - android Wolfram alpha 异常下载网址

转载 作者:行者123 更新时间:2023-12-01 12:43:52 24 4
gpt4 key购买 nike

我正在尝试在我的应用程序中使用 walfram alpha,我已经让我的代码在 java 项目中运行,但是当我尝试在 android 中使用它时,我收到“异常下载 URL”

这是我的代码

public class AlphaAPISample extends Activity{

// PUT YOUR APPID HERE:
private static String appid = MYAPPID;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String input = "who is the president";
WAEngine engine = new WAEngine();
engine.setAppID(appid);
engine.addFormat("plaintext");
WAQuery query = engine.createQuery();
query.setInput(input);
try {
System.out.println("Query URL:");
System.out.println(engine.toURL(query));
WAQueryResult queryResult = engine.performQuery(query);

if (queryResult.isError()) {
System.out.println("Query error");
System.out.println(" error code: " + queryResult.getErrorCode());
System.out.println(" error message: " + queryResult.getErrorMessage());

} else if (!queryResult.isSuccess()) {
System.out.println("Query was not understood; no results available.");

} else {

// Got a result.
System.out.println("Successful query. Pods follow:\n");
for (WAPod pod : queryResult.getPods()) {
if (!pod.isError()) {
if(pod.getTitle().equals("Result")) {
System.out.println(pod.getTitle());
for (WASubpod subpod : pod.getSubpods()) {
for (Object element : subpod.getContents()) {
if (element instanceof WAPlainText) {
System.out.println(((WAPlainText) element).getText());
}
}
}
}
}
}
// We ignored many other types of Wolfram|Alpha output, such as warnings, assumptions, etc.
// These can be obtained by methods of WAQueryResult or objects deeper in the hierarchy.
}
} catch (WAException e) {
e.printStackTrace();
}


}
}

这是我的日志猫

    07-21 05:13:23.276  30688-30688/com.assist.me I/System.out﹕ Query URL:
07-21 05:13:23.276 30688-30688/com.assist.me I/System.out﹕ http://api.wolframalpha.com/v2/query?appid=MYAPPID&input=who+is+the+president&format=plaintext&async=false&reinterpret=true
07-21 05:13:23.276 30688-30688/com.assist.me I/URLFetcher﹕ Downloading url http://api.wolframalpha.com/v2/query?appid=MYAPPID&input=who+is+the+president&format=plaintext&async=false&reinterpret=true
07-21 05:13:23.286 30688-30688/com.assist.me W/URLFetcher﹕ Exception downloading URL http://api.wolframalpha.com/v2/query?appid=MYAPPID&input=who+is+the+president&format=plaintext&async=false&reinterpret=true. com.wolfram.alpha.net.WAHttpException: android.os.NetworkOnMainThreadException
07-21 05:13:23.286 30688-30688/com.assist.me I/URLFetcher﹕ Finished downloading URL http://api.wolframalpha.com/v2/query?appid=MYAPPID&input=who+is+the+president&format=plaintext&async=false&reinterpret=true. Elapsed millis: 8
07-21 05:13:23.286 30688-30688/com.assist.me W/System.err﹕ com.wolfram.alpha.WAException: com.wolfram.alpha.net.WAHttpException: android.os.NetworkOnMainThreadException
07-21 05:13:23.286 30688-30688/com.assist.me W/System.err﹕ at com.wolfram.alpha.WAEngine.performQuery(WAEngine.java:128)
07-21 05:13:23.286 30688-30688/com.assist.me W/System.err﹕ at com.pin.assistme.AlphaAPISample.onCreate(AlphaAPISample.java:83)

代码中带有我的应用 ID 的每个位置都被替换为短语“MYAPPID”

已解决:

问题是我试图从主线程获取互联网数据,而我应该使用 AsyncTask,正如您在异常中看到的那样

android.os.NetworkOnMainThreadException

请参阅下面的代码以了解执行此操作的正确方法。

最佳答案

好吧,我明白了!

public class AlphaAPISample extends Activity {

// PUT YOUR APPID HERE:
private static String appid = "APPID";

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
new YourTask().execute();
}


WAQueryResult queryResult;

private class YourTask extends AsyncTask<WAQueryResult, Void, WAQueryResult> {


protected void onPreExecute() {

}

@Override
protected WAQueryResult doInBackground(WAQueryResult... urls) {
String input = "who is the president";
WAEngine engine = new WAEngine();
engine.setAppID(appid);
engine.addFormat("plaintext");

// Create the query.
WAQuery query = engine.createQuery();
query.setInput(input);
queryResult = null;
try {
queryResult = engine.performQuery(query);
} catch (WAException e) {
e.printStackTrace();
}
return queryResult;
}

@Override
protected void onPostExecute(WAQueryResult response) {
if (queryResult.isError()) {
System.out.println("Query error");
System.out.println(" error code: " + queryResult.getErrorCode());
System.out.println(" error message: " + queryResult.getErrorMessage());

} else if (!queryResult.isSuccess()) {
System.out.println("Query was not understood; no results available.");

} else {

// Got a result.
System.out.println("Successful query. Pods follow:\n");
for (WAPod pod : queryResult.getPods()) {
if (!pod.isError()) {
if (pod.getTitle().equals("Result")) {
System.out.println(pod.getTitle());
for (WASubpod subpod : pod.getSubpods()) {
for (Object element : subpod.getContents()) {
if (element instanceof WAPlainText) {
System.out.println(((WAPlainText) element).getText());
Toast.makeText(getApplicationContext(),((WAPlainText) element).getText(),Toast.LENGTH_SHORT).show();
}
}
}
}
}
}
}
}
}

}

关于java - android Wolfram alpha 异常下载网址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24856967/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com