CS185c
Chris Pollett
Oct. 11, 2010
GET / HTTP/1.0 newline newline
HTTP/1.1 200 OK Date: Wed, 07 Oct 2009 18:11:01 GMT Server: Apache/2.2.0 (Fedora) Last-Modified: Mon, 17 Nov 2008 23:09:56 GMT ETag: "f5678-2723-ad5ef900" Accept-Ranges: bytes Content-Length: 10019 Connection: close Content-Type: text/html ...CS Department homepage
@Override public void onStart() { super.onStart(); ConnectivityManager cMgr = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo netInfo = cMgr.getActiveNetworkInfo(); this.status.setText(netInfo.toString()); }
<uses-permission android:name="android.permission.INTERNET"></uses-permission> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"> The first is for general networking, the second is for the actual info we have above.
import java.io.*; import java.net.*; public final class MyServer extends Thread { private static final int PORT=8889; private static final int NUM_CONNECT=1; private MyServer() {} public static void main(String args[]) { MyServer myServer = new MyServer(); if(myServer !=null) {myServer.start();} } public void run() { try { ServerSocket server = new ServerSocket(PORT, NUM_CONNECT); Socket client = server.accept(); BufferedReader reader = new BufferedReader( new InputStreamReader(client.getInputStream())); BufferedWriter writer = new BufferedWriter( new OutputStreamWriter(client.getOutputStream())); // do some reading and writing } catch(IOException ie) { ie.printStackTrace(); } } }
Which of the following is true?
package org.pollett; import android.app.Activity; import android.os.Bundle; import android.net.*; import android.content.*; import android.util.*; public class MyClient extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); this.socketButton.setOnClickListener(new OnClickListener)() { public void onClick(final View v) { socketOutput.setText(""); String output = callSocket(ipAddress.getText().toString(), port.getText().toString(), socketInput.getText().toString()); socketOutput.setText(output); } } } private String callSocket(String ip, String port, String socketData) { Socket socket = null; BufferedWriter writer = null; BufferedReader reader =null; String output = null; try{ socket = new Socket(ip, Integer.parseInt(port)); reader = new BufferedReader( new InputStreamReader(socket.getInputStream())); writer = new BufferedWriter( new OutputStreamWriter(socket.getOutputStream())); writer.write("hello world"+socketData+"\n"); writer.flush(); output = reader.readLine(); } catch(Exception e){} return output; } }
<uses-permission android:name="android.permission.INTERNET"></uses-permission>to the manifest file
<WebView android:id="@+id/webview" android:layout_width="fill_parent" android:layout_height="wrap_content" />
webview = (WebView) findViewById(R.id.webview); webview.getSettings().setJavaScriptEnabled(true); webview.loadUrl("http://www.pollett.org/");
private class MyWebViewClient extends WebViewClient { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return true; } }
webview.setWebViewClient(new MyWebViewClient());
URL/service_name/method_name/?arg1=value1&arg2=value2...which requests from the given URL the given method of the given web service with the given arguments.
String location="URL/service_name/method_name/?arg1=value1&arg2=value2"; String result = null; try { URL url = new URL(location); } catch (MalformedURLException e) { //handle somehow } if(url != null) { try { HttpURLConnection urlConn = (HttpURLConnection) url.openConnection(); BufferedReader in = new BufferedReader( new InputStreamReader( urlConn.getInputStream())); String inputLine; while((inputLine = in.readLine()) != null) { result += "\n" + inputLine; } } catch (IOException e) { // handle somehow } return result; } return null;
<phone:PhoneApplicationPage x:Class="WindowsPhoneApplication4.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone" xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768" FontFamily="{StaticResource PhoneFontFamilyNormal}" FontSize="{StaticResource PhoneFontSizeNormal}" Foreground="{StaticResource PhoneForegroundBrush}" SupportedOrientations="Portrait" Orientation="Portrait" shell:SystemTray.IsVisible="True"> <!--LayoutRoot is the root grid where all page content is placed--> <Grid x:Name="LayoutRoot" Background="Transparent"> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition Height="*"/> </Grid.RowDefinitions> <!--TitlePanel contains the name of the application and page title--> <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28"> <TextBlock x:Name="ApplicationTitle" Text="Browser Demo" Style="{StaticResource PhoneTextNormalStyle}"/> </StackPanel> <!--ContentPanel - place additional content here--> <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> <Button Content="Go" Height="72" HorizontalAlignment="Left" Margin="308,-16,0,0" Name="button1" VerticalAlignment="Top" Width="160" Click="button1_Click" /> <phone:WebBrowser HorizontalAlignment="Left" Margin="12,116,0,0" Name="webBrowser1" VerticalAlignment="Top" Height="580" Width="444" /> <Button Content="Go Saved" Height="72" HorizontalAlignment="Left" Margin="155,50,0,0" Name="button2" VerticalAlignment="Top" Width="160" Click="button2_Click" /> </Grid> <TextBox Grid.RowSpan="2" Height="73" HorizontalAlignment="Left" Margin="12,56,0,0" Name="textBox1" Text="http://www.pollett.org" VerticalAlignment="Top" Width="330" /> </Grid> </phone:PhoneApplicationPage>
using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; using Microsoft.Phone.Controls; using System.IO.IsolatedStorage; using System.IO; namespace WindowsPhoneApplication4 { public partial class MainPage : PhoneApplicationPage { // Constructor public MainPage() { InitializeComponent(); } ////// When the 'Go' button is clicked, navigate to the web site. /// Save url in storeurl.txt /// private void button1_Click(object sender, RoutedEventArgs e) { string site; site = textBox1.Text; webBrowser1.Navigate(new Uri(site, UriKind.Absolute)); using (var appStorage = IsolatedStorageFile.GetUserStoreForApplication()) using (var file = appStorage.OpenFile("storeurl.txt", FileMode.OpenOrCreate)) using (var writer = new StreamWriter(file)) { writer.Write(site); } } ////// When the 'Go Saved' button is clicked, read from /// storeurl.txt and navigate to the web site. /// private void button2_Click(object sender, RoutedEventArgs e) { //using means we call Dispose() automatically when done with the object // if use var rather than a type compiler will use type inference using (var appStorage = IsolatedStorageFile.GetUserStoreForApplication()) using (var file = appStorage.OpenFile("storeurl.txt", FileMode.Open)) { using (var reader = new StreamReader(file)) { textBox1.Text = reader.ReadLine(); webBrowser1.Navigate(new Uri(textBox1.Text, UriKind.Absolute)); } } } } }