在Android Studio中,如何从网址链接获取HTML内容时纠正错误“ E / Zygote:无v2”?

问题描述

我是android studio的新手,我尝试使用AsyncTask类(不推荐使用的API)获取网页的html内容。我已附加了AndroidManifest.xml文件。我添加了访问互联网的必要权限,但仍然收到错误“ E / Zygote:无v2”,我的应用程序崩溃了。请解释此错误的含义以及如何消除该错误。我在装有Android 6.0.1的手机中启动了该应用程序

public class MainActivity extends AppCompatActivity {
    public static class DownloadTask extends AsyncTask<String,Void,String>{

        @Override
        protected String doInBackground(String... urls) {
            String result ="";
            URL url;
            HttpsURLConnection urlConnection;
            try {
                url = new URL(urls[0]);
                urlConnection =(HttpsURLConnection) 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();
                }
                return result;
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
    }

但是LogCat出现此错误

[08-11 11:46:36.426 27036-27036 /? E /合子:无v2

08-11 11:46:36.426 27036-27036 /? W / SELinux:功能:selinux_compare_spd_ram,索引[1],优先级[2],优先级为VE = SEPF_SECMOBILE_6.0.1_0035

08-11 11:46:36.436 27036-27036 /? W / SELinux:SELinux:seapp_context_lookup:seinfo = default,level = s0:c512,c768,pkgname = com.example.guessthecelebrity

08-11 11:46:36.436 27036-27036 /? I / art:后启用-Xcheck:jni

08-11 11:46:36.827 27036-27036 / com.example.guessthecelebrity D / ResourcesManager:对于用户0,新的叠加层获取了空值

08-11 11:46:36.847 27036-27036 / com.example.guessthecelebrity W /系统:ClassLoader引用的未知路径:/data/app/com.example.guessthecelebrity-2/lib/arm

08-11 11:46:36.907 27036-27036 / com.example.guessthecelebrity D / ResourcesManager:对于用户0,新的叠加层获取了空值

08-11 11:46:36.927 27036-27036 / com.example.guessthecelebrity W / art:在Android 4.1之前,方法android.graphics.PorterDuffColorFilter androidx.vectordrawable.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics。 PorterDuffColorFilter,android.content.res.ColorStateList,android.graphics.PorterDuff $ Mode)会错误地覆盖android.graphics.drawable.Drawable

中的package-private方法
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.guessthecelebrity">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permissioandroid:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:largeHeap="true"
    android:theme="@style/AppTheme">
    <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。您不在任何地方执行AsyncTask。但是,更好的清洁方法如下:

XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <EditText
            android:id="@+id/url_request"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Input a web page url to get." />

        <Button
            android:id="@+id/url_request_btn"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Request Page"
            android:onClick="downloadSiteData"/>

    </LinearLayout>


    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/url_response"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </ScrollView>

</LinearLayout>

带有AsyncTask的MainActivity:

package com.example.downloadsite;

import androidx.appcompat.app.AppCompatActivity;

import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class MainActivity extends AppCompatActivity {

    EditText urlRequest;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        urlRequest = findViewById(R.id.url_request);
    }


    public void downloadSiteData(View view) {

        String url = urlRequest.getText().toString();

        if ( url.equals("")) {
            Toast.makeText(MainActivity.this,"URL can't be empty",Toast.LENGTH_SHORT).show();
        }
        else {
            new DownloadTask().execute(url);
        }
    }

    public class DownloadTask extends AsyncTask<String,Void,String> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            Toast.makeText(MainActivity.this,"Downloading site data",Toast.LENGTH_SHORT).show();
        }


        @Override
        protected String doInBackground(String... urls) {
            String value = urls[0];
            String response = null;

            URL url = null;
            HttpURLConnection urlConnection = null;
            InputStream in = null;

            try {
                url = new URL(value);
                urlConnection = (HttpURLConnection) url.openConnection();
                in = new BufferedInputStream(urlConnection.getInputStream());
                response = readStream(in);

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

            finally {
                urlConnection.disconnect();
            }

            return response;
        }

        private String readStream(InputStream inputStream) {

            StringBuilder sb = new StringBuilder();

            try {

                BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
                String nextLine = "";
                String newLine = "";

                while ((newLine = reader.readLine()) != null) {
                    sb.append(nextLine + newLine);
                }

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

            return sb.toString();
        }

        @Override
        protected void onPostExecute(String responseString) {
            super.onPostExecute(responseString);

            TextView tv = findViewById(R.id.url_response);
            tv.setText(responseString);
        }
    }
}

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...