
cosc 4730. Brief return Sockets And HttpClient (and with AsyncTask ) DownloadManager. Android. Networking is based on standard Java SE methods And get the BufferedReader / PrintWriter Most Java SE network code works with almost no modifications. . typical Android network code.
Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.
try {
InetAddressserverAddr = InetAddress.getByName(host);
//make the connection
Socket socket = new Socket(serverAddr, port);
String message = "Hello from Client android emulator";
try {
//receive a message
PrintWriterout = new PrintWriter( new BufferedWriter( new OutputStreamWriter(socket.getOutputStream())), true);
out.println(message);
//send a message now
BufferedReaderin = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String str = in.readLine();
} catch(Exception e) {
//error!
} finally {
socket.close(); in.close(); out.close();
}
} catch (Exception e) { //unable to connect }
String hostname = “localhost”; // remote machine
Int port = 3012; //remote port number
//make the connection
InetAddressserverAddr = InetAddress.getByName(hostname);
Socket socket = new Socket(serverAddr, port);
//now we have a connection to the server
Int port = 3012; //this is the local port number
//create the server socket
ServerSocketserverSocket = new ServerSocket(port);
//wait for a client to connect
Socket socket= serverSocket.accept();
//now we have a connection to the client.
//Write side
PrintWriterout = new PrintWriter( new BufferedWriter( new OutputStreamWriter( socket.getOutputStream())),true);
//Read side.
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
in.close(); out.close();
socket.close();
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.cosc4755.TCPclient"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".TCPclient"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-sdkandroid:minSdkVersion="5" />
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
HttpClientclient = new DefaultHttpClient();
HttpGetrequest = new HttpGet();
request.setURI(new URI("http://www.uwyo.edu/"));
HttpResponseresponse = client.execute(request);
HttpGet method = new HttpGet( "http://www.com/do.php?key=valueGoesHere");
HttpResponse response = client.execute(method);
private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> { protected Long doInBackground(URL... urls) {int count = urls.length; long totalSize = 0; for (inti = 0; i < count; i++) {totalSize += Downloader.downloadFile(urls[i]);publishProgress((int) ((i / (float) count) * 100)); // Escape early if cancel() is called if (isCancelled()) break; }return totalSize;} //background thread protected void onProgressUpdate(Integer... progress) {setProgressPercent(progress[0]);} //UI thread protected void onPostExecute(Long result) {showDialog("Downloaded " + result + " bytes");} //UI thread}
new DownloadFilesTask().execute(url1, url2, url3);
URL is pamaters to doInBackground
Integer is the value for publishProgress and onProgressUpdate
And Long is the return value and parameter to onPostExecute
The call, uses URL to create the “list” used in doInBackground
DownloadManagerdownloadManager= (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
DownloadManager.Request request = new DownloadManager.Request(URI)
long download_id = downloadManager.enqueue(request);
A note, setShowRunningNotification(false) didn’t work on 4.1.x
IntentFilterintentFilter = new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
registerReceiver(downloadReceiver, intentFilter);
unregisterReceiver(downloadReceiver);
DownloadManager.Queryquery = new DownloadManager.Query();
query.setFilterById(intentdownloadId);
Cursor cursor = downloadManager.query(query);