Log.i 打印 NULL

huangapple go评论73阅读模式
英文:

Log.i printing NULL

问题

package com.example.guesstheceleb;

import androidx.appcompat.app.AppCompatActivity;

import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.concurrent.ExecutionException;

public class MainActivity extends AppCompatActivity {

    public static class downloadTask extends AsyncTask<String, Void, String>
    {

        @Override
        protected String doInBackground(String... urls) {
            String result = "";
            URL url;
            HttpURLConnection urlConnection = null;
            try {
                url = new URL(urls[0]);
                urlConnection = (HttpURLConnection) url.openConnection();
                InputStream in = urlConnection.getInputStream();
                InputStreamReader reader = new InputStreamReader(in);
                int data = reader.read();
                while (data != -1)
                {
                    char current = (char) data;
                    result += current;
                    data = reader.read();
                }
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
            return result;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        downloadTask task = new downloadTask();
        try {
            String result = task.execute("https://www.forbes.com/celebrities/list/").get();
            Log.i("Content of URL", "" + result);
        } catch (ExecutionException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.guesstheceleb">
    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:usesCleartextTraffic="true"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        android:networkSecurityConfig="@xml/network_security_config">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Logcat 部分内容太长,无法一一翻译,请参考上述 Java 代码和 Manifest 代码。

英文:

Am making a program where the source code of the provided website should be printed in the Logcat. But instead am getting null printed.
Internet is connected, networksecurityconfig added, cleartraffix added.

Java Code

package com.example.guesstheceleb;
import androidx.appcompat.app.AppCompatActivity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.concurrent.ExecutionException;
public class MainActivity extends AppCompatActivity {
public static class downloadTask extends AsyncTask&lt;String, Void, String&gt;
{
@Override
protected String doInBackground(String... urls) {
String result=&quot;&quot;;
URL url;
HttpURLConnection urlConnection= null;
try {
url= new URL(urls[0]);
urlConnection=(HttpURLConnection)url.openConnection();
InputStream in= urlConnection.getInputStream();
InputStreamReader reader = new InputStreamReader(in);
int data = reader.read();
while (data != -1)
{
char current = (char) data;
result += current;
data= reader.read();
}
}
catch (Exception e)
{
e.printStackTrace();
}
return null;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
downloadTask task = new downloadTask();
try {
String result = task.execute(&quot;https://www.forbes.com/celebrities/list/&quot;).get();
Log.i(&quot;Content of URL&quot;,&quot;&quot; + result);
} catch (ExecutionException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

Logcat

2020-09-11 08:00:47.672 7394-7394/com.example.guesstheceleb D/ApplicationLoaders: Returning zygote-cached class loader: /system/framework/android.test.base.jar
2020-09-11 08:00:47.788 7394-7394/com.example.guesstheceleb D/NetworkSecurityConfig: Using Network Security Config from resource network_security_config debugBuild: true
2020-09-11 08:00:47.790 7394-7394/com.example.guesstheceleb D/NetworkSecurityConfig: Using Network Security Config from resource network_security_config debugBuild: true
2020-09-11 08:00:47.828 7394-7419/com.example.guesstheceleb D/libEGL: loaded /vendor/lib/egl/libEGL_emulation.so
2020-09-11 08:00:47.835 7394-7419/com.example.guesstheceleb D/libEGL: loaded /vendor/lib/egl/libGLESv1_CM_emulation.so
2020-09-11 08:00:47.844 7394-7419/com.example.guesstheceleb D/libEGL: loaded /vendor/lib/egl/libGLESv2_emulation.so
2020-09-11 08:00:48.365 7394-7394/com.example.guesstheceleb W/e.guessthecele: Accessing hidden method Landroid/view/View;-&gt;computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (greylist, reflection, allowed)
2020-09-11 08:00:48.368 7394-7394/com.example.guesstheceleb W/e.guessthecele: Accessing hidden method Landroid/view/ViewGroup;-&gt;makeOptionalFitsSystemWindows()V (greylist, reflection, allowed)
2020-09-11 08:00:52.769 7394-7416/com.example.guesstheceleb I/e.guessthecele: Waiting for a blocking GC ProfileSaver
2020-09-11 08:00:52.870 7394-7416/com.example.guesstheceleb I/chatty: uid=10153(com.example.guesstheceleb) Profile Saver identical 1 line
2020-09-11 08:00:52.908 7394-7416/com.example.guesstheceleb I/e.guessthecele: Waiting for a blocking GC ProfileSaver
2020-09-11 08:00:52.910 7394-7416/com.example.guesstheceleb I/e.guessthecele: WaitForGcToComplete blocked ProfileSaver on RunEmptyCheckpoint for 140.191ms
2020-09-11 08:01:32.922 7394-7416/com.example.guesstheceleb I/e.guessthecele: Waiting for a blocking GC ProfileSaver
2020-09-11 08:01:32.932 7394-7416/com.example.guesstheceleb I/e.guessthecele: WaitForGcToComplete blocked ProfileSaver on HeapTrim for 9.893ms
2020-09-11 08:03:33.063 7394-7394/com.example.guesstheceleb I/Content&#160;of&#160;URL: null
2020-09-11 08:03:33.147 7394-7417/com.example.guesstheceleb D/HostConnection: HostConnection::get() New Host Connection established 0xec691dd0, tid 7417
2020-09-11 08:03:33.159 7394-7417/com.example.guesstheceleb D/HostConnection: HostComposition ext ANDROID_EMU_CHECKSUM_HELPER_v1 ANDROID_EMU_native_sync_v2 ANDROID_EMU_native_sync_v3 ANDROID_EMU_native_sync_v4 ANDROID_EMU_dma_v1 ANDROID_EMU_direct_mem ANDROID_EMU_host_composition_v1 ANDROID_EMU_host_composition_v2 ANDROID_EMU_vulkan ANDROID_EMU_deferred_vulkan_commands ANDROID_EMU_vulkan_null_optional_strings ANDROID_EMU_vulkan_create_resources_with_requirements ANDROID_EMU_YUV_Cache ANDROID_EMU_async_unmap_buffer ANDROID_EMU_vulkan_ignored_handles ANDROID_EMU_vulkan_free_memory_sync GL_OES_vertex_array_object GL_KHR_texture_compression_astc_ldr ANDROID_EMU_gles_max_version_2 
2020-09-11 08:03:33.163 7394-7417/com.example.guesstheceleb W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
2020-09-11 08:03:33.449 7394-7417/com.example.guesstheceleb D/EGL_emulation: eglCreateContext: 0xec49f490: maj 2 min 0 rcv 2
2020-09-11 08:03:33.647 7394-7417/com.example.guesstheceleb D/EGL_emulation: eglMakeCurrent: 0xec49f490: ver 2 0 (tinfo 0xec7ddab0) (first time)
2020-09-11 08:03:33.694 7394-7417/com.example.guesstheceleb I/Gralloc4: mapper 4.x is not supported
2020-09-11 08:03:33.695 7394-7417/com.example.guesstheceleb D/HostConnection: createUnique: call
2020-09-11 08:03:33.696 7394-7417/com.example.guesstheceleb D/HostConnection: HostConnection::get() New Host Connection established 0xec693330, tid 7417
2020-09-11 08:03:33.697 7394-7417/com.example.guesstheceleb D/goldfish-address-space: allocate: Ask for block of size 0x100
2020-09-11 08:03:33.697 7394-7417/com.example.guesstheceleb D/goldfish-address-space: allocate: ioctl allocate returned offset 0x3f9f9c000 size 0x2000
2020-09-11 08:03:33.712 7394-7417/com.example.guesstheceleb D/HostConnection: HostComposition ext ANDROID_EMU_CHECKSUM_HELPER_v1 ANDROID_EMU_native_sync_v2 ANDROID_EMU_native_sync_v3 ANDROID_EMU_native_sync_v4 ANDROID_EMU_dma_v1 ANDROID_EMU_direct_mem ANDROID_EMU_host_composition_v1 ANDROID_EMU_host_composition_v2 ANDROID_EMU_vulkan ANDROID_EMU_deferred_vulkan_commands ANDROID_EMU_vulkan_null_optional_strings ANDROID_EMU_vulkan_create_resources_with_requirements ANDROID_EMU_YUV_Cache ANDROID_EMU_async_unmap_buffer ANDROID_EMU_vulkan_ignored_handles ANDROID_EMU_vulkan_free_memory_sync GL_OES_vertex_array_object GL_KHR_texture_compression_astc_ldr ANDROID_EMU_gles_max_version_2 
2020-09-11 08:03:33.874 7394-7394/com.example.guesstheceleb I/Choreographer: Skipped 45 frames!  The application may be doing too much work on its main thread.

Manifest

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;manifest xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
package=&quot;com.example.guesstheceleb&quot;&gt;
&lt;uses-permission android:name=&quot;android.permission.INTERNET&quot; /&gt;
&lt;application
android:allowBackup=&quot;true&quot;
android:icon=&quot;@mipmap/ic_launcher&quot;
android:label=&quot;@string/app_name&quot;
android:roundIcon=&quot;@mipmap/ic_launcher_round&quot;
android:usesCleartextTraffic=&quot;true&quot;
android:supportsRtl=&quot;true&quot;
android:theme=&quot;@style/AppTheme&quot;
android:networkSecurityConfig=&quot;@xml/network_security_config&quot;&gt;
&lt;activity android:name=&quot;.MainActivity&quot;&gt;
&lt;intent-filter&gt;
&lt;action android:name=&quot;android.intent.action.MAIN&quot; /&gt;
&lt;category android:name=&quot;android.intent.category.LAUNCHER&quot; /&gt;
&lt;/intent-filter&gt;
&lt;/activity&gt;
&lt;/application&gt;
&lt;/manifest&gt;

答案1

得分: 2

在你的AsyncTask内部,你应该重写onPostExecute(String result)方法,并在其中进行日志记录:

@Override
protected void onPostExecute(String result) {
   Log.i("URL的内容", result);
}

在你的doInBackground方法中,不要返回null,而应该返回结果,因为这将作为输入传递给onPostExecute方法:

protected String doInBackground(String... urls) {
   ...
   return result;
}

这是你的AsyncTask应该的样子:

public static class downloadTask extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... urls) {
        ...
        return result;
    }

    @Override
    protected void onPostExecute(String result) {
        Log.i("URL的内容", result);
    }
}

最后,在你的Activity中,你可以像这样使用它:

downloadTask task = new downloadTask();
task.execute("https://www.forbes.com/celebrities/list/")

请注意,AsyncTask已被弃用。更多信息请参阅此处

英文:

Inside your AsyncTask, you should override the onPostExecute(String result) method and log inside it:

@Override
protected void onPostExecute(String result) {
   Log.i(&quot;Content of URL&quot;, result);
}

In your doInBackground method, instead of returning null, you should return the the result as this will be used as input to the onPostExecute method:

protected String doInBackground(String... urls) {
   ...
   return result
}

This is how your AsyncTask should look like:

public static class downloadTask extends AsyncTask&lt;String, Void, String&gt; {

    @Override
    protected String doInBackground(String... urls) {
        ...
        return result
    }

    @Override
    protected void onPostExecute(String result) {
        Log.i(&quot;Content of URL&quot;, result);
    }
}

Finally, in your Activity, you can use it like this:

downloadTask task = new downloadTask();
task.execute(&quot;https://www.forbes.com/celebrities/list/&quot;)

Please, bear in mind AsyncTask is deprecated. More info here.

huangapple
  • 本文由 发表于 2020年9月11日 10:42:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/63840114.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定