I think that the best way for me to start this post is to explain what
I'm doing. I'm learning the Android platform because I want to learn
how to program in Java (I'll need it for a class next year), and I
reckon that the best way of doing this is for me to write simple apps.
I have spent the last year programming in python so I know basic
concepts, but I am teaching myself the most of it as I go along. This
is the first time I have ventured outside of the relative safety of
the book I am 'learning' from (in all honesty, I find these books
better as references than actual tutorials).

I am currently stuck on a certain part of an app I am writing. What
the app is meant to do is grab a string from a dynamically generated
text file (over the http protocol). This is then displayed on the
screen. I tend to learn best by example, so I have been looking for
examples of opening http resources. I have started with a book I am
learning from (Beginning Android) and a snippet on stackoverflow.
These two 'examples' are
http://stackoverflow.com/questions/1959676/httpget-in-android-unresolvedhostexception/1960768
and
http://github.com/commonsguy/cw-android/blob/a5108d95e07eb79807f865c21bcee005b9dac917/Internet/Weather/src/com/commonsware/android/internet/WeatherDemo.java
which are either more complicated, complete ideas or are tiny samples.
I have botched together some code, but it is not working, and whilst I
am fairly sure why, I'm not entirely sure how I could remedy it. This
is my source code at the moment for anyone interested:

As you can see, I'm having some trouble with the whole http side of
things. I am not looking for an explanation here or any sort of fix to
my code (that would be rude for me to ask), more a list of places
where I have gone wrong, and perhaps another example of a similar
action to mine. I am new to Java in general, and whilst I am a fast
learner at these sorts of things, I am still firmly in the 'noob'
zone, and being spoon-fed in any way is not great for learning! My
development device is API 3, if that changes anything.

package com.bakes.ChuckNorrisFacts;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;

public class ChuckNorrisFacts extends Activity
        implements View.OnClickListener {
            HttpEntity httpEntity;
                Button btn;
                TextView text;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        btn=(Button)findViewById(R.id.updatebutton);
        text=(TextView)findViewById(R.id.factbox);
        updateFact();
        }
    public void onClick(View view){
        updateFact();
    }
    private void updateFact() {
        String url = "http://www.site.com/norris.php";;
        HttpClient client = new DefaultHttpClient();
        HttpGet getMethod=new HttpGet(url);
        HttpResponse httpResponse = client.execute(getMethod);
        httpEntity = httpResponse.getEntity();
        try {
                ResponseHandler<String> responseHandler = new
BasicResponseHandler();
                String responseBody=client.execute(getMethod, responseHandler);
                text.setText(httpEntity.getContent());
        }
        catch (Throwable t) {
                text.setText("Chuck Norris doesn\'t need internet access. You
do, so make sure you are connected and try again.");
        }
    }
}
-- 
You received this message because you are subscribed to the Google
Groups "Android Beginners" group.

NEW! Try asking and tagging your question on Stack Overflow at
http://stackoverflow.com/questions/tagged/android

To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-beginners?hl=en

Reply via email to