-2
我想通過tcp發送一個簡單的日期。我與線程工作,但我不知道爲什麼,但是當我嘗試運行我的代碼停止imediatelly在這裏。我已經設置了許可。Android通過套接字發送數據
我做錯了什麼?我真的初學者所以請幫我
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>
如果我按下按鈕通過socket發送數據到我的服務器 – meklod400
我做了一個mistacek我住的這條線 'date.setText(新的Date()的toString()); ' 在線程中。刪除後仍然不發送任何數據 – meklod400