java - Android send data via socket -
i send simple date through tcp. working threads, dont know why when try run code stop imediatelly here that. have set permissons well.
what did wrong? beginner please me
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 menu; adds items action bar if 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 android"); date.settext(new date().tostring()); out.flush(); out.close(); socket.close(); } catch (unknownhostexception e) {e.printstacktrace();} catch (ioexception e) { e.printstacktrace();} } }); thread.start(); } }
this 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>
you can interact ui main thread. can´t do
date.settext(new date().tostring());
Comments
Post a Comment