从Android电子数据传输到Java桌面应用程序 [英] Transfer data from android to java desktop application

查看:123
本文介绍了从Android电子数据传输到Java桌面应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在运行Android的应用程序和一个字符串值作为一个答案。现在,我想通过这个结果是在我的本地Java服务器,即通过在Android code到Java桌面应用程序的价值。

我试图用这个code,但是当我运行客户端它说,不幸的是,应用程序已停止。

它说:

  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地址和端口号是任何随机数。

我尝试运行服务器,然后客户端。我甚至尝试在模拟器和实际设备上运行的客户端。该应用程序关闭。我是新来的Andr​​oid系统。我做错了吗?

下面是客户端的code:

 包com.example.client;    进口java.io.IOException异常;
    进口的java.io.PrintWriter;
    进口的java.net.Socket;
    进口的java.net.UnknownHostException;
    公共类MainActivity延伸活动{    私人客户端的Socket;
    私人PrintWriter的PrintWriter的;
    私人的EditText文本框;
    私人Button按钮;
    私人字符串消息话题;    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);    文本框=(EditText上)findViewById(R.id.editText1); //参考文本字段
    按钮=(按钮)findViewById(R.id.button1); //参考发送按钮    //按钮preSS事件监听器
    button.setOnClickListener(新View.OnClickListener(){    公共无效的onClick(视图v){    。消息话题= textField.getText()的toString(); //获取文本字段短信
    textField.setText(); //重置文本字段为空    尝试{    客户端=新的Socket(10.0.2.2,4444); //连接到服务器
    为PrintWriter =的新PrintWriter(client.getOutputStream(),TRUE);
    printwriter.write(一封邮件); //写邮件到输出流    printwriter.flush();
    printwriter.close();
    client.close(); //关闭连接    }赶上(UnknownHostException异常五){
    e.printStackTrace();
    }赶上(IOException异常五){
    e.printStackTrace();
    }
    }
    });
    }}

和这里的服务器的code:

 公共类服务器{    私有静态的ServerSocket ServerSocket的;
    私有静态插座ClientSocket的;
    私有静态的InputStreamReader InputStreamReader的;
    私有静态的BufferedReader的BufferedReader;
    私人静态字符串消息;    公共静态无效的主要(字串[] args){    尝试{
    ServerSocket的=新的ServerSocket(4444); //服务器套接字    }赶上(IOException异常五){
    的System.out.println(端口无法听:4444);
    }    的System.out.println(服务器开始听端口4444。);    而(真){
    尝试{    ClientSocket的=的ServerSocket.accept(); //接受客户端的连接
    InputStreamReader的=新的InputStreamReader(clientSocket.getInputStream());
    的BufferedReader =新的BufferedReader(InputStreamReader的); //获取客户端消息
    消息= bufferedReader.readLine();    的System.out.println(消息);
    inputStreamReader.close();
    clientSocket.close();    }赶上(IOException异常前){
    的System.out.println(问题的消息读);
    }
    }    }
    }

的异步任务code:

包com.example.client;

 公共类MainActivity延伸活动{
    私人客户端的Socket;
    私人PrintWriter的PrintWriter的;
    私人的EditText文本框;
    私人Button按钮;
    私人字符串消息话题;    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);    文本框=(EditText上)findViewById(R.id.editText1); //参考文本字段
    按钮=(按钮)findViewById(R.id.button1); //参考发送按钮    //按钮preSS事件监听器
    button.setOnClickListener(新View.OnClickListener(){    公共无效的onClick(视图v){    。消息话题= textField.getText()的toString(); //获取文本字段短信
    textField.setText(); //重置文本字段为空    尝试{     客户端=新的Socket(134.190.162.165,44440); //连接到服务器
     为PrintWriter =的新PrintWriter(client.getOutputStream(),TRUE);
     printwriter.write(一封邮件); //写邮件到输出流     printwriter.flush();
     printwriter.close();
     client.close(); //关闭连接    }赶上(UnknownHostException异常五){
     e.printStackTrace();
    }赶上(IOException异常五){
     e.printStackTrace();
    }
    }
    });    }
 }


解决方案

使用这个在PC端获取其IP:

 字符串IPfromPC = InetAddress.getLocalHost()getHostAddress()。

使用该IP在Andoid的应用程序:

  mSocket.connect(新的InetSocketAddress(IPfromPC,4444),5000);

I run an application in Android and a string value as an answer. Now I want to pass this result to the Java server which is on my localhost, i.e pass the value from Android code to Java desktop application.

I tried using this code, but when I run the client it says unfortunately the application has stopped.

It says:

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

I found from somewhere that I need to use asynctask for it. Can you please guide me on how to use asynctask here?

The IP address is my localhost's IP and port number is any random number.

I try to run the server and then the client. I even tried running client on emulator and on the actual device. The app closes. I am new to Android. What I am doing wrong?

Here's the code of the client:

    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();
    }
    }
    });
    }}

And here's the code of the server:

    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");
    }
    }

    }
    }

The Async Task code :

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();
    }
    }
    });

    }


 }

解决方案

Use this on the PC side to get its IP:

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

Use that IP in your Andoid app:

mSocket.connect(new InetSocketAddress(IPfromPC, 4444), 5000);

这篇关于从Android电子数据传输到Java桌面应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆