0

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.

and on the button click which executes the asynctask

                        new usdjson().execute();

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
    }

sorry for being long

5
  • note that it I should get rhs= 0.645452785 British pounds from the json as a string so I sidplay it in textview Commented Aug 30, 2013 at 17:26
  • it is better for you to delete this question(because it shows that you don't know java syntax since those error are compiler time errors) ... problem is with new usdjson.execute(""); replace it with new usdjson().execute(""); Commented Aug 30, 2013 at 17:26
  • You are returning a JSONObject public JSONObject loadJSON(URL url) { Commented Aug 30, 2013 at 17:27
  • use URL url = new URL(requestUrl); to get URl from String. and start AsyncTask as new usdjson.execute(url); Commented Aug 30, 2013 at 17:27
  • I changed it, still I get same errors Commented Aug 30, 2013 at 17:27

1 Answer 1

1

Instead of

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

you should have

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

EDIT

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

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

JSONObject json = loadJSON(url);
return json;

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...

Sign up to request clarification or add additional context in comments.

9 Comments

Did you try to execute the AsyncTask doing that : new usdjson().execute(requestUrl ); ?
there are still errors in the activity, I can't start the app
What errors ? The only one you mentionned is "The method execute(URL...) in the type AsyncTask is not applicable for the arguments (String)", which should be solved by my comment above
check my updated question and the errors are the one commented //
I edited my answer. But in fact if you just followed what I told you in the other question, it would have been fine...
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.