安卓&对象输出流:僵局

发布于 2025-01-07 21:12:36 字数 1351 浏览 5 评论 0原文

该应用程序的目的是简单地将字符串对象发送到我机器上的服务器。所有端口转发配置都是在我在机器上使用 java 客户端测试服务器时设置的。

问题在于从 Android 客户端与服务器通信。服务器似乎永远不会被击中。

该应用程序的基本概要:带有 1 个按钮和 1 个按钮的简单屏幕。 1 个文本视图。按钮将对象发送到服务器,服务器用消息响应,更新文本视图。正如我所说,服务器似乎从未受到攻击。有人知道我能做些什么来解决这个问题吗?

public class ObjectTestActivity extends Activity {

Button submit;
private String string = "Hello Android";
private ObjectOutputStream oos;
private ObjectInputStream ois;
private final int PORT = 3000;

TextView tv;


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

    Button send = (Button) findViewById(R.id.send);
    tv = (TextView) findViewById(R.id.tv);

    try{

        InetAddress host = InetAddress.getLocalHost();
        Socket socket = new Socket("xx.xx.xxx.xxx", PORT);

        oos = new ObjectOutputStream(socket.getOutputStream());
        ois = new ObjectInputStream(socket.getInputStream());

    }catch(UnknownHostException e){}
     catch(IOException e){}
}


public void onClick(View view){

    try{
        oos.writeObject(string);
        String serverMsg = (String) ois.readObject();
        ois.close();
        oos.close();
                    tv.setText("Message from Server: " + serverMsg);
    }catch(IOException e){}
     catch(ClassNotFoundException e){}
}

}

The purpose of the app is to simply send a string object to the server on my machine. All port forwarding configurations are set as I tested the server with a java client on my machine.

Problem lies in talking to the server from android client. Server seems never to be hit.

Basic rundown of the app: Simple screen with 1 button & 1 textview. Button sends object to server, server responds with message, which updates the textview. As I've said, the server never seems to be hit. Anyone have any ideas what I can do to fix this?

public class ObjectTestActivity extends Activity {

Button submit;
private String string = "Hello Android";
private ObjectOutputStream oos;
private ObjectInputStream ois;
private final int PORT = 3000;

TextView tv;


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

    Button send = (Button) findViewById(R.id.send);
    tv = (TextView) findViewById(R.id.tv);

    try{

        InetAddress host = InetAddress.getLocalHost();
        Socket socket = new Socket("xx.xx.xxx.xxx", PORT);

        oos = new ObjectOutputStream(socket.getOutputStream());
        ois = new ObjectInputStream(socket.getInputStream());

    }catch(UnknownHostException e){}
     catch(IOException e){}
}


public void onClick(View view){

    try{
        oos.writeObject(string);
        String serverMsg = (String) ois.readObject();
        ois.close();
        oos.close();
                    tv.setText("Message from Server: " + serverMsg);
    }catch(IOException e){}
     catch(ClassNotFoundException e){}
}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

野稚 2025-01-14 21:12:36

首先尝试从简单的 java 客户端测试您的 Android 客户端代码。如果这有效,至少你知道这是一个安卓问题。

然后查看您的清单文件。您需要“android.permission.INTERNET”。

您没有在 onCreate 中打印异常的堆栈跟踪,因此无法知道是否发生了异常。

也可以尝试使用更高的端口号 32344。

Try testing your android client code from a simple java client first. If that works at least you know its a android issue.

And then look at your manifest file. You need "android.permission.INTERNET".

You are not printing the stacktrace on the Exceptions in the onCreate, so there is no way to know if a exception occurred.

Also try using a higher port number 32344 maybe.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文