Am正在制作一个程序,其中所提供网站的源代码应该打印在Logcat中。但我却打印了null
。已连接Internet,添加了networksecurityconfig,添加了cleartraffix。
Java代码
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 null;
}
}
@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();
}
}
}
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;->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;->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 of 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.
清单
<?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>
在AsyncTask中,您应该覆盖onPostExecute(String result)
方法并在其中日志:
@Override
protected void onPostExecute(String result) {
Log.i("Content of 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("Content of URL", result);
}
}
最后,在你的活动中,你可以这样使用它:
downloadTask task = new downloadTask();
task.execute("https://www.forbes.com/celebrities/list/")
请记住,AsyncTask已弃用。点击此处了解更多信息。