Master Zangetsu
Master Zangetsu

Reputation: 262

Android, get text from url works fine in eclipse but not on device

As title suggests this code works fine in eclipse, but as soon as I export and try to use it only on the device it doesn't bring anything down and simply returns null.

It is supposed to read text from a url and display it in a text view

I have no idea what I'm doing wrong, its probably something fairly simple but I just cant see it

public class nextEvent extends Activity implements OnClickListener {

String Event;
TextView eventText;
TextView titleText;

String HTML;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.newevent);

    titleText = (TextView) findViewById(R.id.Title);
    eventText = (TextView) findViewById(R.id.Event);



            try { 
            getHTML();
        } catch (Exception e) {
            e.printStackTrace();
        }       
            eventText.setText("" + HTML);
    }


private void getHTML() throws ClientProtocolException, IOException 

{
    HttpClient httpClient = new DefaultHttpClient();
    HttpContext localContext = new BasicHttpContext();
    HttpGet httpGet = new HttpGet("http://masterzangetsu.eu/Apps/rocksoctest"); //URL!
    HttpResponse response = httpClient.execute(httpGet, localContext);
    String result = "";

    BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

    String line = null;
    while ((line = reader.readLine()) != null) {
        result += line + "\n";
        HTML = result;
    }

}


public void onClick(View v) {
    // TODO Auto-generated method stub


}

and heres the xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scroller"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true" >

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/Title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:text="Upcoming Events"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/Event"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/Title"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="44dp"
        android:text="Sever Event Update Failed" />

</RelativeLayout>

</ScrollView>

any help would be great

Upvotes: 0

Views: 414

Answers (2)

Bill Mote
Bill Mote

Reputation: 12823

Your emulator is probably running an older version of Android than your device and you're executing a networking call on the UI thread inside of a try-catch block that is hiding your error ;) The older emulator will work fine. Your device will not allow that. Use an AsyncTask{} to make your network call.

To illustrate this point get rid of your e.printStackTrace() and put a Log.e() in there that dumps to the LogCat.

Upvotes: 2

Blackbelt
Blackbelt

Reputation: 157487

You are performing an Http call on the UI Thread. Probably you set up your emulator with an older version of Android (pre 3.0) than your device (probably post 3.0). You should use Thread or AsyncTask to perform every potential blocking call

Upvotes: 1

Related Questions