1
\$\begingroup\$

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 < 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 < 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 < 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 < 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);
}
Pimgd
22.5k5 gold badges68 silver badges144 bronze badges
asked Jun 29, 2016 at 6:15
\$\endgroup\$
3
  • \$\begingroup\$ You're not using interfaces at all - look up "java interface" and "implements interface" \$\endgroup\$ Commented Jun 29, 2016 at 7:12
  • \$\begingroup\$ I AM using interface. onResultFound, onResultNotFound are the methods belong to interface used in exception clause and if and else clause.As mentioned 'MainActivity' this is the class implementing my interface. \$\endgroup\$ Commented Jun 29, 2016 at 7:26
  • \$\begingroup\$ can you add the interface definition to your code in the question? And the tres and baseClass variables? \$\endgroup\$ Commented Jun 29, 2016 at 7:27

1 Answer 1

2
\$\begingroup\$

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

answered Jun 29, 2016 at 7:34
\$\endgroup\$
3
  • \$\begingroup\$ No that is not a bug. I just want to get id on that case and in rest of the case I am parsing complete json. \$\endgroup\$ Commented Jun 30, 2016 at 4:40
  • \$\begingroup\$ @AbdulWaheed if it is not a bug, then at that point, you're done parsing and could stop the entire parse function \$\endgroup\$ Commented Jun 30, 2016 at 7:26
  • \$\begingroup\$ yes, you are right I am doing some extra work just for nothing. Thank you for mentioning that :) \$\endgroup\$ Commented Jun 30, 2016 at 8:00

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.