简体   繁体   English

无法使用Android接收网络数据?

[英]Not receiving network data with android?

this is my ConnectionThread for Android i have been trying for a few hours but i am unable to send data from my computer to my app that i am making.. any help would be amazing.. just comment if you need to know anything more.. no exceptions are being thrown and i know a connection is being made.. 这是我为Android所用的ConnectionThread,我已经尝试了几个小时,但是我无法将数据从计算机发送到我正在制作的应用程序中。任何帮助都将是惊人的..如果您需要更多的知识,请发表评论。 。没有异常被抛出,我知道正在建立连接。

public class ConnectionThread implements Runnable {

private final int PORT = 4567;
private static PrintWriter out = null;
private BufferedReader in = null;
Socket kkSocket = null;
String ip = "";
//LoginActivity a;

public ConnectionThread(/*LoginActivity a,*/String ip) {
     this.ip = ip;
}

public void run() {
    try {
        // this.a = a;
            kkSocket = new Socket(ip, PORT);
            out = new PrintWriter(kkSocket.getOutputStream(), true);
            in = new BufferedReader(
                    new InputStreamReader(kkSocket.getInputStream()));
        //    a.makeToast("CONNECTION SUCCESS!!");
        } catch (IOException ex) {
            //Logger.getLogger(Auth.class.getName()).log(Level.SEVERE, null, ex);
        //  a.makeToast("UNABLE TO CONNECT TO:"+ ip);
            Log.e("PPChat", "UNABLE TO CONNECT");
        }
    String fromServer = null;
    try {
        while ((fromServer = in.readLine()) == null) {
            Log.e("PP", "it happened...");
            FullscreenActivity.appendText(fromServer);
        }
    } catch (IOException e) {
        e.printStackTrace();
        Log.e("READ ERROR", "was unable to read from the socket");
        //a.makeToast("was unable to read from the socket");
    }
    Log.e("PPChat", "connection is done");
}

public static boolean sendNetworkMessage(String str) {
    try{
    out.println(str);
    out.flush();
    } catch(Exception e) {
        return false;
    }
    return true;
}

}

this is the thread that sends the network data.. i know a connection is being made.. i think i am just making a simple error that i am not seeing.. 这是发送网络数据的线程..我知道正在建立连接..我认为我只是在犯一个我看不到的简单错误。

public class ConnectionThread extends Thread{
private Socket socket = null;
private String inputLine;
public static PrintWriter out = null;
public ConnectionThread(Socket accept) {
    super("ConnectionThread");
    this.socket = accept;
}

@Override
public void run() {

    try {
    System.out.println("connection made");
        out = new PrintWriter(socket.getOutputStream(), true);
        BufferedReader in = new BufferedReader(
                new InputStreamReader(
                        socket.getInputStream()));
        while ((inputLine = in.readLine()) != null) {
            System.out.println("server recieved: " +inputLine);
        }
    } catch(Exception e) {

    }
    System.out.println("thread is finished");
}
}

and here is where i actually send the data in a thread the gets the input from the user to send the data 这是我实际上在线程中发送数据的地方,从用户那里获取输入以发送数据

public class InputThread extends Thread{

private Scanner sc = null;

public InputThread() {
    sc = new Scanner(System.in);
}

@Override
public void run() {
    while(true) {
        String temp = sc.nextLine();
        ConnectionThread.out.println(temp);
        System.out.println("i wrote that: "+temp);
    }
}

}

sorry if this is really easy.. im getting 抱歉,如果这真的很简单..

Why don't you use Android AsyncTask to do this kind for works. 您为什么不使用Android AsyncTask进行此类工作。 I don't really know your exact requirement but normally to do this kind of background works it's recommended to use the AsyncTask. 我真的不知道您的确切要求,但是通常要进行这种背景工作,建议使用AsyncTask。 Please refer this 请参考这个

http://developer.android.com/reference/android/os/AsyncTask.html . http://developer.android.com/reference/android/os/AsyncTask.html

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

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