package com.yourpackage.name;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import org.apache.http.util.ByteArrayBuffer;
import android.app.Activity;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;
public class DynamicContent extends Activity {
TextView txtView;
String textUrl = "http://www.yourdomain.com/your_text_file.txt";
ImageView imgView;
String imageUrl = "http://www.yourdomain.com/your_image.png";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
// Hide title bar.
getWindow().requestFeature(Window.FEATURE_NO_TITLE);
// Set content view
setContentView(R.layout.main);
// Get views
txtView = (TextView) findViewById(R.id.content_text);
imgView = (ImageView) findViewById(R.id.content_image);
// Download content
downloadText(textUrl, txtView);
downloadImage(imageUrl, imgView);
}
// Function to download text from given URL, and parse it to desired TextView
void downloadText(String fileUrl, TextView v){
URL myFileUrl = null;
try {
myFileUrl= new URL(fileUrl);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
/* Read bytes to the Buffer until
* there is nothing more to read(-1). */
ByteArrayBuffer baf = new ByteArrayBuffer(50);
int current = 0;
while((current = bis.read()) != -1){
baf.append((byte)current);
}
/* Convert the Bytes read to a String. */
v.setText(new String(baf.toByteArray()));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// Function to download image from given URL, and parse it to desired ImageView
void downloadImage(String fileUrl, ImageView v) {
URL myFileUrl = null;
try {
myFileUrl= new URL(fileUrl);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
v.setImageBitmap(BitmapFactory.decodeStream(is));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}