Internet recovered string in textview not working

Problems with WiFi, SQLite ,Bluetooth, WiMax, Proxies, etc...

Internet recovered string in textview not working

Postby geekyhawkes » Thu Oct 25, 2012 10:30 am

Hey all, so im new to android dev / java but not new to programming and am pulling my hair out over something i thought would be simple.... Following the guide here I want to load a text file from the internet (preferably dropbox..) and extract the contents to a string. I want to then use this string to populate a textview on the activity.

The code i am using is:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.nexgen.threedashone.trainer;
  2.  
  3. import android.app.Activity;
  4. import android.content.Intent;
  5. import android.os.Bundle;
  6. import android.support.v4.app.NavUtils;
  7. import android.view.MenuItem;
  8. import android.view.View;
  9. import android.widget.Button;
  10. import android.widget.TextView;
  11.  
  12. //import stuff for web files
  13.  
  14. import java.io.BufferedInputStream;
  15. import java.io.InputStream;
  16. import java.net.URL;
  17. import java.net.URLConnection;
  18.  
  19. import org.apache.http.util.ByteArrayBuffer;
  20.  
  21.  
  22. public class AboutScreen extends Activity {
  23.     @Override
  24.     public void onCreate(Bundle savedInstanceState) {
  25.         super.onCreate(savedInstanceState);
  26.         setContentView(R.layout.activity_about_screen);
  27.         getActionBar().setDisplayHomeAsUpEnabled(true);
  28.        
  29.         // button handler for return to mainmenu activity
  30.        
  31.         Button next = (Button) findViewById(R.id.button1);
  32.         next.setOnClickListener(new View.OnClickListener() {
  33.             public void onClick(View view) {
  34.                Intent intent = new Intent();
  35.                setResult(RESULT_OK, intent);
  36.               finish();
  37.             }
  38.           });
  39.        
  40.         //data handler from the web
  41.         /* We will show the data we read in a TextView. */
  42.         // TextView tv = new TextView(this);
  43.      
  44.         /* Will be filled and displayed later. */
  45.         String version = null;
  46.         try {
  47.                 /* Define the URL we want to load data from. */
  48.                 URL myURL = new URL(
  49.                                 "http://www.anddev.org/images/tut/basic/getdatafromtheweb/loadme.txt");
  50.                                                 // "http://db.tt/HGh2uzgf");
  51.                 /* Open a connection to that URL. */
  52.                 URLConnection ucon = myURL.openConnection();
  53.  
  54.                 /* Define InputStreams to read
  55.                  * from the URLConnection. */
  56.                 InputStream is = ucon.getInputStream();
  57.                 BufferedInputStream bis = new BufferedInputStream(is);
  58.                
  59.                 /* Read bytes to the Buffer until
  60.                  * there is nothing more to read(-1). */
  61.                 ByteArrayBuffer baf = new ByteArrayBuffer(50);
  62.                 int current = 0;
  63.                 while((current = bis.read()) != -1){
  64.                         baf.append((byte)current);
  65.                 }
  66.  
  67.                 /* Convert the Bytes read to a String. */
  68.                 version = new String(baf.toByteArray());
  69.         } catch (Exception e) {
  70.                 /* On any Error we want to display it. */
  71.                 version = e.getMessage();
  72.         }
  73.         /* Show the String on the GUI. */
  74.         TextView textview = (TextView) findViewById(R.id.tvversion);
  75.         textview.setText(version);
  76.        
  77.        
  78.     }  
  79.     @Override
  80.     public boolean onOptionsItemSelected(MenuItem item) {
  81.         switch (item.getItemId()) {
  82.             case android.R.id.home:
  83.                 NavUtils.navigateUpFromSameTask(this);
  84.                 return true;
  85.         }
  86.         return super.onOptionsItemSelected(item);
  87.     }
  88.  
  89. }
  90.  
Parsed in 0.043 seconds, using GeSHi 1.0.8.4


I think that should work, but i dont see any result from the textview (named tvversion). If i change the code slightly and remove the variable to just text textview.setText("version"); then the textview is populated with the phrase version as you expect, so i guess my code isnt far off?

I have added <uses-permission android:name="android.permission.INTERNET"></uses-permission> to the manifest, but still no luck.

Any help (with some explanation if possible) would be well received. Do I need to do anything extra when i comes to using the dropbox hosted file as well, or just change the URL?

Thanks;

Andy
geekyhawkes
Freshman
Freshman
 
Posts: 3
Joined: Thu Oct 25, 2012 9:11 am

Top

Re: Internet recovered string in textview not working

Postby geekyhawkes » Thu Oct 25, 2012 12:18 pm

EDIT: I just noticed that i didnt have any "" in the textview.setText(version) I edited my code to say:

textview.setText("" + version); and now at least the textview is displayed in the app. The next problem is that it only displays null and not the string from the URL. Anyone help?
geekyhawkes
Freshman
Freshman
 
Posts: 3
Joined: Thu Oct 25, 2012 9:11 am

Top

Return to Networking & Database Problems

Who is online

Users browsing this forum: No registered users and 6 guests