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

John Jared
  • 790
  • 3
  • 16
  • 40
  • note that it I should get rhs= 0.645452785 British pounds from the json as a string so I sidplay it in textview – John Jared Aug 30 '13 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("");` – Selvin Aug 30 '13 at 17:26
  • You are returning a `JSONObject` `public JSONObject loadJSON(URL url) {` – Raghunandan Aug 30 '13 at 17:27
  • use `URL url = new URL(requestUrl);` to get URl from String. and start AsyncTask as `new usdjson.execute(url);` – ρяσѕρєя K Aug 30 '13 at 17:27
  • I changed it, still I get same errors – John Jared Aug 30 '13 at 17:27

1 Answers1

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

jbihan
  • 3,053
  • 2
  • 24
  • 34
  • Did you try to execute the AsyncTask doing that : `new usdjson().execute(requestUrl );` ? – jbihan Aug 30 '13 at 17:35
  • there are still errors in the activity, I can't start the app – John Jared Aug 30 '13 at 17:36
  • 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 – jbihan Aug 30 '13 at 17:37
  • check my updated question and the errors are the one commented // – John Jared Aug 30 '13 at 17:41
  • I edited my answer. But in fact if you just followed what I told you in the other question, it would have been fine... – jbihan Aug 30 '13 at 17:52
  • i replaced the doinbackground successfuly. but second one urls I get this error The constructor URL(URL) is undefined. – John Jared Aug 30 '13 at 17:54
  • See the update in the other question : http://stackoverflow.com/questions/18537860/getting-value-using-json-nullpointer/18538090#18538090 I didn't try it but it should be close to what you want. – jbihan Aug 30 '13 at 18:00
  • new url(xx) gives me The constructor URL(URL) is undefined – John Jared Aug 30 '13 at 18:01
  • Don't forget to accept both my answers if you make it work ;) And you can continue asking questions, I'll be around for an hour or so – jbihan Aug 30 '13 at 18:02