0

我正在尝试使用 asynctask 从谷歌获取 JSON 数据。但是我收到很多错误,我不知道为什么。我是android开发的新手,我对它很感兴趣,然后现在又退出了:P。

并单击执行异步任务的按钮

                        new usdjson().execute();

这是我的异步任务

    private class usdjson extends AsyncTask<URL, Void, JSONObject> {

        @Override
        protected Boolean doInBackground(URL... urls) {
// I get this from Boolean : The return type is incompatible with AsyncTask<URL,Void,JSONObject>.doInBackground(URL[])
            URL url = new URL(requestUrl);
            loadJSON(url);
        }

        @Override
        protected void onPostExecute(JSONObject jsonData) {
            try {
               String USD = json.getJSONArray("rhs");
//I get this from json. :json cannot be resolved
        }
    }


    public void loadJSON(URL url) {
        JSONParser jParser = new JSONParser();
//I get this from JSONPareser JSONParser cannot be resolved to a type
        JSONObject json = jParser.getJSONFromUrl(url);
        return json;
//Void methods cannot return a value
    }

抱歉太长了

4

1 回答 1

1

代替

URL requestUrl = "http://www.google.com/ig/calculator?hl=en&q=1USD=?GBP";

你应该有

URL requestUrl = new URL("http://www.google.com/ig/calculator?hl=en&q=1USD=?GBP");

编辑

首先,将返回类型更改doInBackground()为 JSONObject :

protected JSONObject doInBackground(URL... urls) {

urls是一个数组,因此要访问其中的 url,您必须指定位置:

URL url = new URL(urls[0]);    // urls[0] is the URL you passed when calling the .execute method

您想在 中处理 JSONObject onPostExecute(),因此您必须更改的返回 doInBackground

JSONObject json = loadJSON(url);
return json;

但是对于其余部分,您似乎只是复制/粘贴了一个代码,并且似乎缺少很多代码,因此很难为您提供更多帮助...您必须进行更改loadJSON,以便它执行您想要的处理(获取来自 URL 的数据,处理它并返回一个 JSON 对象)。事实上,复制您在几分钟前发布的其他 StackOverflow 问题中的代码......

于 2013-08-30T17:28:02.260 回答