0

我正在尝试将 JAVA 服务器连接到 android 应用程序,但我没能做到.....

这是我的服务器......

package com.example.androidserver;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class ServerMain 
{
public static void main(String []args)
{
    ServerSocket ss;
    try 
    {
        ss = new ServerSocket(7654);
        Socket socket = ss.accept();

        boolean t = socket.isConnected();
        if(t)
        {
            System.out.println("Client connected");
        }
    }
    catch (IOException e) 
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }


}
}

这是客户...

package com.example.androidclient;

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

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View; 
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

Button send;
EditText et1;
Socket socket;
@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    new Thread()
    {
        public void run()
        {
            try
            {
                socket = new Socket("localhost", 7654);
                boolean t = socket.isConnected();
                if(t)
                {
                    Toast.makeText(getBaseContext(), "Connected", Toast.LENGTH_LONG).show();
                }
            } 
            catch (UnknownHostException e) 
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } 
            catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }.start();
    send = (Button)findViewById(R.id.button1);
    et1= (EditText)findViewById(R.id.editText1);

    send.setOnClickListener(new OnClickListener(){

        public void onClick(View v) {
            String str;
            str = et1.getText().toString();
            Toast.makeText(getBaseContext(), str, Toast.LENGTH_LONG).show();
        }});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

}

XML 文件..

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true" >

    <requestFocus />
</EditText>

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/editText1"
    android:text="Send" />

</RelativeLayout>

我没弄明白怎么回事……我的客户端 n 服务器没有连接……请帮我纠正它……

4

2 回答 2

0

我认为您的问题在这里: socket = new Socket("localhost", 7654); 为什么是本地主机?它应该是服务器 IP 地址。

还有另一个问题:

Toast.makeText(getBaseContext(), "Connected", Toast.LENGTH_LONG).show();

您不能从非 UI 线程显示 toast。读这个。

于 2013-01-29T09:56:58.273 回答
0

localhost指执行代码的计算机,在 Android 应用程序的上下文中,它是手机或模拟器,而不是您的开发计算机。

测试您的场景的最佳方法是将您的手机连接到您的开发机器所连接的同一个本地无线网络,并使用您机器的 IP 地址,而不是localhost.

于 2013-01-29T09:57:25.003 回答