Java android wolfram alpha异常下载URL

Java android wolfram alpha异常下载URL,java,android,android-asynctask,android-studio,wolframalpha,Java,Android,Android Asynctask,Android Studio,Wolframalpha,我尝试在我的应用程序中使用walfram alpha我已经在java项目中使用了我的代码,但是当我尝试在android中使用它时,我得到了“异常下载URL” 这是我的密码 public class AlphaAPISample extends Activity{ // PUT YOUR APPID HERE: private static String appid = MYAPPID; @Override protected void onCreate(Bundle savedInstanc

我尝试在我的应用程序中使用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”

已解决:

问题是我试图从主线程获取internet数据,而我本应该使用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();
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
公共类alphapisample扩展活动{
//将您的APPID放在此处:
私有静态字符串appid=“appid”;
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
新建YourTask().execute();
}
WAQueryResult queryResult;
私有类YourTask扩展了AsyncTask{
受保护的void onPreExecute(){
}
@凌驾
受保护的WAQueryResult doInBackground(WAQueryResult…URL){
String input=“谁是总裁”;
WAEngine engine=新WAEngine();
engine.setAppID(appid);
engine.addFormat(“纯文本”);
//创建查询。
WAQuery query=engine.createQuery();
query.setInput(输入);
queryResult=null;
试一试{
queryResult=engine.performQuery(查询);
}捕获(WAE例外){
e、 printStackTrace();
}
返回查询结果;
}
@凌驾
受保护的void onPostExecute(WAQueryResult响应){
if(queryResult.isError()){
System.out.println(“查询错误”);
System.out.println(“错误代码:+queryResult.getErrorCode());
System.out.println(“错误消息:+queryResult.getErrorMessage());
}如果(!queryResult.isSuccess()),则为else{
System.out.println(“查询未被理解;没有可用的结果”);
}否则{
//我得到了一个结果。
System.out.println(“成功查询。Pods跟随:\n”);
for(WAPod:queryResult.getPods()){
如果(!pod.isError()){
if(pod.getTitle().equals(“结果”)){
System.out.println(pod.getTitle());
for(WASubpod subpod:pod.getSubpods()){
for(对象元素:subpod.getContents()){
if(WAPlainText的元素实例){
System.out.println(((WAPlainText)元素).getText());
Toast.makeText(getApplicationContext(),((WAPlainText)元素).getText(),Toast.LENGTH_SHORT.show();
}
}
}
}
}
}
}
}
}

}

您是否介意记下到底哪里出了问题,以及您是如何解决的?如果将来有人引用此内容,请不要立即注意到您的问题是
NetworkOnMainThreadException