Java Log.i打印为空
我正在制作一个程序,其中提供的网站的源代码应打印在Logcat中。但是我得到的是打印的Java Log.i打印为空,java,android,android-asynctask,Java,Android,Android Asynctask,我正在制作一个程序,其中提供的网站的源代码应打印在Logcat中。但是我得到的是打印的null。 已连接Internet,已添加networksecurityconfig,已添加cleartraffix Java代码 package com.example.guesstheceleb; import androidx.appcompat.app.AppCompatActivity; import android.os.AsyncTask; import android.os.Bundle;
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();
}
}
}
清单
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(字符串结果)
方法并在其中记录:
@覆盖
受保护的void onPostExecute(字符串结果){
Log.i(“URL内容”,结果);
}
在doInBackground
方法中,您应该返回结果,而不是返回null,因为这将用作onPostExecute
方法的输入:
受保护的字符串doInBackground(字符串…URL){
...
返回结果
}
以下是异步任务的外观:
公共静态类downloadTask扩展了AsyncTask{
@凌驾
受保护的字符串doInBackground(字符串…URL){
...
返回结果
}
@凌驾
受保护的void onPostExecute(字符串结果){
Log.i(“URL内容”,结果);
}
}
最后,在您的活动中,您可以这样使用它:
downloadTask task=new downloadTask();
任务。执行(“https://www.forbes.com/celebrities/list/")
请记住,AsyncTask已被弃用。更多信息。在AsyncTask中,您应该覆盖
onPostExecute(字符串结果)
方法并在其中记录:
@覆盖
受保护的void onPostExecute(字符串结果){
Log.i(“URL内容”,结果);
}
在doInBackground
方法中,您应该返回结果,而不是返回null,因为这将用作onPostExecute
方法的输入:
受保护的字符串doInBackground(字符串…URL){
...
返回结果
}
以下是异步任务的外观:
公共静态类downloadTask扩展了AsyncTask{
@凌驾
受保护的字符串doInBackground(字符串…URL){
...
返回结果
}
@凌驾
受保护的void onPostExecute(字符串结果){
Log.i(“URL内容”,结果);
}
}
最后,在您的活动中,您可以这样使用它:
downloadTask task=new downloadTask();
任务。执行(“https://www.forbes.com/celebrities/list/")
请记住,AsyncTask已被弃用。更多信息。查看异步任务的onpreexecute、doinbackground和onpostexecute方法。错误很可能是由于您在任务完成之前记录了结果。您必须等待异步任务完成检查我的答案我忘记在异步任务的while语句之后提供
返回结果
,检查异步任务的onpreexecute、doinbackground、onpostexecute方法。错误很可能是由于您在任务完成之前记录了结果。您必须等待异步任务完成检查我的答案我忘记在异步任务中的while语句之后提供返回结果
。我的错误是。。。我忘了提供一个关于Asynctask@PrithinBabu如果这回答了您的问题,请记住接受它作为答案。这不是答案,我希望在while语句之后添加返回结果
。您不应该使用AsyncTask执行(…).get()
方法,您应该将onPostExecute
方法添加到AsyncTask中,并将结果记录在那里,正如我在回答中所解释的。。。我忘了提供一个关于Asynctask@PrithinBabu如果这回答了您的问题,请记住接受它作为答案。这不是答案,我希望在while语句之后添加返回结果
。您不应该使用AsyncTask执行(…).get()
方法,您应该将onPostExecute
方法添加到AsyncTask中,并按照我的回答将结果记录在那里