How to make System.out.print in an Android project?

This is a pure Java project to check if a connection has been established between the client and the server, see if the received stream has been converted to a string, etc. I want to check if these steps work in my Android project. What is the equivalent of System.out.print or .println .

 public class TestConnect { static String result = ""; public static void main(String[] args) { try { HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost("http://127.0.0.1/index.php"); HttpResponse response = httpclient.execute(httppost); HttpEntity entity = response.getEntity(); InputStream is = entity.getContent(); System.out.println(is); try { BufferedReader reader = new BufferedReader( new InputStreamReader(is, "UTF-8"), 8); StringBuilder sb = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { System.out.print(line); sb.append(line + "\n"); } is.close(); result = sb.toString(); } catch (Exception e) { } try { JSONArray jArray = new JSONArray(result); for (int i = 0; i < jArray.length(); i++) { JSONObject json_data = jArray.getJSONObject(i); json_data.getString("nom"); // json_data.getInt("Id_Patients"); System.out.println(json_data.getString("nom")); // r.add(json_data.getString("categorie")); } } catch (Exception e) { } } catch (Exception e) { } } } 
+4
source share
4 answers

Use the built-in logger

Copy paste from the documentation: Usually use

  • Log.v ()
  • Log.d ()
  • Log.i ()
  • Log.w ()
  • Log.e ()

methods.

The order from the point of view of verbosity, from smallest to largest, is ERROR, WAR, INFO, DEBUG, VERBIS. Details should never be compiled into an application, except during development. Debug logs are compiled but deleted at runtime. Error, warning and information logs are always kept.

Tip. A good convention is to declare a TAG constant in your class:

private static final String TAG = "MyActivity";

and use this in subsequent calls to the log methods.

Tip. Do not forget that when you make a call, for example

Log.v(TAG, "index=" + i);

that when you create a string to pass to Log.d, the compiler uses StringBuilder and there are at least three distributions: the StringBuilder itself, the buffer, and the String object. Actually, there is another distribution and copying of the buffer, and even more pressure on gc. This means that if your log message is filtered, you may be doing a lot of work and you will get significant overhead.

+3
source

Use Log to print the desired values ​​in LogCat.

 Log.v(MYTAG, myString); 

MYTAG : The tag that you want to associate with log messages so that you can send your LogCat messages later to check only the necessary messages.

myString : The value you want to log in String format.

You can use any of the log methods from Log.v (), Log.d (), Log.i (), Log.w (), Log.e () according to how you want to mark your message log as . For example, if you want to mark your log message as a debug message, use Log.d ().

+1
source

Use the Log class.

 Log.i("TAG", "info"); Log.d("TAG", "debug"); 
+1
source

You can use System.out.println() It behaves the same as Log.i(TAG, String);

However, on Android, you can use various logging methods:

  • Log.v() - Detailed
  • Log.d() - Debugging
  • Log.i() - Information
  • Log.w() - Warning
  • Log.e() - Error
+1
source

All Articles