不能使用socket从java服务器发送字符串数组到android客户端

时间:2014-02-19 07:10:05

标签: java android sockets objectoutputstream

我正在尝试创建一个与java服务器交互的android应用程序。服务器需要将字符串数组发送到客户端。客户端(android)接收字符串数组,需要在列表视图中显示它。我尝试以下代码,但客户端没有在logcat中打印收到的字符串。

Android客户端

ObjectInputStream objectinput=new ObjectInputStream(client.getInputStream());
operation=(String[])objectinput.readObject();
Log.i("operation",operation[0]);

Java Server

ObjectOutputStream objectoutput=new ObjectOutputStream( clientSocket.getOutputStream());
objectoutput.writeObject(G.opername);
objectoutput.flush();

完整服务器代码

public class Project {

private static ServerSocket serverSocket;
private static Socket clientSocket;
private static InputStreamReader inputStreamReader;
private static BufferedReader bufferedReader;
private static String message;
private static byte[] bytearray;
private static Global G = new Global();
private static String msg = "Hai";

public static void main(String[] args) throws ClassNotFoundException, SQLException {

    try {
        serverSocket = new ServerSocket(7575); // Server socket

    } catch (IOException e) {
        System.out.println("Could not listen on port: 7575");
    }

    System.out.println("Server started. Listening to the port 7575");

    while (true) {
        try {

            clientSocket = serverSocket.accept(); // accept the client
                                                  // connection
            inputStreamReader = new InputStreamReader(clientSocket.getInputStream());
            bufferedReader = new BufferedReader(inputStreamReader); // get the client message
            message = bufferedReader.readLine();
            Dbsearch db = new Dbsearch();
            db.search(message);
            System.out.println(message);
            inputStreamReader.close();
            ObjectOutputStream objectoutput = new ObjectOutputStream(
                    clientSocket.getOutputStream());
            objectoutput.writeObject(G.opername);
            objectoutput.flush();
            clientSocket.close();
        } catch (IOException ex) {
            System.out.println("Problem in message reading");
        }
    }

}

}

GLOBAL CLASS

 class Global
 {
      public static String opername[]=new String[100];
 }

我在一个项目中单独尝试套接字编程代码,但它也没有在我的android屏幕上显示任何结果。

Java Server代码

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class Server
{
    private static String msg="Hai";
private static ServerSocket serverSocket;
    private static Socket clientSocket;
public static void main(String args[])
{
    try
        {
            serverSocket = new ServerSocket(7676);  //Server socket
            System.out.println("Server Started listening in 7676");
    clientSocket=serverSocket.accept();
    PrintWriter writer=new PrintWriter(clientSocket.getOutputStream(),true);
    writer.write(msg);
    System.out.println("Message Send");
    writer.flush();
    clientSocket.close();
         } 
         catch (IOException e) 
         {
            System.out.println("Could not listen on port: 7676");
         }
}
}

Android客户端代码

    public class MainActivity extends Activity 
    {
         private EditText ed;
         private Socket sc;
         private String msg="";
         @Override
         protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
         ed=(EditText)findViewById(R.id.editText1);
         try 
         {
          sc=new Socket("10.0.2.2",7676);
          InputStreamReader isr=new InputStreamReader(sc.getInputStream());
          BufferedReader bf=new BufferedReader(isr);
              msg=bf.readLine();
              Log.i("message",msg);
          ed.setText(msg);
          sc.close();
         } 
         catch (UnknownHostException e) 
         {
          // TODO Auto-generated catch block
              e.printStackTrace();
         } 
         catch (IOException e) 
         {
          // TODO Auto-generated catch block
          e.printStackTrace();
         }
   }

0 个答案:

没有答案