Tight coupling in using call back method I am using below code and for result i am using interface for call back purposes.

@Override
public void onResult(String result) {

    if (this.mClient != null) {
        this.mClient.cancel();
        mProcessing = false;
    }

    try {
        JSONObject j = new JSONObject(result);
        JSONObject j1 = j.getJSONObject("status");
        int j2 = j1.getInt("code");
        if (j2 == 0) {
            JSONObject metadata = j.getJSONObject("metadata");
            //
            if (metadata.has("humming")) {
                JSONArray hummings = metadata.getJSONArray("humming");
                for (int i = 0; i &lt; hummings.length(); i++) {
                    JSONObject tt = (JSONObject) hummings.get(i);
                    String title = tt.getString("title");
                    JSONArray artistt = tt.getJSONArray("artists");
                    JSONObject art = (JSONObject) artistt.get(0);
                    String artist = art.getString("name");
                    tres = tres + (i + 1) + ".  Title: " + title + "    Artist: " + artist + "\n";
                }
            }
            if (metadata.has("music")) {
                JSONArray musics = metadata.getJSONArray("music");
                for (int i = 0; i &lt; musics.length(); i++) {
                    JSONObject tt = (JSONObject) musics.get(i);
                    String title = tt.getString("title");
                    JSONArray artistt = tt.getJSONArray("artists");
                    JSONObject art = (JSONObject) artistt.get(0);
                    String artist = art.getString("name");
                    tres = tres + (i + 1) + ".  Title: " + title + "    Artist: " + artist + "\n";
                }
            }
            if (metadata.has("streams")) {
                JSONArray musics = metadata.getJSONArray("streams");
                for (int i = 0; i &lt; musics.length(); i++) {
                    JSONObject tt = (JSONObject) musics.get(i);
                    String title = tt.getString("title");
                    JSONArray channelId = tt.getJSONArray("channel_id");
                    tres = tres + (i + 1) + ".  Title: " + title + "    Channel Id: " + channelId + "\n";
                }
            }
            if (metadata.has("custom_files")) {
                JSONArray musics = metadata.getJSONArray("custom_files");
                for (int i = 0; i &lt; musics.length(); i++) {
                    JSONObject tt = (JSONObject) musics.get(i);
                    String title = tt.getString("title");
                    //tres = tres + (i + 1) + ".  Title: " + title + "\n";
                    tres = tt.getString("acr_id");
                }
            }
            //tres = tres + "\n\n" + result;
            if (baseClass instanceof MainActivity)
                ((MainActivity) baseClass).onResultFound(tres);
        } else {
            tres = result;
            if (baseClass instanceof MainActivity)
                ((MainActivity) baseClass).onResultNotFound(tres);
        }

    } catch (JSONException e) {
        tres = result;
        e.printStackTrace();
        if (baseClass instanceof MainActivity)
            ((MainActivity) baseClass).onErrorFound(tres);
    }

    //mResult.setText(tres);

}


My question is that is it the right way of coding to give call back or it causes tight coupling issue using below code.

if (baseClass instanceof MainActivity)
            ((MainActivity) baseClass).onResultNotFound(tres);


Interface:

public interface IResultListener {

    /*
    * This is call back listener when acr starts recognition sound of audio/video
    * and methods are available according to result
    * */

    // result found successfully
    void onResultFound(String result);

    // result not found
    void onResultNotFound(String result);

    // some exception occurred
    void onErrorFound(String result);
}
Instead of casting to MainActivity, you should be casting to IResultListener.

if (baseClass instanceof IResultListener)
            ((IResultListener) baseClass).onResultNotFound(tres);


That said, usually one registers a listener. So you'd have a List&lt;IResultListener&gt; somewhere, and then in this method you'd call a method fireEventResultFound(String result), which iterates over the list and calls onResultFound for each of them. This is called the Observer pattern (you can google that if you want to know more), and it decouples the reporting class from the observing class.

I also think you should move the parsing of hummings and musics, streams and custom_files to their own methods. That would greatly simplify this onResult method.



Also,

tres = tt.getString("acr_id");


Is this a possible bug? Everywhere else you're appending, but here you just overwrite the result.