fork(2) download
  1. package net.npaka.socketex;
  2. import android.app.Activity;
  3. import android.graphics.Color;
  4. import android.os.Bundle;
  5. import android.os.Handler;
  6. import android.view.View;
  7. import android.view.Window;
  8. import android.widget.Button;
  9. import android.widget.EditText;
  10. import android.widget.LinearLayout;
  11. import android.widget.TextView;
  12. import java.io.InputStream;
  13. import java.io.OutputStream;
  14. import java.net.Socket;
  15.  
  16. import net.npaka.socketex.TestAccelerometer.TickHandler;
  17.  
  18. //ソケット通信
  19. public class SocketEx extends Activity
  20. implements View.OnClickListener {
  21. private final static String BR=System.getProperty("line.separator");
  22. private final static int WC=LinearLayout.LayoutParams.WRAP_CONTENT;
  23. private final static int MP=LinearLayout.LayoutParams.MATCH_PARENT;
  24.  
  25. //IPアドレスの指定(1)
  26. private final static String IP="192.168.0.61";//★変更必須
  27.  
  28. private TextView lblReceive;//受信ラベル
  29. private EditText edtSend; //送信エディットテキスト
  30. private Button btnSend; //送信ボタン
  31.  
  32. private Socket socket; //ソケット
  33. private InputStream in; //入力ストリーム
  34. private OutputStream out; //出力ストリーム
  35. private boolean error; //エラー
  36.  
  37. private final Handler handler=new Handler();//ハンドラ
  38.  
  39. //アクティビティ起動時に呼ばれる
  40. @Override
  41. public void onCreate(Bundle bundle) {
  42. super.onCreate(bundle);
  43. requestWindowFeature(Window.FEATURE_NO_TITLE);
  44.  
  45. //レイアウトの生成
  46. LinearLayout layout=new LinearLayout(this);
  47. layout.setBackgroundColor(Color.rgb(255,255,255));
  48. layout.setOrientation(LinearLayout.VERTICAL);
  49. setContentView(layout);
  50.  
  51. /*//送信エディットテキストの生成
  52.   edtSend=new EditText(this);
  53.   edtSend.setId(2);
  54.   edtSend.setText("",TextView.BufferType.NORMAL);
  55.   edtSend.setLayoutParams(new LinearLayout.LayoutParams(MP,WC));
  56.   layout.addView(edtSend);
  57.   */
  58. //送信ボタンの生成
  59. btnSend=new Button(this);
  60. btnSend.setText("送信");
  61. btnSend.setOnClickListener(this);
  62. btnSend.setLayoutParams(new LinearLayout.LayoutParams(WC,WC));
  63. layout.addView(btnSend);
  64.  
  65. //受信ラベルの生成
  66. lblReceive=new TextView(this);
  67. lblReceive.setId(1);
  68. lblReceive.setText("");
  69. lblReceive.setTextSize(16.0f);
  70. lblReceive.setTextColor(Color.rgb(0,0,0));
  71. lblReceive.setLayoutParams(new LinearLayout.LayoutParams(MP,WC));
  72. layout.addView(lblReceive);
  73. }
  74.  
  75. //アクティビティ開始時に呼ばれる
  76. @Override
  77. public void onStart() {
  78. super.onStart();
  79.  
  80. //スレッドの生成
  81. Thread thread=new Thread(){
  82. public void run() {
  83. try {
  84. connect(IP,8080);
  85. } catch (Exception e) {
  86. }
  87. }
  88. };
  89. thread.start();
  90. }
  91.  
  92. //アクティビティの停止時に呼ばれる
  93. @Override
  94. public void onStop() {
  95. super.onStop();
  96. disconnect();
  97. }
  98.  
  99. //受信テキストの追加
  100. private void addText(final String text) {
  101. //ハンドラの生成
  102. handler.post(new Runnable(){
  103. public void run() {
  104. lblReceive.setText(text+BR+
  105. lblReceive.getText());
  106. }
  107. });
  108. }
  109.  
  110. //接続
  111. private void connect(String ip,int port) {
  112. int size;
  113. String str;
  114. byte[] w=new byte[1024];
  115. try {
  116. //ソケット接続(2)
  117. addText("接続中");
  118. socket=new Socket(ip,port);
  119. in =socket.getInputStream();
  120. out=socket.getOutputStream();
  121. addText("接続完了");
  122.  
  123. //受信ループ(3)
  124. while (socket!=null && socket.isConnected()) {
  125. //データの受信(4)
  126. size=in.read(w);
  127. if (size<=0) continue;
  128. str=new String(w,0,size,"UTF-8");
  129.  
  130. //ラベルへの文字列追加
  131. addText(str);
  132. }
  133. } catch (Exception e) {
  134. addText("通信失敗しました");
  135. }
  136. }
  137.  
  138. //切断
  139. private void disconnect() {
  140. try {
  141. socket.close();
  142. socket=null;
  143. } catch (Exception e) {
  144. }
  145. }
  146.  
  147. public String edtStr;
  148. //ボタンクリックイベントの処理
  149. public void onClick(View v) {
  150.  
  151. //スレッッドの生成
  152. Thread thread=new Thread(new Runnable(){public void run(){
  153.  
  154. error=false;
  155. try {
  156. //データの送信(5)
  157.  
  158. if (socket!=null && socket.isConnected()) {
  159.  
  160.  
  161. TestAccelerometer.TickHandler a = new TestAccelerometer().new TickHandler();
  162. edtStr = a.getStr();
  163.  
  164. byte[] w=edtStr.getBytes("UTF8");
  165. out.write(w);
  166. out.flush();
  167. }
  168. } catch (Exception e) {
  169. error=true;
  170. }
  171. //ハンドラの生成
  172. handler.post(new Runnable(){public void run() {
  173. if (!error) {
  174. edtSend.setText("",TextView.BufferType.NORMAL);
  175. } else {
  176. addText("通信失敗しました");
  177. }
  178. }});
  179. }});
  180. thread.start();
  181. }
  182. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:19: class SocketEx is public, should be declared in a file named SocketEx.java
public class SocketEx extends Activity    
       ^
Main.java:2: package android.app does not exist
import android.app.Activity;
                  ^
Main.java:3: package android.graphics does not exist
import android.graphics.Color;
                       ^
Main.java:4: package android.os does not exist
import android.os.Bundle;
                 ^
Main.java:5: package android.os does not exist
import android.os.Handler;
                 ^
Main.java:6: package android.view does not exist
import android.view.View;
                   ^
Main.java:7: package android.view does not exist
import android.view.Window;
                   ^
Main.java:8: package android.widget does not exist
import android.widget.Button;
                     ^
Main.java:9: package android.widget does not exist
import android.widget.EditText;
                     ^
Main.java:10: package android.widget does not exist
import android.widget.LinearLayout;
                     ^
Main.java:11: package android.widget does not exist
import android.widget.TextView;
                     ^
Main.java:16: package net.npaka.socketex.TestAccelerometer does not exist
import net.npaka.socketex.TestAccelerometer.TickHandler;
                                           ^
Main.java:19: cannot find symbol
symbol: class Activity
public class SocketEx extends Activity    
                              ^
Main.java:20: package View does not exist
    implements View.OnClickListener {
                   ^
Main.java:28: cannot find symbol
symbol  : class TextView
location: class net.npaka.socketex.SocketEx
    private TextView lblReceive;//?????
            ^
Main.java:29: cannot find symbol
symbol  : class EditText
location: class net.npaka.socketex.SocketEx
    private EditText edtSend;   //???????????
            ^
Main.java:30: cannot find symbol
symbol  : class Button
location: class net.npaka.socketex.SocketEx
    private Button   btnSend;   //?????
            ^
Main.java:37: cannot find symbol
symbol  : class Handler
location: class net.npaka.socketex.SocketEx
    private final Handler handler=new Handler();//????
                  ^
Main.java:41: cannot find symbol
symbol  : class Bundle
location: class net.npaka.socketex.SocketEx
    public void onCreate(Bundle bundle) {
                         ^
Main.java:149: cannot find symbol
symbol  : class View
location: class net.npaka.socketex.SocketEx
    public void onClick(View v) {
                        ^
Main.java:22: package LinearLayout does not exist
    private final static int WC=LinearLayout.LayoutParams.WRAP_CONTENT;
                                            ^
Main.java:23: package LinearLayout does not exist
    private final static int MP=LinearLayout.LayoutParams.MATCH_PARENT;
                                            ^
Main.java:37: cannot find symbol
symbol  : class Handler
location: class net.npaka.socketex.SocketEx
    private final Handler handler=new Handler();//????
                                      ^
Main.java:42: cannot find symbol
symbol  : variable super
location: class net.npaka.socketex.SocketEx
        super.onCreate(bundle);
        ^
Main.java:43: cannot find symbol
symbol  : variable Window
location: class net.npaka.socketex.SocketEx
        requestWindowFeature(Window.FEATURE_NO_TITLE);
                             ^
Main.java:46: cannot find symbol
symbol  : class LinearLayout
location: class net.npaka.socketex.SocketEx
        LinearLayout layout=new LinearLayout(this);
        ^
Main.java:46: cannot find symbol
symbol  : class LinearLayout
location: class net.npaka.socketex.SocketEx
        LinearLayout layout=new LinearLayout(this);
                                ^
Main.java:47: cannot find symbol
symbol  : variable Color
location: class net.npaka.socketex.SocketEx
        layout.setBackgroundColor(Color.rgb(255,255,255));
                                  ^
Main.java:48: cannot find symbol
symbol  : variable LinearLayout
location: class net.npaka.socketex.SocketEx
        layout.setOrientation(LinearLayout.VERTICAL);
                              ^
Main.java:59: cannot find symbol
symbol  : class Button
location: class net.npaka.socketex.SocketEx
        btnSend=new Button(this);
                    ^
Main.java:62: package LinearLayout does not exist
        btnSend.setLayoutParams(new LinearLayout.LayoutParams(WC,WC));
                                                ^
Main.java:66: cannot find symbol
symbol  : class TextView
location: class net.npaka.socketex.SocketEx
        lblReceive=new TextView(this);
                       ^
Main.java:70: cannot find symbol
symbol  : variable Color
location: class net.npaka.socketex.SocketEx
        lblReceive.setTextColor(Color.rgb(0,0,0));
                                ^
Main.java:71: package LinearLayout does not exist
        lblReceive.setLayoutParams(new LinearLayout.LayoutParams(MP,WC));        
                                                   ^
Main.java:40: method does not override or implement a method from a supertype
    @Override
    ^
Main.java:78: cannot find symbol
symbol  : variable super
location: class net.npaka.socketex.SocketEx
        super.onStart();
        ^
Main.java:76: method does not override or implement a method from a supertype
    @Override
    ^
Main.java:95: cannot find symbol
symbol  : variable super
location: class net.npaka.socketex.SocketEx
        super.onStop();
        ^
Main.java:93: method does not override or implement a method from a supertype
    @Override
    ^
Main.java:161: package TestAccelerometer does not exist
                	TestAccelerometer.TickHandler a = new TestAccelerometer().new TickHandler();
                	                 ^
Main.java:161: cannot find symbol
symbol: class TestAccelerometer
                	TestAccelerometer.TickHandler a = new TestAccelerometer().new TickHandler();
                	                                      ^
Main.java:174: package TextView does not exist
                    edtSend.setText("",TextView.BufferType.NORMAL);
                                               ^
42 errors
stdout
Standard output is empty