Java Android客户端无法连接
我正在尝试将JAVA服务器连接到android应用程序,但失败了 这是我的服务器Java Android客户端无法连接,java,android,Java,Android,我正在尝试将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;
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服务器未连接…请帮我更正…我认为您的问题在这里:socket=new socket(“localhost”,7654); 为什么是localhost?它应该是服务器IP地址 还有一个问题:
Toast.makeText(getBaseContext(), "Connected", Toast.LENGTH_LONG).show();
您不能从非UI线程显示toast
localhost
是指执行代码的计算机,在Android应用程序的上下文中是手机或模拟器,而不是开发计算机
测试场景的最佳方法是将手机连接到开发机器连接的同一本地无线网络,并使用机器的IP地址,而不是localhost