将数据从android传输到java桌面应用程序

时间:2014-02-16 20:19:35

标签: java android sockets android-asynctask asyncsocket

我在Android中运行应用程序并将字符串值作为答案。现在我想将此结果传递给我的localhost上的Java服务器,即将值从Android代码传递到Java桌面应用程序。

我尝试使用此代码,但是当我运行客户端时,它说不幸的是应用程序已停止。

它说:

android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1145)
libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
libcore.io.IoBridge.connectErrno(IoBridge.java:127)

我从某个地方发现我需要使用asynctask。你能指导我如何在这里使用asynctask吗?

IP地址是我本地主机的IP,端口号是任意随机数。

我尝试运行服务器,然后运行客户端。我甚至尝试在模拟器和实际设备上运行客户端。该应用关闭。我是Android新手。我做错了什么?

以下是客户的代码:

    package com.example.client;

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


    public class MainActivity extends Activity {

    private Socket client;
    private PrintWriter printwriter;
    private EditText textField;
    private Button button;
    private String messsage;

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

    textField = (EditText) findViewById(R.id.editText1); //reference to the text field
    button = (Button) findViewById(R.id.button1); //reference to the send button

    //Button press event listener
    button.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) {

    messsage = textField.getText().toString(); //get the text message on the text field
    textField.setText(""); //Reset the text field to blank

    try {

    client = new Socket("10.0.2.2", 4444); //connect to server
    printwriter = new PrintWriter(client.getOutputStream(),true);
    printwriter.write(messsage); //write the message to output stream

    printwriter.flush();
    printwriter.close();
    client.close(); //closing the connection

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

这是服务器的代码:

    public class server {

    private static ServerSocket serverSocket;
    private static Socket clientSocket;
    private static InputStreamReader inputStreamReader;
    private static BufferedReader bufferedReader;
    private static String message;

    public static void main(String[] args) {

    try {
    serverSocket = new ServerSocket(4444); //Server socket

    } catch (IOException e) {
    System.out.println("Could not listen on port: 4444");
    }

    System.out.println("Server started. Listening to the port 4444");

    while (true) {
    try {

    clientSocket = serverSocket.accept(); //accept the client connection
    inputStreamReader = new InputStreamReader(clientSocket.getInputStream());
    bufferedReader = new BufferedReader(inputStreamReader); //get the client message
    message = bufferedReader.readLine();

    System.out.println(message);
    inputStreamReader.close();
    clientSocket.close();

    } catch (IOException ex) {
    System.out.println("Problem in message reading");
    }
    }

    }
    }

异步任务代码:

package com.example.client;

public class MainActivity extends Activity {
    private Socket client;
    private PrintWriter printwriter;
    private EditText textField;
    private Button button;
    private String messsage;

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

    textField = (EditText) findViewById(R.id.editText1); //reference to the text field
    button = (Button) findViewById(R.id.button1);   //reference to the send button

    //Button press event listener
    button.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) {

    messsage = textField.getText().toString(); //get the text message on the text field
    textField.setText("");      //Reset the text field to blank

    try {

     client = new Socket("134.190.162.165", 44440);  //connect to server
     printwriter = new PrintWriter(client.getOutputStream(),true);
     printwriter.write(messsage);  //write the message to output stream

     printwriter.flush();
     printwriter.close();
     client.close();   //closing the connection

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

    }


 }

1 个答案:

答案 0 :(得分:0)

在PC端使用它来获取其IP:

String IPfromPC = InetAddress.getLocalHost().getHostAddress();

在Andoid应用中使用该IP:

mSocket.connect(new InetSocketAddress(IPfromPC, 4444), 5000);
相关问题