[英]android client-server(socket) response from the server is null
//服務器方法
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上運行服務器,在電話上運行客戶端,然后發送消息時,我得到以下信息。 服務器正常獲取消息: 服務器日志
但是服務器到電話的響應始終是空的客戶端日志
我認為我對輸入/輸出流做錯了。 請幫我解決這個問題。/
客戶端日志 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/ ]
/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/ ]
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()
調用中,除了null之外,您什么都不會得到。
注意:不要在accept()
線程中進行I / O。 每個接受的套接字都應在其自己的線程中處理。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.