-1

I am new to android programming and i am trying to get my head around the AsyncTask concept so please review this code and tell me why it is crashing and keeps giving the "android.os.NetworkOnMainThreadException" error. This app just tells the weather according to the given x and y coordinates using a bit web scraping.

Here is my code:

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;

    }
}

Here is the Android Manifest file:

<?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 Answers2

2

Use this

String GetInfo = runner.execute();

Instead of this

String GetInfo = runner.doInBackground();
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
0

try this

    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);
        }
}
Omkar
  • 3,040
  • 1
  • 22
  • 42