Misuse of BufferedReader

s=new Scanner(new BufferedReader(new InputStreamReader(this.clientSocket.getInputStream()))); while(s.hasNext()){ System.out.println("Am intrat in bucla s:"); longitude=Integer.parseInt(s.next()); System.out.println("Valoare longitudine:"+longitude); latitude=Integer.parseInt(s.next()); System.out.println(latitude); 

I use the lines above to read some data from a client-server connection, this is the server side. Data is read in scanners, and after that I try to display it, but when I look in logcat I donโ€™t see anything, but this is an exception:

04-18 00: 07: 56.138: INFO / global (295): The default buffer size used in the BufferedReader Constructor. It is better to be explicit if 8k char is required.

Both my client and server are on android! Does anyone know what I'm doing wrong?

This is how I read the data, I send latitude and longitude, I assume that spaces are limited, it is strange that sometimes it works:

  Cursor c=db.getAllData(); if(c.moveToFirst()) { do{ longitude=Integer.parseInt(c.getString(1)); out.println(longitude); latitude=Integer.parseInt(c.getString(2)); out.println(latitude); }while(c.moveToNext()); } 
+6
source share
2 answers

The message is similar to the BufferedReader construct.

Firstly, I donโ€™t think you are doing something โ€œwrongโ€ because you are saying that the code is working as expected and the message is โ€œINFOโ€ and not โ€œERRORโ€ or even โ€œWARNINGโ€.

Secondly, if you look at the BufferedReader constructor, you will see:

BufferedReader (Reader in, int size) Creates a new BufferedReader, providing buffer size characters.

http://developer.android.com/reference/java/io/BufferedReader.html

Use this constructor instead, and you should not see this message.

BTW, logcat filled with output, some lines are more relevant than others.


Use Log.d instead of System.out.println() . Regarding System.out : http://developer.android.com/guide/developing/tools/adb.html

View stdout and stderr

By default, the Android system sends stdout and stderr (System.out and System.err) to / dev / null. In the processes that run Dalvik VM, you can force the system to write a copy of the output to a log file. In this case, the system writes messages to the log using the log tags stdout and stderr, both with priority I.

To route output in this way, you stop the running emulator / device instance, and then use the setprop command shell to enable output redirection. Here's how you do it:

$ adb shell stop $ adb shell setprop log.redirect-stdio true $ adb shell start

The system saves this setting until you complete the emulator / device example. To use the default setting on the emulator / device For example, you can add an entry to /data/local.prop on the device.

+3
source

Is your data limited to a space? If not, you need to specify a separator for your scanner.

You have an exception handling code that you don't show ... for example. hiding NumberFormatException if parseInt failed?

While you are debugging this problem (if you cannot connect the debugger), you can log messages at points such as when you reconnect to the client and when you enter and exit the workflow. This can help you understand if you are getting to data scanning when you think you are getting it.

+1
source

Source: https://habr.com/ru/post/886140/


All Articles