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);
}
1 Answer 1
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.
-
\$\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\$Abdul Waheed– Abdul Waheed2016年06月30日 04:40:58 +00:00Commented 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\$Pimgd– Pimgd2016年06月30日 07:26:58 +00:00Commented 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\$Abdul Waheed– Abdul Waheed2016年06月30日 08:00:28 +00:00Commented Jun 30, 2016 at 8:00
tres
andbaseClass
variables? \$\endgroup\$