来自服务器的android客户端 - 服务器(套接字)响应为空

时间:2015-11-15 17:38:24

标签: java android sockets android-asynctask client-server

//服务器方法

public static void searchMethodCatcher()
{
  try
  {
 clientSocket = serverSocket.accept();
 isr = new InputStreamReader(clientSocket.getInputStream(),charset);
 br = new BufferedReader(isr);
 String str = br.readLine();
   if(str.charAt(0) == '{')
   {
      extendetSearchSystem(str);
   }
   else {simpleSearchListener(str);}    
 }catch(IOException e){e.printStackTrace();}
}
public static void simpleSearchListener(String str)
{
  String result = str;
        System.out.println(line);

  try {
        clientSocket.shutdownInput();
        pw = new PrintWriter(clientSocket.getOutputStream(),true);
        pw.write("DONE");
        pw.flush();
        pw.close();
        clientSocket.close();

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
public static void serverListener(int port)
{
    new Thread() {
        @Override
        public void run() {
            super.run();
            try
            {
                serverSocket = new ServerSocket(PORT_WORK);
            }catch(IOException e){
                e.printStackTrace();
                System.out.println("Could not listen on port"+ PORT_WORK);
            }
            while(isRun)
            {
                searchMethodCatcher();
               // simpleSearchListener();
            }
        }
    }.start();
}

//客户端asyncTast clas

 class ServerWorker extends AsyncTask<Void,Void,Wrapper> {
    public int flag = 0, i = 0;
    public String[] tarr;
    public Wrapper w = new Wrapper();
    public String jsonResult,temp;
    public JSONObject jsonObject;
    @Override
    protected Wrapper doInBackground(Void... params) {
        try {
            try {
                clientSocket = new Socket(ip, port);
                if (line.isEmpty()) {
                    flag = 2;
                } else {
                    printWriter = new PrintWriter(clientSocket.getOutputStream());
                    printWriter.write(line);
                    printWriter.flush();
                    clientSocket.shutdownOutput();


                  //  clientSocket.shutdownOutput();
                    isr = new InputStreamReader(clientSocket.getInputStream(),"UTF-8");
                    br = new BufferedReader(isr);
                    Log.e("READY?",br.ready()+" INPUT?" + clientSocket.isInputShutdown());
                    String message = br.readLine();
                    if(br.readLine()!=null)
                    Log.e("LINE IS",br.readLine()+"");
                    else{ Log.e("ERROR","br.readLine is null")
                    }
                    clientSocket.close();

                }
            } catch (ConnectException e) {
                e.printStackTrace();
                flag = 1;
            }
        }catch (IOException e) {e.printStackTrace();}
        return w;
    }

    @Override
    protected void onProgressUpdate(Void... values) {
        super.onProgressUpdate(values);
    }

    @Override
    protected void onPostExecute(Wrapper wrapper) {
        super.onPostExecute(wrapper);

    }
 }
}

所以。当我在手机上的PC和客户端上运行服务器,然后发送消息我得到以下内容。 服务器获取正常消息:Server logs

但是从服务器到手机的响应始终为空 client logs

我认为输入/输出流有问题。请帮我解决这个问题./

客户端日志 11-15 22:29:31.750 16011-16011/com.kiskin.vlad.kvsearchsystem W/KeyCharacterMap﹕ Can't open keycharmap file 11-15 22:29:31.750 16011-16011/com.kiskin.vlad.kvsearchsystem W/KeyCharacterMap﹕ Error loading keycharmap file 11-15 22:29:31.750 16011-16011/com.kiskin.vlad.kvsearchsystem W/KeyCharacterMap﹕ Using default keymap 11-15 22:29:33.257 16011-16011/com.kiskin.vlad.kvsearchsystem I/ApplicationPackageManager﹕ cscCountry is not German : SEK 11-15 22:29:33.359 16011-16011/com.kiskin.vlad.kvsearchsystem W/KeyCharacterMap﹕ No keyboard for id -1 11-15 22:29:33.359 16011-16011/com.kiskin.vlad.kvsearchsystem W/KeyCharacterMap﹕ Using default keymap 11-15 22:29:35.289 16011-16011/com.kiskin.vlad.kvsearchsystem I/ApplicationPackageManager﹕ cscCountry is not German : SEK 11-15 22:29:40.578 16011-25796/com.kiskin.vlad.kvsearchsystem E/READY?﹕ false INPUT?false 11-15 22:29:41.976 16011-25796/com.kiskin.vlad.kvsearchsystem E/ERROR﹕ br.readLine is null

服务器日志 /192.168.0.8 iphone Apple iPhone 5S 16GB (Space Gray): http://hotline.ua/mobile-mobilnye-telefony-i-smartfony/apple-iphone-5s-16gb-space-gray/ Диапазон цен: 7 895 - 14 500 грн Apple iPhone 6 16GB (Space Gray): http://hotline.ua/mobile-mobilnye-telefony-i-smartfony/apple-iphone-6-16gb-space-gray/ Диапазон цен: 13 199 - 30 200 грн Apple iPhone 6s 64GB (Space Gray): http://hotline.ua/mobile-mobilnye-telefony-i-smartfony/apple-iphone-6s-64gb-space-gray/ Диапазон цен: 20 043 - 27 816 грн Apple iPhone 5 16GB (Black): http://hotline.ua/mobile-mobilnye-telefony-i-smartfony/apple-iphone-5-16gb-black/ Диапазон цен: 6 099 - 8 299 грн Apple iPhone 6 64GB (Space Gray): http://hotline.ua/mobile-mobilnye-telefony-i-smartfony/apple-iphone-6-64gb-space-gray/ Диапазон цен: 15 782 - 25 457 грн Apple iPhone 6 16GB (Gold): http://hotline.ua/mobile-mobilnye-telefony-i-smartfony/apple-iphone-6-16gb-gold/ Диапазон цен: 14 271 - 20 999 грн Apple iPhone 5S 16GB (Gold): http://hotline.ua/mobile-mobilnye-telefony-i-smartfony/apple-iphone-5s-16gb-gold/ Диапазон цен: 8 555 - 13 640 грн Apple iPhone 6s 16GB (Space Gray): http://hotline.ua/mobile-mobilnye-telefony-i-smartfony/apple-iphone-6s-16gb-space-gray/ Диапазон цен: 17 220 - 23 520 грн Apple iPhone 5 16GB (White): http://hotline.ua/mobile-mobilnye-telefony-i-smartfony/apple-iphone-5-16gb-white/ Диапазон цен: 6 099 - 10 417 грн Apple iPhone 6s 64GB (Rose Gold): http://hotline.ua/mobile-mobilnye-telefony-i-smartfony/apple-iphone-6s-64gb-rose-gold/ Диапазон цен: 20 160 - 29 551 грн Apple iPhone 5S 32GB (Space Gray): http://hotline.ua/mobile-mobilnye-telefony-i-smartfony/apple-iphone-5s-32gb-space-gray/ Диапазон цен: 9 018 - 15 750 грн Apple iPhone 6s 16GB (Rose Gold): http://hotline.ua/mobile-mobilnye-telefony-i-smartfony/apple-iphone-6s-16gb-rose-gold/ Диапазон цен: 17 349 - 23 520 грн Apple iPhone 6s 16GB (Gold): http://hotline.ua/mobile-mobilnye-telefony-i-smartfony/apple-iphone-6s-16gb-gold/ Диапазон цен: 17 145 - 23 520 грн Apple iPhone 6s Plus 64GB (Space Gray): http://hotline.ua/mobile-mobilnye-telefony-i-smartfony/apple-iphone-6s-plus-64gb-space-gray/ Диапазон цен: 24 630 - 33 450 грн Apple iPhone 5C 16GB (White): http://hotline.ua/mobile-mobilnye-telefony-i-smartfony/apple-iphone-5c-16gb-white/ Диапазон цен: 6 048 - 11 250 грн Apple iPhone 6s 128GB (Space Gray): http://hotline.ua/mobile-mobilnye-telefony-i-smartfony/apple-iphone-6s-128gb-space-gray/ Диапазон цен: 22 200 - 31 152 грн Apple iPhone 6 16GB (Silver): http://hotline.ua/mobile-mobilnye-telefony-i-smartfony/apple-iphone-6-16gb-silver/ Диапазон цен: 14 250 - 22 015 грн Apple iPhone 6s 64GB (Silver): http://hotline.ua/mobile-mobilnye-telefony-i-smartfony/apple-iphone-6s-64gb-silver/ Диапазон цен: 20 050 - 27 816 грн Apple iPhone 5S 16GB (Silver): http://hotline.ua/mobile-mobilnye-telefony-i-smartfony/apple-iphone-5s-16gb-white/ Диапазон цен: 8 399 - 14 750 грн Apple iPhone 6 64GB (Silver): http://hotline.ua/mobile-mobilnye-telefony-i-smartfony/apple-iphone-6-64gb-silver/ Диапазон цен: 15 531 - 21 649 грн Results count is 20 Results product is [Apple iPhone 5S 16GB (Space Gray) , Apple iPhone 6 16GB (Space Gray) , Apple iPhone 6s 64GB (Space Gray) , Apple iPhone 5 16GB (Black) , Apple iPhone 6 64GB (Space Gray) , Apple iPhone 6 16GB (Gold) , Apple iPhone 5S 16GB (Gold) , Apple iPhone 6s 16GB (Space Gray) , Apple iPhone 5 16GB (White) , Apple iPhone 6s 64GB (Rose Gold) , Apple iPhone 5S 32GB (Space Gray) , Apple iPhone 6s 16GB (Rose Gold) , Apple iPhone 6s 16GB (Gold) , Apple iPhone 6s Plus 64GB (Space Gray) , Apple iPhone 5C 16GB (White) , Apple iPhone 6s 128GB (Space Gray) , Apple iPhone 6 16GB (Silver) , Apple iPhone 6s 64GB (Silver) , Apple iPhone 5S 16GB (Silver) , Apple iPhone 6 64GB (Silver) ] Results price is [7 895 - 14 500 грн , 13 199 - 30 200 грн , 20 043 - 27 816 грн , 6 099 - 8 299 грн , 15 782 - 25 457 грн , 14 271 - 20 999 грн , 8 555 - 13 640 грн , 17 220 - 23 520 грн , 6 099 - 10 417 грн , 20 160 - 29 551 грн , 9 018 - 15 750 грн , 17 349 - 23 520 грн , 17 145 - 23 520 грн , 24 630 - 33 450 грн , 6 048 - 11 250 грн , 22 200 - 31 152 грн , 14 250 - 22 015 грн , 20 050 - 27 816 грн , 8 399 - 14 750 грн , 15 531 - 21 649 грн ] Results links is [/mobile-mobilnye-telefony-i-smartfony/apple-iphone-5s-16gb-space-gray/ , /mobile-mobilnye-telefony-i-smartfony/apple-iphone-6-16gb-space-gray/ , /mobile-mobilnye-telefony-i-smartfony/apple-iphone-6s-64gb-space-gray/ , /mobile-mobilnye-telefony-i-smartfony/apple-iphone-5-16gb-black/ , /mobile-mobilnye-telefony-i-smartfony/apple-iphone-6-64gb-space-gray/ , /mobile-mobilnye-telefony-i-smartfony/apple-iphone-6-16gb-gold/ , /mobile-mobilnye-telefony-i-smartfony/apple-iphone-5s-16gb-gold/ , /mobile-mobilnye-telefony-i-smartfony/apple-iphone-6s-16gb-space-gray/ , /mobile-mobilnye-telefony-i-smartfony/apple-iphone-5-16gb-white/ , /mobile-mobilnye-telefony-i-smartfony/apple-iphone-6s-64gb-rose-gold/ , /mobile-mobilnye-telefony-i-smartfony/apple-iphone-5s-32gb-space-gray/ , /mobile-mobilnye-telefony-i-smartfony/apple-iphone-6s-16gb-rose-gold/ , /mobile-mobilnye-telefony-i-smartfony/apple-iphone-6s-16gb-gold/ , /mobile-mobilnye-telefony-i-smartfony/apple-iphone-6s-plus-64gb-space-gray/ , /mobile-mobilnye-telefony-i-smartfony/apple-iphone-5c-16gb-white/ , /mobile-mobilnye-telefony-i-smartfony/apple-iphone-6s-128gb-space-gray/ , /mobile-mobilnye-telefony-i-smartfony/apple-iphone-6-16gb-silver/ , /mobile-mobilnye-telefony-i-smartfony/apple-iphone-6s-64gb-silver/ , /mobile-mobilnye-telefony-i-smartfony/apple-iphone-5s-16gb-white/ , /mobile-mobilnye-telefony-i-smartfony/apple-iphone-6-64gb-silver/ ]

1 个答案:

答案 0 :(得分:0)

String message = br.readLine();
if(br.readLine()!=null)
    Log.e("LINE IS",br.readLine()+"");
else{
     Log.e("ERROR","br.readLine is null");
}

你正在读线并扔掉它们。您应该测试和记录的内容是message的内容。不读message,读取另一行并对其进行测试,并记录第三个readLine()的结果。由于服务器甚至没有发送一行,更不用说三行,除了前两个readLine()调用之外,你永远不会得到任何东西。

注意:不要在accept()线程中执行I / O.每个接受的套接字都应该在自己的线程中处理。

相关问题