Office Hours — Today, June 18

Monday, June 16

Jun 18
9:50 AM
Mark M.
has entered the room
9:55 AM
Mark M.
turned on guest access
Guy W.
has entered the room
Guy W.
Hey
Mark M.
hello, Guy!
how can I help you today?
Guy W.
how are you doing?
Mark M.
busy busy busy
Guy W.
:-)
View paste
trying to build a http client that uses Digest access authentication
10:00 AM
Guy W.
REST
EGHDK
has entered the room
Mark M.
(EGHDK: hi! I'll be with you shortly!)
Guy: OK... but what's your question?
Guy W.
could you recommend a tutorial to implement this?
Mark M.
off the cuff, no, because I have never used it
Guy W.
OK thanks
Mark M.
sorry
EGHDK: do you have a question?
EGHDK
okay so back to my last question from Monday. You said that "signed integers" means that they can be negative. But why is a byte in Java range from -128 to 127? Wouldn't it make sense if it was the other way around -127 to 128?
Mark M.
you would have to ask the people who wrote Java
10:05 AM
Mark M.
Guy: do you have another question?
EGHDK
Hm. Okay, thought that there may be a specific reason for that.
Mark M.
EGHDK: there may be, but I do not know what it is. I can confidently state that in this chat, I have thought more about this issue than in the rest of my life combined. :-)
Guy W.
at the moment just the "Digest access authentication"
Mark M.
what HTTP client API are you using? HttpUrlConnection? HttpClient? a third-party HTTP library?
Guy W.
HttpClient
Mark M.
though personally HttpClient is your worst choice for client API, simply because Google is not maintaining it anymore
10:10 AM
Mark M.
(on Android -- Apache is maintaining their own copy, but Android's is much older)
if either of you have more questions, go right ahead
EGHDK
Yep. Got my list ready.
Guy W.
so I should use HttpsURLConnection instead
Mark M.
Guy: or a third-party library layered atop of HttpsUrlConnection, yes
EGHDK
View paste
(int) (Math.random() * 6) gives 0-5 but (int) Math.random() * 6 gives 0. Why?
Mark M.
because you are casting a float value from 0 to 1 (Math.random()) to an int
and that cast does not round IIRC
10:15 AM
EGHDK
When reading about jars trying to figure out what they "really" are... "You can put your application files into a Java Archive - .jar file - thats based on the pkzip format. The jar file can include a simple text file formatted as something called the manifest that defines which class in the jar holds the main method that should run." Does this mean an Android project is just a jar file? Because isn't an android project zipped up with a manifest?
Mark M.
"Does this mean an Android project is just a jar file?" -- no, just as a Word document in a ZIP file is not a JAR file
JARs, APKs, and Word-docs-in-ZIP-files are all ZIP files
their contents differ
APKs *resemble* JARs more than Word-docs-in-ZIP-files do
but they are not the same
EGHDK
So out of the Android world over here... if I wanted to make a java program and send it to someone, and the program used 5 classes... I could just package it as a jar?
Mark M.
yes
10:20 AM
EGHDK
So a lot of the libraries you make are simply jar files. Like mergeadapter and stuff. So are just compressed classes?
Mark M.
compiled classes
off the cuff, I forget whether JARs use actual ZIP compression or not
EGHDK
Got it. So there are compiled... so you get the .class and not the .java?
10:25 AM
Mark M.
correct
EGHDK
So if I downloaded a .jar file I'm not able to actually edit the code in there?
Mark M.
not directly
EGHDK
But I can see the code... right?
So I can edit it... indirectly by just copying it into a different file?
Mark M.
"But I can see the code... right?" -- no, because it is compiled, not the raw source code
EGHDK
Hmm... got it. So how would you edit it "indirectly" like you said?
Mark M.
reverse engineering is way out of scope for this chat
EGHDK
Okay. I just had to know if it was reverse engineering or some other way. Thanks
10:30 AM
Mark M.
Guy: if you have other questions, feel free to chime in
EGHDK
View paste
Okay, my next question is actually android related. Lately I've been having to use LayoutParams a lot. I never know which LayoutParams to use (import) though.
Mark M.
the ones for your parent container
if you are adjusting the LayoutParams for a widget in a RelativeLayout, you will be using RelativeLayout.LayoutParams
10:35 AM
EGHDK
Hmm... okay that makes sense. Why does anyone have to ever use LayoutParams itself?
Mark M.
there is no "LayoutParams" itself
RelativeLayout.LayoutParams, for example, inherits from ViewGroup.MarginLayoutParams, which inherits from ViewGroup.LayoutParams
some container classes can get by with using one of those inner classes of ViewGroup, because they did not need any extra rules
EGHDK
Gotcha. Okay. sounds good.
10:40 AM
EGHDK
View paste
I tried this:
ProgressDialog dialog = new ProgressDialog(MyActivity.this);
				dialog.setMessage("Saving");
				dialog.show();
				try {
					Thread.sleep(500);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
Looking at it... logically I would feel like the Saving dialog would show for half a second... but it doesn't. The dialog shows afterwards. I don't understand how thats possible.
Mark M.
because show() is asynchronous
your dialog will not show until sometime after you return control to the main application thread
10:45 AM
EGHDK
show is async? But, it's on the main thread... no?
Mark M.
you are *calling* show() on the main application thread
all this does is schedule work to be done later
EGHDK
Sorry... this is going to take some explaining because this blows my mind.
Mark M.
just like startActivity() is asynchronous
EGHDK
isn't every method "scheduled"? Isn't that what the stack is?
Mark M.
no and no
let's draw an analogy
IIRC, you are college
sorry, you are in college
when a professor assigns you a term paper, or some other sort of project, does the professor expect you to hand in the result in the next few seconds?
or does the professor give you a submission date in the future?
most professors will give you a submission date in the future, so you have time to actually do the work
however, if the professor asks you a *question*, they expect an immediate answer
not everything that a professor tells you to do, therefore, is stuff that is done immediately -- only some things are
this is no different in computer programming
10:50 AM
Mark M.
some things your program asks to have done will be done immediately
some things will be arranged to happen in the future
sometimes, "the future" is a scheduled point in time (see AlarmManager in Android)
sometimes, "the future" is in the form of a work queue (see the main application thread in Android)
and there are other patterns
in this case, the constructor creates the object immediately
and setMessage() configures the object immediately
and show() puts the request to display the dialog onto a work queue
the main application thread's primary role is process requests on that work queue
which is why we need to spend as little time as possible in our own code on the main application thread
and a side effect is that if you call some method that simply puts work on the work queue, that work will not even *begin* until you return control of the main application thread to the framework (by returning from whatever callback that you are in), so the framework code can return to processing work off of that queue
10:55 AM
EGHDK
Okay. So how would I figure out that a method like show() or startActivity() are methods that get put on the work queue? I don't see anything in the docs about it.
Mark M.
experience
the documentation does not cover everything
EGHDK
Not the answer I was looking for -_- Hmm.. Yeah I guess. It's just like the show method says "Start the dialog and display it on screen." so it makes it seem like it will drop everything and do that now.
So now for a solution... how would I rewrite that progress dialog so that The dialog shows for 5 seconds with thread.sleep()?
Put it into an async task?
onpreExecute create the dialog and show it, onBackground Thread.Sleep, onPostExecute hide progress dialog?
Mark M.
you could use postDelayed() on some View to schedule a Runnable to run in five seconds, where the Runnable cancels the dialog
EGHDK
postDelayed() on a view? Never heard of that before... Heard of a runnable... but haven't looked into what it is yet.
Mark M.
it's covered in my book
in a few places, actually
11:00 AM
EGHDK
Okay, I put your book onPause() for now because of your update. I'm still trying to brush up on my java fundamentals.
Alright, well it looks like your scheduled for 7:30 so I'll be there tonight with more questions... Thanks mark!
Mark M.
yes, the next chat is in a bit over 8 hours
this chat's transcript will be posted to http://commonsware.com/office-hours/ shortly
have a pleasant day, all!
Guy W.
has left the room
EGHDK
has left the room
Mark M.
turned off guest access

Monday, June 16

 

Office Hours

People in this transcript

  • EGHDK
  • Guy West
  • Mark Murphy