Android通过套接字发送数据

时间:2013-05-18 21:32:17

标签: java android multithreading sockets tcp

我想通过tcp发送一个简单的日期。我使用线程,但我不知道为什么,但当我尝试运行我的代码,这是停止imediatelly这是。我已经设定了许可证。

我做错了什么?我真的很初学,所以请帮帮我

package com.example.teszt;

import java.util.Date;

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;


public class MainActivity extends Activity {

    TextView date;
    EditText textout;
    TextView textin;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        date=(TextView)findViewById(R.id.Text1);


        //////////////////////////////////////////////////////////////////////////

    }

    public void showNewDate(View v)
    {
        date.setText(new Date().toString());
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

///////////////////////////////////////////////////////////////////////////////////




    public void send_socket(View arg0)
    {


        Thread thread =  new Thread(new Runnable() {
            public void run() { 
        try {


        Socket socket = new Socket("10.38.248.121", 2101);   
        OutputStream out = socket.getOutputStream();  
        PrintWriter output = new PrintWriter(out);         

        output.println("Hello from Android");              
        date.setText(new Date().toString());     

        out.flush();
        out.close();

        socket.close();                                    


          } 
        catch (UnknownHostException e) {e.printStackTrace();} 
        catch (IOException e) { e.printStackTrace();}


            }
        });  thread.start();

    }


}

这是xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/Text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button1"
        android:layout_alignBottom="@+id/button1"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="133dp"
        android:text="Katt"
        tools:ignore="HardcodedText" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="32dp"
        android:layout_marginTop="40dp"
        android:onClick="showNewDate"
        android:text="Dátum"
        tools:ignore="HardcodedText" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button1"
        android:layout_below="@+id/button1"
        android:layout_marginTop="26dp"
        android:onClick="send_socket"
        android:text="Send"
        tools:ignore="HardcodedText" />



</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

您只能从主线程与UI进行交互。你不能做

date.setText(new Date().toString());