简体   繁体   English

将数据从Android移动版发送到Java服务器

[英]Send data from Android mobile to java server

I try to send data from my Android mobile to a java server. 我尝试将数据从Android手机发送到Java服务器。 I done the server application and simulated a client directly from it by starting a "ClientConnexion" thread. 我完成了服务器应用程序,并通过启动“ ClientConnexion”线程直接从中模拟了一个客户端。 It works fine, the data is coming and I can read it. 它工作正常,数据即将来临,我可以读取它。

Then, I copy ma "ClientConnexion" class to my Android mobile, as it. 然后,将“ ClientConnexion”类复制到我的Android手机上。

When debuging on my server, I see that connection is correctly established, but reading function is blocking, as if data was never incoming. 在服务器上调试时,我看到正确建立了连接,但是读取功能正在阻塞,就好像从未传入数据一样。 However, debugging on Android side shows that it (seems) sent. 但是,在Android端调试表明它(似乎)已发送。

Here is my ClientConnexion code: 这是我的ClientConnexion代码:

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Random;

public class Client implements Runnable{

    private Socket connexion = null;
    private PrintWriter writer = null;
    private BufferedInputStream reader = null;

    public Client(String host, int port){
        try {
            connexion = new Socket(host, port);
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public void run(){

        try {

            writer = new PrintWriter(connexion.getOutputStream(), true);
            reader = new BufferedInputStream(connexion.getInputStream());

            // COMMAND is received by server when emulating client directly from it
            // COMMAND is NOT received when sent from Android mobile
            writer.write("COMMAND");
            writer.flush();

            System.out.println("COMMAND sent");

            String response = read();
            System.out.println("Response : " + response);

            } 
        catch (IOException e1) {
            e1.printStackTrace();
        }

    writer.write("CLOSE");
    writer.flush();
    writer.close();

    }

    private String read() throws IOException{
        String response = "";
        int stream;
        byte[] b = new byte[4096];
        stream = reader.read(b);
        response = new String(b, 0, stream);
        return response;
    }
}

What could be blocking on the Android client side ? Android客户端可能会阻止什么? No exception is thrown. 没有异常被抛出。

make sure your connection is working properly,maybe your android client and your server in different network segment? 确保您的连接正常,也许您的android客户端和服务器在不同的网段中? Last you can debug or log you client. 最后,您可以调试或登录客户端。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM