这是一个纯Java项目,检查客户端和服务器之间的连接是否建立,查看接收到的流是否被转换为字符串等。我想检查这些步骤是否在我的Android项目中工作。 System.out.print
或 .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) {
}
}
}
最佳答案
使用内置的 Logger
文档中的复制粘贴: 通常,使用
- Log.v()
- Log.d()
- Log.i()
- Log.w()
- Log.e()
方法。
详细程度从低到高依次为 ERROR、WARN、INFO、DEBUG、VERBOSE。除非在开发期间,否则不应将 Verbose 编译到应用程序中。调试日志被编译但在运行时被剥离。始终保留错误、警告和信息日志。
提示:一个好的约定是在您的类中声明一个 TAG 常量:
private static final String TAG = "MyActivity";
并在后续调用日志方法时使用它。
提示:当你像这样打电话时,不要忘记这一点
Log.v(TAG, "index="+ i);
当您构建要传递到 Log.d 的字符串时,编译器使用 StringBuilder 并且至少会发生三个分配:StringBuilder 本身、缓冲区和 String 对象。实际上,还有另外一个buffer的分配和copy,对gc的压力更大。这意味着如果您的日志消息被过滤掉,您可能会做大量工作并产生大量开销。
关于java - 如何在 Android 项目中执行 System.out.print?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13552825/