commit 09ece3009a8c397e34667381cf943d6f65e650b4
parent 94643b8b59bf5cfad91173818db84cb4665a0071
Author: David Engel <david@absentmindedproductions.ca>
Date: Sat, 23 May 2015 23:36:38 -0600
connecting to and sending message to middle man works
Diffstat:
3 files changed, 68 insertions(+), 5 deletions(-)
diff --git a/kd/app/src/main/AndroidManifest.xml b/kd/app/src/main/AndroidManifest.xml
@@ -1,15 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="kd.shared_lists" >
-
+ <uses-permission android:name="android.permission.INTERNET" />
+ <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
+ <uses-permission android:name="android.permission.INTERNET" />
+ <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<activity
android:name=".HomeScreen"
android:label="@string/app_name" >
+ <uses-permission android:name="android.permission.INTERNET" />
+ <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<intent-filter>
<action android:name="android.intent.action.MAIN" />
diff --git a/kd/app/src/main/java/kd/shared_lists/HomeScreen.java b/kd/app/src/main/java/kd/shared_lists/HomeScreen.java
@@ -1,21 +1,78 @@
package kd.shared_lists;
+import android.content.Context;
+import android.net.ConnectivityManager;
+import android.net.NetworkInfo;
+import android.os.AsyncTask;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
+import android.widget.TextView;
+import java.io.BufferedWriter;
+import java.io.OutputStreamWriter;
+import java.io.PrintWriter;
+import java.net.Inet4Address;
+import java.net.InetAddress;
+import java.net.Socket;
-public class HomeScreen extends ActionBarActivity {
+public class HomeScreen extends ActionBarActivity {
+ private TextView tv;
+ public static final int SERVERPORT = 5437;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
- setContentView(R.layout.layout_home_screen);
+ testNet();
+ new sendMessageTask().execute("foobar");
}
private void testNet() {
+ setContentView(R.layout.layout_home_screen);
+ ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
+ NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
+ tv = (TextView) findViewById(R.id.hellotext);
+ if (networkInfo != null && networkInfo.isConnected()) {
+ tv.setText("Connected");
+ } else {
+ tv.setText("Disconnected");
+ }
+ }
+ private class sendMessageTask extends AsyncTask<String, Void, String> {
+ @Override
+ protected String doInBackground(String... urls) {
+ sendMessage();
+ return "hey";
+ }
+ }
+
+ public void sendMessage() {
+ // new lists need title, kyle sends me id
+ try {
+ InetAddress addr = InetAddress.getByName("104.236.186.39");
+ //InetAddress addr = InetAddress.getByName("127.0.0.0");
+ Socket socket = new Socket(addr, SERVERPORT);
+ PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
+ if (out != null) {
+ if (!out.checkError()) {
+ tv.setText("sending");
+ out.println("CHECK IT OUT");
+ } else {
+ tv.setText("errror");
+ }
+ } else {
+ tv.setText("null");
+ }
+ out.flush();
+ socket.close();
+ //tv.setText("Sent");
+ } catch (java.net.UnknownHostException e) {
+ tv.setText("unknown host");
+ } catch (java.io.IOException e) {
+ tv.setText("io exception");
+ }
}
@Override
@@ -39,4 +96,4 @@ public class HomeScreen extends ActionBarActivity {
return super.onOptionsItemSelected(item);
}
-}
+}
+\ No newline at end of file
diff --git a/kd/app/src/main/res/layout/layout_home_screen.xml b/kd/app/src/main/res/layout/layout_home_screen.xml
@@ -5,7 +5,7 @@
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".HomeScreen">
- <TextView android:text="@string/hello_world" android:layout_width="wrap_content"
+ <TextView android:id="@+id/hellotext" android:text="@string/hello_world" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>