unable to get metadate title on hebrew (get it on gibberish) - java

I'm developing android applications
When doing a code to get streaming title "now loading" i unable to recieve the title on hebrew
but i recieved him on gibberish
if someone can help me with this i will be a greatful
enter image description here
#Override
protected IcyStreamMeta doInBackground(URL... urls)
{
try
{
streamMeta.refreshMeta();
Log.e("Retrieving MetaData","Refreshed Metadata");
}
catch (IOException e)
{
Log.e(MetadataTask2.class.toString(), e.getMessage());
}
return streamMeta;
}
#Override
protected void onPostExecute(IcyStreamMeta result)
{
try
{
title_artist=streamMeta.getTitle();
Log.e("Retrieved title_artist", title_artist);
if(title_artist.length()>0)
{
textView.setText(title_artist);
}
}
catch (IOException e)
{
Log.e(MetadataTask2.class.toString(), e.getMessage());
}
}
}
class MyTimerTask extends TimerTask {
public void run() {
try {
streamMeta.refreshMeta();
} catch (IOException e) {
e.printStackTrace();
}
try {
String title_artist=streamMeta.getTitle();
Log.i("ARTIST TITLE", title_artist);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

It looks like IcyMetaData simply casts raw bytes to char (effectively doing ISO-8859-1 encoding instead of using detecting whatever the server sends) at line 149:
metaData.append((char) b);
I don't see a way to fix this without patching/fixing the IcyMetaData class.

Related

How to access Plurals in Java code for Harmony OS?

I have defined some strings which I can access using Resource Table. How can I define and use Plurals similarly?
Are you asking how to use the plural.json resource file? If so, you can refer to the following code:
1.plural.json sample code
{
"plural":[
{
"name":"eat_apple",
"value":[
{
"quantity":"one",
"value":"%d apple"
},
{
"quantity":"other",
"value":"%d apples"
}
]
}
]
}
2.The following is an example of using the MainAbilitySlice.java file:
public class MainAbilitySlice extends AbilitySlice {
private static final HiLogLabel LOG_LABEL = new HiLogLabel(3, 0xD001100, "MainAbilitySlice");
#Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_main);
String pluralString = null;
try {
pluralString = getResourceManager().getElement(ResourceTable.Plural_eat_apple).getPluralString(1, 1);
HiLog.info(LOG_LABEL, pluralString);
pluralString = getResourceManager().getElement(ResourceTable.Plural_eat_apple).getPluralString(2, 10);
HiLog.info(LOG_LABEL, pluralString);
} catch (IOException e) {
e.printStackTrace();
} catch (NotExistException e) {
e.printStackTrace();
} catch (WrongTypeException e) {
e.printStackTrace();
}
}
}

How to extract try catch outside method?

I have this method sendParameterValueAsMQTTMessage() which I use to publish message via MQTT on a specific topic. I am using try catch two times after another (not nested) but it still seems somewhat ugly and overcrowding the method. I read an article on clean code where Uncle Bob talks about extracting the body of try catch but I seem to not grasp it quite well or at least not in my case.
How could I get rid of the try catch in my method by extracting it outside?
public void sendParameterValueAsMQTTMessage() {
String payload = null;
try {
payload = convertToJSONString("range", String.valueOf(range));
} catch (JSONException e) {
this.logger.log(Level.ERROR, e);
}
MQTTMessage message = new MQTTMessage(MQTTTopics.RANGE_TOPIC,payload,0);
try {
this.client.publish(message);
Thread.sleep(3000);
} catch (Exception e) {
this.logger.log(Level.ERROR, e);
}
}
there are multiple different problems with provided code, here is how I'd refactor it:
public void sendParameterValueAsMQTTMessage() {
final String payload = tryGetPayloadAsJson();
if (payload != null) {
trySendPayloadViaMQTT(payload);
}
}
private String tryGetPayloadAsJson() {
try {
return convertToJSONString("range", String.valueOf(range));
} catch (JSONException e) {
this.logger.log(Level.ERROR, e);
}
return null;
}
private void trySendPayloadViaMQTT(final String payload) {
try {
final MQTTMessage message = new MQTTMessage(MQTTTopics.RANGE_TOPIC, payload, 0);
this.client.publish(message);
Thread.sleep(3000);
} catch (Exception e) {
this.logger.log(Level.ERROR, e);
}
}
one thing which might be improved here based on Uncle Bob's advice is to actually move try/catch outside of trySendPayloadViaMQTT, like this:
public void sendParameterValueAsMQTTMessage() {
final String payload = tryGetPayloadAsJson();
if (payload != null) {
trySendPayloadViaMQTT(payload);
}
}
private String tryGetPayloadAsJson() {
try {
return convertToJSONString("range", String.valueOf(range));
} catch (JSONException e) {
this.logger.log(Level.ERROR, e);
}
return null;
}
private void trySendPayloadViaMQTT(final String payload) {
try {
sendPayloadViaMQTT(payload);
} catch (Exception e) {
this.logger.log(Level.ERROR, e);
}
}
private void sendPayloadViaMQTT(final String payload) {
final MQTTMessage message = new MQTTMessage(MQTTTopics.RANGE_TOPIC, payload, 0);
this.client.publish(message);
Thread.sleep(3000);
}
you can put all of your code in just one try block and set multiple catches, when ever an exception be happened, the catch that is revelated to it will be execute, like:
try{
int a[]=new int[5];
a[5]=30/0;
}
catch(ArithmeticException e)
{
System.out.println("Arithmetic Exception occurs");
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("ArrayIndexOutOfBounds Exception occurs");
}
You can use single general catch for both possible exceptions inside the method as following:
public void sendParameterValueAsMQTTMessage() {
String payload = null;
try {
payload = convertToJSONString("range", String.valueOf(range));
MQTTMessage message = new MQTTMessage(MQTTTopics.RANGE_TOPIC,payload,0);
this.client.publish(message);
Thread.sleep(3000);
} catch (Exception e) {
this.logger.log(Level.ERROR, e);
}
}
public void sendParameterValueAsMQTTMessage() {
String payload = null;
try {
payload = convertToJSONString("range", String.valueOf(range));
} catch (JSONException e) {
this.logger.log(Level.ERROR, e);
}
MQTTMessage message = new MQTTMessage(MQTTTopics.RANGE_TOPIC,payload,0);
publishMessage(message); //extracted in a new method
}
public void publishMessage(MQTTMessage message){
try {
this.client.publish(message);
Thread.sleep(3000);
} catch (Exception e) {
this.logger.log(Level.ERROR, e);
}
}

How to increase record and play volume with Java sound?

I use the following code to record and play sounds with Java, but the volume is too low, how to make it louder, at least 2,3 times louder ?
public void Record_Audio(String File_Path,AudioFileFormat.Type File_Type)
{
try
{
audioFormat=getAudioFormat(); // Get things set up for capture
DataLine.Info dataLineInfo=new DataLine.Info(TargetDataLine.class,audioFormat);
targetDataLine=(TargetDataLine)AudioSystem.getLine(dataLineInfo);
//Create a thread to capture the microphone data into an audio file and start the thread running. It will run
// until the Stop button is clicked. This method will return after starting the thread.
new Record_Thread(File_Path,File_Type).start();
}
catch (Exception e)
{
System.out.println(e);
System.exit(0);
}
}
private void Play_Audio_Recording()
{
File Audio_File=new File(Current_Folder_Path+File_Name_ComboBox.getSelectedItem().toString().trim()+"."+Get_Audio_File_Type());
try
{
Audio_Clip=Applet.newAudioClip(Audio_File.toURI().toURL());
Audio_Clip.play();
}
catch (Exception e) { e.printStackTrace(); }
}
class Record_Thread extends Thread
{
String File_Path;
AudioFileFormat.Type File_Type;
Record_Thread(String File_Path) { this(File_Path,AudioFileFormat.Type.WAVE); }
Record_Thread(String File_Path,AudioFileFormat.Type File_Type)
{
this.File_Path=File_Path;
this.File_Type=File_Type;
}
public void run()
{
Audio_File=new File(File_Path);
try
{
targetDataLine.open(audioFormat);
targetDataLine.start();
AudioSystem.write(new AudioInputStream(targetDataLine),File_Type,Audio_File);
}
catch (Exception e) { e.printStackTrace(); }
}
}
You would use the FloatControl.Type (https://docs.oracle.com/javase/7/docs/api/javax/sound/sampled/FloatControl.Type.html) to set either the volume or the master gain. Something like:
targetDataLine=(TargetDataLine)AudioSystem.getLine(dataLineInfo);
javax.sound.sampled.FloatControl c = (FloatControl)targetDataLine.getControl(FloatControl.Type.VOLUME);
c.setValue(c.getMaximum());
might work.

Function into fragment stop refreshing after change tab

I have an app with 2 tabs where the first include somes values which are refreshed during the time. I take these values from a PhoneStateListener activity and I transfer them into the fragment with the function below. The problem is, when I move to 2nd tab and after that move back to first the values are not refresed anymore.
public void test(int LteSignalStrength,int LteRsrp,int LteRsrq,int LteRssnr,int LteCqi){
if(ntype=="\nNetwork Type: LTE\n"){
tv2.setText("\nLteSignalStrength:"+dbm+" dbm"+
"\nLteRsrp: "+LteRsrp+
"\nLteRsrq: "+LteRsrq+
"\nLteRssnr: "+LteRssnr);
}
}
PhonestateListener Activity
public Tab1Signal(Tab1Values fragment){
mcontext = fragment.getContext();
main=fragment;
}
public void onSignalStrengthsChanged(SignalStrength signalStrength){
super.onSignalStrengthsChanged(signalStrength);
try {
Method[] methods = android.telephony.SignalStrength.class.getMethods();
for (Method mthd : methods) {
if (mthd.getName().equals("getLteSignalStrength")){
//val1=mthd.getName() ;
LteSignalStrength=(Integer)mthd.invoke(signalStrength);
//main.test(val2);
}
if (mthd.getName().equals("getLteRsrp")){
LteRsrp=(Integer)mthd.invoke(signalStrength);
}
if (mthd.getName().equals("getLteRsrq")){
LteRsrq=(Integer)mthd.invoke(signalStrength);
}
if (mthd.getName().equals("getLteRssnr")){
LteRssnr=(Integer)mthd.invoke(signalStrength);
}
if (mthd.getName().equals("getLteCqi")){
LteCqi=(Integer)mthd.invoke(signalStrength);
}
main.test(LteSignalStrength,LteRsrp,LteRsrq,LteRssnr,LteCqi);
}
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
Does anyone know how can I solve it?
You can pass listener in method onTabChanged when tab contain Tab1Signal

Non-Static Generic function that invokes a method passed in as a parameter

I'm trying to consolidate 2 methods into 1, because they handle exceptions the same way. I know in C# you can pass functions/actions as parameters into other functions. I tried creating a generic method to invoke a function, but can't seem to figure it out.
public String getTheStuff(String client) {
try {
return extService.getProduct(client);
} catch (UIException e) {
notHealthy();
} catch (HostException e) {
notHealthy();
} catch (Exception e) {
Throwables.propagate(e);
}
}
public CustomType getsomeMoreStuff(String source, int offset) {
try {
return extService.getMetrics(source, offset);
} catch (UIException e) {
notHealthy();
} catch (HostException e) {
notHealthy();
} catch (Exception e) {
Throwables.propagate(e);
}
}
What I'm looking for is something like
public T invokeExtService(Function functionToInvoke, Parameters[] params){
try {
return functionToInvoke.Invoke(params);
} catch (UIException e) {
notHealthy();
} catch (HostException e) {
notHealthy();
} catch (Exception e) {
Throwables.propagate(e);
}
}
As #LouisWasserman said, this would be much nicer in Java 8, but how about something like this (untested):
public <T> T invoke(Callable<T> function) {
try {
return function.call();
} catch (UIException e) {
notHealthy();
} catch (HostException e) {
notHealthy();
} catch (Exception e) {
Throwables.propagate(e);
}
}
public String getTheStuff(final String client) {
return invoke(new Callable<String>() {
#Override
public String call() {
return extService.getProduct(client);
}
});
}
public CustomType getsomeMoreStuff(final String source, final int offset) {
return invoke(new Callable<CustomType>() {
#Override
public CustomType call() {
return extService.getMetrics(source, offset);
}
});
}
To be honest, I'm not sure how worthwhile this is considering how short your methods are (and they could be even shorter with multi-catch).

Categories

Resources