how to log an integer value with log.d? - java

I am very new to Android and am trying some simple log to get a random background color. I have the code and it returns an integer between 1-256, or so I think. I need to log the value to check if it's OK, but I'm not sure how to log it with Android.. I've been using System.out.println("stuff") to log stuff in the past but I believe that's not how you're supposed to do it in Android.
I have my class:
public static int backgroundColorRandomize()
that returns
return randomRGB;
and I try to log it like this
Log.d(backgroundColorRandomize(), "value = " + randomRGB);
but I need to convert the returned value from backgroundColorRandomize to a String in order for it to log.
I tried java's .toString but I'm not sure I'm using it right.. Any help would be appreciated! Thanks!

Log.d("MYINT", "value: " + randomRGB);

private static final String TAG = YourClass.class.getSimpleName();
...
android.util.Log.d(TAG, String.format("value = %d. random color = %d", randomRGB, backgroundColorRandomize()));
More info:
http://developer.android.com/reference/android/util/Log.html
https://developer.android.com/tools/debugging/debugging-log.html
Logging libraries: https://android-arsenal.com/tag/57

I prefer String.valueOf(value).
Log.d(String.valueOf(backgroundColorRandomize()), "value = " + randomRGB);

Log.d(backgroundColorRandomize() + "" /* <-- all you need. */, "value = " + randomRGB);

I use
Log.d("MYINT", "value: " + randomRGB.toString());

Related

reduce() not working with lightcouch

I have wirtten a program to manage tv series and I am stuck at an issue with lightcouch and a specific database query. This is what I have so far. To setup the database views I used the following lines:
MapReduce get_numberOfSeasonsMR = new MapReduce();
get_numberOfSeasonsMR.setMap(
"function(doc) { "
+ " emit(doc.seriesName, doc.season)"
+ "}");
get_numberOfSeasonsMR.setReduce(
"function (key, values, rereduce) {"
+ "return Math.max.apply({}, values)"
+ "}");
map.put("get_numberOfSeasons", get_numberOfSeasonsMR);
In Futon everything appears normal (see http://i.stack.imgur.com/1hgSJ.png).
However, when I try to execute the following line, I get an exception, instead of the results that appear in Futon.
int nr = client.view("design/get_numberOfSeasons").key("Arrow").queryForInt();
Exception:
org.lightcouch.NoDocumentException: Expecting exactly a single result of this view query, but was: 0
org.lightcouch.View.queryValue(View.java:246)
org.lightcouch.View.queryForInt(View.java:219)
....db.Server.getNumberOfSeasons(Server.java:237)
...
I tried to emit Strings in my map() function instead on ints, but it did not make any difference. What am I doing wrong? Or can someone post an example of a successful lightcouch map()+reduce() operation? The tutorials I found only used map() without reduce().
Thanks in advance ;)
Nothing seems wrong with your code, here is the full version:
CouchDbClient dbClient = new CouchDbClient();
DesignDocument designDocument = new DesignDocument();
designDocument.setId("_design/mydesign");
designDocument.setLanguage("javascript");
MapReduce get_numberOfSeasonsMR = new MapReduce();
get_numberOfSeasonsMR.setMap(
"function(doc) { "
+ " emit(doc.seriesName, doc.season)"
+ "}");
get_numberOfSeasonsMR.setReduce(
"function (key, values, rereduce) {"
+ "return Math.max.apply({}, values)"
+ "}");
Map<String, MapReduce> view = new HashMap<>();
view.put("get_numberOfSeasons", get_numberOfSeasonsMR);
designDocument.setViews(view);
dbClient.design().synchronizeWithDb(designDocument);
int count = dbClient.view("mydesign/get_numberOfSeasons").key("Arrow").queryForInt();

download cover art from musicbrainz with java

I am struggling for a couple of hours now on how to link a discid to a musicbrainz mbid.
So, using dietmar-steiner / JMBDiscId
JMBDiscId discId = new JMBDiscId();
if (discId.init(PropertyFinder.getProperty("libdiscid.path")))
{
String musicBrainzDiscID = discId.getDiscId(PropertyFinder.getProperty("cdrom.path"));
}
or musicbrainzws2-java
Disc controller = new Disc();
String drive = PropertyFinder.getProperty("cdrom.path");
try {
DiscWs2 disc =controller.lookUp(drive);
log.info("DISC: " + disc.getDiscId() + " match: " + disc.getReleases().size() + " releases");
....
I can extract a discid for freedb or musicbrainz easily (more or less), but I have not found a way on calculating the id I that I need to download cover art via the CoverArtArchiveClient from last.fm.
CoverArtArchiveClient client = new DefaultCoverArtArchiveClient();
try
{
UUID mbid = UUID.fromString("mbid to locate release");
fm.last.musicbrainz.coverart.CoverArt coverArt = client.getByMbid(mbid);
Theoretically, I assume, I could you the data collected by musicbrainzws2-java to trigger a search, and then use the mbid from the result ... but that cannot be the best option to do.
I am happy about any push into the right direction...
Cheers,
Ed.
You don't calculate the MBID. The MBID is attached on every entity you retrieve from MusicBrainz.
When getting releases by DiscID you get a list. Each entry is a release and has an MBID, accessible with getId():
for (ReleaseWs2 rel : disc.getReleases()){
log.info("MBID: " + rel.getId() + ", String: " + rel.toString());
}
You then probably want to try the CoverArtArchive (CAA) for every release and take the first cover art you get.
Unfortunately I don't know of any API documentation for musicbrainzws2 on the web. I recommend running javadoc on all source files.

phonegap displaying text in native textbox

I'm making an android app which uses bluetooth using phonegap and I'm having issues handling code because it get's called every 100ms.
I want to try if the issue occurs because my phonegap javascript code is to long causing delays.
To try this I want to dispaly the received message using a native textbox.
Is this possible? and How would I do this?
Thanks
Martijn
PS: I've tried using Toast but since I want to change the displayed value every 100ms this doesn't work.
Code
upon receiving a message this is the order the code is processed
bytes = mmInStream.read(buffer);
s_message = new String(buffer, 0, bytes);
String[] strArray = s_message.split(",");
mHandleReceivedThread = new HandleReceivedThread(strArray);
mHandleReceivedThread.start();
In the HandleReceivedThread:
if(strArrayReceived[0].startsWith("Pressure"))
{
if(strArrayReceived[1].contains("Pressure"))
{
double Pressure = Double.parseDouble(strArrayReceived[1].replace("Pressure", ""));
double PressureResult = Pressure/10;
String PressureValue = String.valueOf(PressureResult);
webView.sendJavascript("PressureValue = " + Pressure + ";");
webView.sendJavascript("document.getElementById('Pressure').innerHTML = " + PressureValue + ";");
Log.e(TAG, "Message fail: " + strArrayReceived[1] );
}
else
{
double Pressure = Double.parseDouble(strArrayReceived[1]
double PressureResult = Pressure/10;
String PressureValue = String.valueOf(PressureResult);
webView.sendJavascript("PressureValue = " + Pressure + ";");
webView.sendJavascript("document.getElementById('Pressure').innerHTML = " + PressureValue + ";");
}
}
this is basically what happens.
And this happens every 100ms , and for some reason the displaying of the Value has lag and sometimes the received message (which should be "Pressure,1500" , or a different value) is Pressure,1500Pressure , causing errors (which I've managed to fix). but this still isn't optimal.
The message is send correctly and if I use a simple chat app they are received correctly.
if there is anything else I should supply , just ask ;-)

String concatenation in java is not working on one system

I believe that most of you would be thinking that this is the same question you have heard multiple times (and answered ) about string concatenation in Java. But trust me, it is different. In fact, so different that I am even hesitant in posting it here. But anyways, here it is. I have some piece of code which goes like:
public void handleSuccess(String result)
{
result = result.trim();
MessageBox.alert("Information","Result after trimming: '" + result + "'");
result = result.substring(result.indexOf('\n') + 1);
MessageBox.alert("Information","Result after substring: '" + result + "'");
String returns = getReturns();
MessageBox.alert("Information","Returns: '" + returns + "'");
String action = getAction();
MessageBox.alert("Information","Action: '" + action + "'");
String finalResult = result + returns + action;
MessageBox.alert("Information","Final result: '" + finalResult + "'");
}
Now the situation here is that, all of these : getReturns(), result and getAction() return non blank values, and in fact the string finalResult contains the concatenated value after the last line is executed.
So, at Line 1, "result" contains "12/03/2013|04-AERTY|". The value of result remains same at end of line 1,2. getReturns() returns value 12.4724. So at end of line 3, finalResult contains "12/03/2013|04-AERTY|12.4724". getAction() returns "expt". So, at end of line 5, finalResult contains "12/03/2013|04-AERTY|12.4724|expt"
This is , when I debug or run the application in eclipse. As soon as build the same application on a UNIX system to generate a "war" file, and deploy the war on a tomcat server, the problem rears it's ugly head. When I run the application on the deployed war, the last line does not contain the concatenated value. So at the end of line 5, finalResult contains just "12/03/2013|04-AERTY|12.4724". I expected it to contain "12/03/2013|04-AERTY|12.4724|expt" as it does while running in eclipse.
I have tried stringbuffer, stringbuilder and the "+" operator as well, but nothing seems to work. I am not even getting an exception.
Can somebody help me in fixing this or at least enlightening me in what I might be doing wrong here?
Just to stress again, the code on eclipse(which is on a windows machine) and UNIX machine are exactly same. I have done a diff on them.
Here is what I get after putting the message-boxes:
Message-box 1: "Result after trimming: '12/03/2013|04-AERTY|'"
Message-box 2: "Result after substring: '12/03/2013|04-AERTY|'"
Message-box 3:"Returns: '12.4724'"
Message-box 4:"Action: '|expt'"
Message-box 5:"Final result: '12/03/2013|04-AERTY|12.4724|expt'"
Message-box 5 output is the one I receive when I execute code using eclipse
When running on deployed war, Message-box 1-4 have the same output as above, but Message-box 5 says: "Final result: '12/03/2013|04-AERTY|12.4724"
It's not clear where the extra "|" is meant to come from - if getAction() just returns expt, the result would be 12/03/2013|04-AERTY|12.4724|expt.
Anyway, I think it's safe to say that string concatenation will be working fine, and something else is wrong. You should add more diagnostics, logging everything:
public void handleSuccess(String result) {
result = result.trim();
log.info("Result after trimming: '" + result + "'");
result = result.substring(result.indexOf('\n') + 1);
log.info("Result after substring: '" + result + "'");
String returns = getReturns();
log.info("Returns: '" + returns + "'");
String action = getAction();
log.info("Action: '" + action + "'");
// It's not clear what this is meant to do. I suggest you remove it and
// use logging instead.
MessageBox.alert("Information", "The selected action is " + action, null);
String finalResult = result + returns + action;
log.info("Final result: '" + finalResult + "'");
I suspect you'll find that action is an empty string in the broken case.
Note that I've added quotes round each of the logged values, very deliberately. That means that if there's some unprintable character at the end of a string which causes problems, you should be able to detect that in the logging.
EDIT: As per the comment thread, when these were turned into message boxes (as it turns out this is running in GWT) it looks like there's something wrong with the early strings, as the closing ' isn't seen in diagnostics, in the broken case. The OP is going to investigate further.

Intent.getIntExtra() behaviour changes on a virtual device and on a real device

I have the below code and on my computer via Eclipse virtual device it works fine. But when installed on a real life phone it always reverts to the else statement.
This activity does not always get passed a value and if it is not I want a random record to appear. Thank you for any help or advice and time taken to read.
searchId = getIntent().getIntExtra("EMPLOYEE_ID", 0);
if(searchId > 0){
Query="SELECT * FROM " + DB_TABLE +" ORDER BY RANDOM() LIMIT 1";
Log.v("STANDARD RANDOM", "Was run");
}
else{
Query ="SELECT * FROM " + DB_TABLE +" WHERE _id=" + searchId + "";
Log.v("FROM SEARCH PAGE", "Was run");
}
Not sure if this fixes your problem. But according to the documentation, your code is not correct:
The name must include a package prefix, for example the app com.android.contacts would use names like "com.android.contacts.ShowAll".
Taken from Intent.putExtra.
You are using "EMPLOYEE_ID", without a package prefix, but you must include one!

Categories