在AsyncTask的错误的Andr​​oid JSON [英] Android JSON in AsyncTask errors

查看:116
本文介绍了在AsyncTask的错误的Andr​​oid JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用的AsyncTask抓住从谷歌JSON数据。 但我得到了很多错误,我不知道为什么。我是新的Andr​​oid开发,我很感兴趣,它然后退出,现在回来了:P

I'm trying to grab JSON data from google by using asynctask. But i get a lot of errors I don't know why. I'm new to android development, I was interested in it then quit now back again :P.

和按钮点击它执行的AsyncTask

and on the button click which executes the asynctask

                        new usdjson().execute();

这是我的AsyncTask

this is my Asynctask

    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
    }

抱歉是很长

推荐答案

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

你应该有

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

修改

首先,改变的返回类型doInBackground()来的JSONObject:

First, change the return type of doInBackground() to JSONObject :

protected JSONObject doInBackground(URL... urls) {

网​​址是一个数组,因此,您所指定的位置访问该网址在里面:

urls is an array, so to access the url in it, you have to specify the position :

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

您想处理的JSONObject在 onPostExecute(),所以你必须要改变 doInBackground 的回报:

You want to process a JSONObject in onPostExecute(), so you have to change the return of doInBackground :

JSONObject json = loadJSON(url);
return json;

但对于剩下的,好像刚复制/粘贴一个code和似乎有很多code丢失,所以很难来帮助你更多...你必须改变 loadJSON 所以你想要做的处理(您可以通过URL中的数据,处理它,并返回一个JSON对象)。我其实复制code你把你贴在几分钟前你的其他的StackOverflow问题...

But for the rest, it seems that you just copied/pasted a code and there seems to be a lot of code missing, so it's hard to help you more... You have to change loadJSON so it does the processing you want (get the data from the Url, process it and return a JSON Object). I fact, copy the code you put in your other StackOverflow question that you posted a few minutes ago...

这篇关于在AsyncTask的错误的Andr​​oid JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆