Lab9 :
Reading values from Text file hosted in Tomcat Server over the network
Part 1
7. Replace the contents of activity_net2.xml with the file downloaded from OLIVE.
activity_net2.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/customise" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:id="@+id/tvNet2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/hello_world" /> <Button android:id="@+id/btnGetConfig" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Get Config" /> </LinearLayout>
Type the following for Net2Activity.java and correct all errors with Eclipse intellisense:
package mdad.lab9; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import android.os.AsyncTask; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.graphics.Color; import android.util.Log; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.LinearLayout; import android.widget.TextView; public class Net2Activity extends Activity { static int c1=255, c2=255, c3=255; //this statement is outside of onCreate method String[] s=new String[3]; TextView tv2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
setContentView(R.layout.activity_net2); tv2 = (TextView)findViewById(R.id.tvNet2); tv2.setTextColor(Color.BLUE); tv2.setText("Red:"+c1+" Green:"+c2+" Blue:"+c3); //Set background Color LinearLayout layout = (LinearLayout) findViewById(R.id.customise); layout.setBackgroundColor(Color.rgb(c1, c2, c3)); Button btnGetConfig = (Button)findViewById(R.id.btnGetConfig); btnGetConfig.setOnClickListener(new OnClickListener(){ public void onClick(View v){ new NetworkTask().execute("http://10.0.2.2:8080/MDAD/html/config.txt"); } }); } //AsyncTask inner class needed because of StrictMode to ensure network tasks are on separate threads private class NetworkTask extends AsyncTask<String, Void, String>{ protected String doInBackground(String... params) { String a = params[0]; try{ System.out.println("NetworkTask started……"); //print to logcat for debugging URL url = new URL(a); HttpURLConnection con = (HttpURLConnection)url.openConnection(); con.setRequestProperty("Accept", "text/plain"); BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream())); String line; int i=0; while((line=br.readLine())!=null) { //read values from config.txt in the server over the network s[i]=line; System.out.println(s[i]); i++; } Net2Activity.c1 = Integer.parseInt(s[0]);//convert to integer Net2Activity.c2 = Integer.parseInt(s[1]); Net2Activity.c3 = Integer.parseInt(s[2]); con.disconnect(); } catch(IOException e){ Log.e("error", "error in reading", e); } return "Red:"+s[0]+" Green:"+s[1]+" Blue:"+s[2]; } protected void onPostExecute(String result) { tv2.setTextColor(Color.RED); tv2.setText(result); LinearLayout layout = (LinearLayout) findViewById(R.id.customise); layout.setBackgroundColor(Color.rgb(c1, c2, c3)); }//end onPostExecute }//end of inner class NetworkTask @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.net2, menu); return true; } }//end of Net2Activity class
Part 2
activity_about.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".About" > <TextView android:id="@+id/tvAbout" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="16dp" android:layout_marginTop="23dp" android:text="TextView" /> </RelativeLayout>
Look at the onCreate method of NetworkingActivity.java.
You need to add one more menu item. Fill in the blank below:
package mdad.lab9; import java.util.SortedMap; import java.util.TreeMap; import android.os.Bundle; import android.app.Activity; import android.app.ListActivity; import android.content.Intent; import android.graphics.Color; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.LinearLayout; import android.widget.ListView; public class NetworkingActivity extends ListActivity { SortedMap<String, Object> actions = new TreeMap<String, Object>(); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); addMenuItem("Read text file from server", Net2Activity.class); addMenuItem("About Us", About.class); String[] keys = actions.keySet().toArray( new String[actions.keySet().size()]); setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, keys)); } public void addMenuItem(String label, Class<?> cls){ actions.put(label, new Intent(this, cls )); } protected void onListItemClick(ListView l, View v, int position, long id) { String key = (String) l.getItemAtPosition(position); startActivity((Intent) actions.get(key)); } }
4. Look at the onCreate method of NetworkingActivity.java. You need to add one more menu item. Fill in the blank below:
package mdad.lab9; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import android.os.AsyncTask; import android.os.Bundle; import android.app.Activity; import android.util.Log; import android.widget.TextView; public class About extends Activity { TextView tv3; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_about); tv3 = (TextView)findViewById(R.id.tvAbout); new AboutTask().execute("http://10.0.2.2:8080/MDAD/html/about.txt"); } private class AboutTask extends AsyncTask<String, Void, String>{ @Override protected String doInBackground(String... params) { String a = params[0], s=""; try{ URL url = new URL(a); HttpURLConnection con = (HttpURLConnection)url.openConnection(); con.setRequestProperty("Accept", "text/plain"); BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream())); String line; while((line=br.readLine())!=null) { s+=line+"\n"; } con.disconnect(); } catch(IOException e){ Log.e("error", "error in reading", e); } return s; }
protected void onPostExecute(String result) { tv3.setText(result); } } }