1

Android クライアントが文字列をサーバーに送信します。サーバーはデバイスからの接続を正しいポートで認識しますが、それだけです.何が起こるかは、文字列がサーバーコンソールに出力されることです.

参考までに、Androidアプリ内で実行せずにまったく同じクライアントを作成しましたが、正常に動作するため、Android側で何かが欠けていると思います。この問題を解決するための提案を誰でも提供できますか。どうもありがとう。

クライアントコード:

public class ObjectTestActivity extends Activity {

Button submit;
TextView tv;
private String name = "Hello Android";
private DataOutputStream dos;
private DataInputStream dis;
private final int PORT = 3000;

Button send;
InetAddress host;


@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    send = (Button) findViewById(R.id.send);
    tv = (TextView) findViewById(R.id.tv);


    try{

        host = InetAddress.getLocalHost();
        Socket socket = new Socket("xx.xx.xxx.xxx", PORT);

        dos = new DataOutputStream(socket.getOutputStream());
        dis = new DataInputStream(socket.getInputStream());

    }catch(UnknownHostException e){}
     catch(IOException e){}
}


public void onClick(View view){

    try{
        dos.writeUTF(name);
        dos.flush();
        dis.close();
        dos.close();
    }catch(IOException e){}
}
4

1 に答える 1

1

onClick は何に付属していますか? 次のように変更してみてください。

public class MyActivity extends Activity {

  Button submit;
  TextView tv;
  private String name = "Hello Android";
  private DataOutputStream dos;
  private DataInputStream dis;
  private final int PORT = 3000;

  Button send;
  InetAddress host;

  protected void onCreate(Bundle icicle) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.main);

        send = (Button) findViewById(R.id.send);
        tv = (TextView) findViewById(R.id.tv);


        try{

            host = InetAddress.getLocalHost();
            Socket socket = new Socket("xx.xx.xxx.xxx", PORT);

            dos = new DataOutputStream(socket.getOutputStream());
            dis = new DataInputStream(socket.getInputStream());

       }catch(UnknownHostException e){}
        catch(IOException e){}


        send.setOnClickListener(new View.OnClickListener() {
             public void onClick(View v) {
                 try{
                 dos.writeUTF(name);
                 dos.flush();
                 dis.close();
                 dos.close();
              }catch(IOException e){}
             }
         });
     }
 }

ボタンの onClick イベント用。

簡単に言うと、onCreate (send.onCreate(...)) 内でボタンの onClick メソッドを定義します。

この例はhereから来ました

于 2012-03-03T18:02:15.353 に答える