Android通过UDP发送字符串

时间:2014-07-21 09:10:16

标签: android sockets udp client

(老邮报) 我试图使用一种方法在Android设备上发送字符串UDP(而不是模拟器) 但应用程序不起作用,我不知道为什么......也许有人有想法。

在Android Manifest中,它是Internet设置的权限。

提前致谢!

(新帖) 好的代码正在工作,但我不确定静态变量是最好的还是唯一的方式给线程一个变量。它只是我所知道的唯一解决方案。

是否有更专业的方法将变量传递给线程方法?

(PS:这是真正最好的论坛)

新的工作守则如下:

  package com.example.androidudp_client;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;

import android.app.Activity;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener{

    private Button button1;
    public EditText editText1;

    public static String sendingWord = "";

    // Method to send Sting to UDP Server
    public static void sendToServer(String nachricht) throws SocketException, IOException, Exception, Throwable
    {
        DatagramSocket clientSocket = new DatagramSocket();

        InetAddress ipaddr = InetAddress.getByName("192.168.178.65");

        byte[] sendData    = new byte[1024];
        byte[] receiveData = new byte[1024];

        String sentence = nachricht;            
        sendData = sentence.getBytes();

        DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, ipaddr, 8888);

        clientSocket.send(sendPacket);
        clientSocket.close();
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button1 = (Button) findViewById(R.id.button1);
        button1.setOnClickListener(this);
        editText1 = (EditText)findViewById(R.id.editText1);
    }


    @Override
    public void onClick(View arg0) {
         String stringEditText = editText1.getText().toString();
         sendingWord = stringEditText;
         Toast.makeText(getApplicationContext(), "In editText steht : " + stringEditText, Toast.LENGTH_SHORT).show();


             Thread thread = new Thread(new Runnable(){
                    @Override
                    public void run() {
                        try 
                        {
                            sendToServer(sendingWord);
                            sendingWord = "";
                        } catch (Exception e) 
                        {
                            StringWriter errors = new StringWriter();
                            e.printStackTrace(new PrintWriter(errors));
                            String hier2 =  errors.toString();
                            Toast.makeText(getApplicationContext(), "Exception :" + hier2, Toast.LENGTH_LONG).show();

                        } 
                        catch (Throwable th) 
                        {
                            StringWriter errors = new StringWriter();
                            th.printStackTrace(new PrintWriter(errors));
                            String hier3 =  errors.toString();
                            Toast.makeText(getApplicationContext(), "Throwable :" + hier3, Toast.LENGTH_LONG).show();
                        }
                    }
                });

                thread.start();

    }
}

1 个答案:

答案 0 :(得分:1)

我看到你只是捕获IOExceptions。 它可能是由SecurityException引起的。您可以更改" IOException"到"例外"并查看抛出的异常类型。

注意:添加此答案,因为它会导致修复

相关问题