Android JSON in AsyncTask errors

前端 未结 1 969
天命终不由人
天命终不由人 2021-01-27 08:51

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

1条回答
  •  猫巷女王i
    2021-01-27 09:46

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

    0 讨论(0)
提交回复
热议问题