我不知道为什么我仍然在使用AsyncTask时出现“android.os.NetworkOnMainThreadException”错误

时间:2018-06-16 10:00:52

标签: java android

我是Android编程的新手,我正试图了解AsyncTask概念,所以请查看此代码并告诉我它崩溃的原因并不断给出“android.os.NetworkOnMainThreadException”错误。 这个应用程序只是根据给定的x和y坐标使用一点网页抓取来告诉天气。

这是我的代码:

public class MainActivity extends AppCompatActivity {

    TextView weatherInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        weatherInfo = (TextView) findViewById(R.id.weather_info);
        Weathers runner = new Weathers();
        String GetInfo = runner.doInBackground();
        weatherInfo.append(GetInfo);

    }
}

class Weathers extends AsyncTask<String,String,String>{

    @Override
    protected String doInBackground(String... strings) {
        String x="19.11";
        String y="72.88";
        String result = null;
        try {
            Document doc = Jsoup.connect("https://weather.com/en-IN/weather/today/l/"+x+","+y).get();
            for (Element row : doc.select("header[class=loc-container]") ){
                result=row.text();
            }
            for (Element row : doc.select("div[class=today_nowcard-section today_nowcard-condition]") ){
                result=result+(row.text());
            }


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

        }
        return result;

    }
}

这是Android Manifest文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.inferno.sunshine">

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="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>

<uses-permission android:name="android.permission.INTERNET"/>

</manifest>

2 个答案:

答案 0 :(得分:2)

使用此

String GetInfo = runner.execute();

而不是这个

String GetInfo = runner.doInBackground();

答案 1 :(得分:0)

试试这个

    public class MainActivity extends AppCompatActivity {

    TextView weatherInfo;
    String GetInfo = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        weatherInfo = (TextView) findViewById(R.id.weather_info);
        Weathers runner = new Weathers();
        runner.execute();

    }
}

class Weathers extends AsyncTask<String,String,String>{
String result = "";
    @Override
    protected String doInBackground(String... strings) {
        String x="19.11";
        String y="72.88";

        try {
            Document doc = Jsoup.connect("https://weather.com/en-IN/weather/today/l/"+x+","+y).get();
            for (Element row : doc.select("header[class=loc-container]") ){
                result=row.text();
            }
            for (Element row : doc.select("div[class=today_nowcard-section today_nowcard-condition]") ){
                result=result+(row.text());
            }


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

        }
        return result;

    }
     @Override
        protected void onPostExecute(Void aVoid) {
             weatherInfo.append(result);
        }
}