gpt4 book ai didi

android - 在 Logcat 中打印 InputStream

转载 作者:太空狗 更新时间:2023-10-29 15:39:05 28 4
gpt4 key购买 nike

我想在logcat中打印InputStream(用于测试/稍后我会用到它),我目前的代码如下。

        @Override
protected Void doInBackground(Void... params) {

try {

InputStream in = null;
int response = -1;

URL url = new URL(
"http://any-website.com/search/users/sports+persons");
URLConnection conn = null;
HttpURLConnection httpConn = null;
conn = url.openConnection();

if (!(conn instanceof HttpURLConnection))
throw new IOException("Not an HTTP connection");

httpConn = (HttpURLConnection) conn;
httpConn.setAllowUserInteraction(false);
httpConn.setInstanceFollowRedirects(true);
httpConn.setRequestMethod("GET");
httpConn.connect();

response = httpConn.getResponseCode();
if (response == HttpURLConnection.HTTP_OK) {
in = httpConn.getInputStream();
}

if (in != null) {
Log.e(TAG, ">>>>>PRINTING<<<<<");
Log.e(TAG, in.toString());
// TODO: print 'in' from here
}
in.close();
in = null;



} catch (Exception e) {
e.printStackTrace();
}

return null;
}

但我无法做到这一点,所以请检查代码并添加/修改代码以执行此操作。

最佳答案

String convertStreamToString(java.io.InputStream is) {
try {
return new java.util.Scanner(is).useDelimiter("\\A").next();
} catch (java.util.NoSuchElementException e) {
return "";
}
}

在你的代码中:

                Log.e(TAG, ">>>>>PRINTING<<<<<");
Log.e(TAG, in.toString());
Log.e(TAG, convertStreamToString(in));

关于android - 在 Logcat 中打印 InputStream,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11358277/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com