0

I need to reach a JSON hosted somewhere on the web, retrieve it and manipulate it. To do this, I know it's recommended to implement a handler class to help with the JSON calls and all but I cannot remember the how-to. I do remember you could do it extending AsyncTask and using String, Void and JSONObject as the wildcards, you know, like this:

public class JSONhandler extends AsyncTask <String, Void, JSONObject>{
     //code here
}

I would like to do this strictly by extending AsyncTask<>. I'm aware this question addresses the use of AsyncTask but that's not how I'm looking for it. That question's code extends Activity, and I do not which this class to be an Android Activity, I wanna implement this class in an Android Activity.

The thing is I do not remember the how-to.

Thanks in advance.

1 Answers1

0

There are many ways to do this, if you wanna use the AsyncTask the code inside your class should be the following

public class JSONrequest extends AsyncTask<String, Void, JSONObject> {

    // First you'll need an interface that'll call requestDone() for the JSONobject you wanna listen
    public interface JSONListener{
        void requestDone(JSONObject jsonObject);
    }

    private JSONListener jListener;

    //Your class will need a constructor. Basic Java.
    public JSONrequest(JSONListener jListener){
        this.jListener = jListener;
    }

    @Override
    //You'll be required to implement this method because of the extension and
    //here's where the logic of your handler class goes
    protected JSONObject doInBackground(String... strings) {

    // First try to retrieve the url
    JSONObject result = null;  //initializing the result we'll return
    try{
        URL url = new URL(strings[0]);
        HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();


        int responseCode = urlConnection.getResponseCode();
        if(responseCode == HttpURLConnection.HTTP_OK){

            InputStream is = urlConnection.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            StringBuilder sb = new StringBuilder();
            String currentLine = "";

            while((currentLine = br.readLine()) != null){
                Log.d("JSON REQUEST", currentLine); //a little log so you can see what's going on
                sb.append(currentLine);
            }

            result = new JSONObject(sb.toString());

        }

    }catch (Exception e){
        e.printStackTrace();
    }
    return result;
        return null;
    }
}

If you're gonna fetch your JSON from a server it's very important that you give your app the permission to use Internet. To do this, you add the following line to the Android Manifest:

<uses-permission android:name="android.permission.INTERNET" />

I hope this solves your problem. Good luck.

  • Thanks! I went perfect with my activity class, which ended up like this: public class Activity extends AppCompatActivity implements JSONrequest.JSON.JSONListener { //Override protected void onCreate(Bundle savedInstanceState) { //onCreate and setContentView stuff JSONrequest req = new JSONrequest(this); req.execute("the url where Im fetching the JSON"); } //Override public void requestDone(){ try{ //my logic }catch(JSONExeption je){ je.printStack(); } } } – Zikri Megat Oct 23 '17 at 05:52